|
|
|
@ -6,6 +6,7 @@ import ( |
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto" |
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp" |
|
|
|
|
"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/options" |
|
|
|
|
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring" |
|
|
|
@ -17,13 +18,12 @@ import ( |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type shortenerAttrType struct { |
|
|
|
|
Slug string |
|
|
|
|
URL string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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}$") |
|
|
|
@ -58,6 +58,9 @@ func wrapper(coll *mongo.Collection) func(w http.ResponseWriter, r *http.Request |
|
|
|
|
|
|
|
|
|
counterFound.Inc() |
|
|
|
|
var u = doc.Shortener.URL |
|
|
|
|
if u == "" { |
|
|
|
|
u = strings.Replace(redirectFound, "%s", doc.ID.Hex(), 1) |
|
|
|
|
} |
|
|
|
|
http.Redirect(w, r, u, 302) |
|
|
|
|
log.Printf("Redirecting %s to %s\n", slug, doc.Shortener.URL) |
|
|
|
|
} |
|
|
|
|