Handle missing/empty `shortener.url`
This commit is contained in:
parent
fe102e072d
commit
b698fc6b88
15
main.go
15
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
|
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
|
||||||
|
@ -17,13 +18,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type shortenerAttrType struct {
|
|
||||||
Slug string
|
|
||||||
URL string
|
|
||||||
}
|
|
||||||
|
|
||||||
type inventoryItemType struct {
|
type inventoryItemType struct {
|
||||||
Shortener shortenerAttrType
|
ID primitive.ObjectID `bson:"_id" json:"_id,omitempty"`
|
||||||
|
Shortener struct {
|
||||||
|
Slug string `bson:"slug" json:"slug"`
|
||||||
|
URL string `bson:"url" json:"url"`
|
||||||
|
} `bson:"shortener" json:"shortener"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var reValid = regexp.MustCompile("^[a-zA-Z0-9]{4,6}$")
|
var reValid = regexp.MustCompile("^[a-zA-Z0-9]{4,6}$")
|
||||||
|
@ -58,6 +58,9 @@ func wrapper(coll *mongo.Collection) func(w http.ResponseWriter, r *http.Request
|
||||||
|
|
||||||
counterFound.Inc()
|
counterFound.Inc()
|
||||||
var u = doc.Shortener.URL
|
var u = doc.Shortener.URL
|
||||||
|
if u == "" {
|
||||||
|
u = strings.Replace(redirectFound, "%s", doc.ID.Hex(), 1)
|
||||||
|
}
|
||||||
http.Redirect(w, r, u, 302)
|
http.Redirect(w, r, u, 302)
|
||||||
log.Printf("Redirecting %s to %s\n", slug, doc.Shortener.URL)
|
log.Printf("Redirecting %s to %s\n", slug, doc.Shortener.URL)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue