package main import ( "context" "github.com/prometheus/client_golang/prometheus" "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" "log" "net/http" "os" "regexp" "strings" "time" ) type inventoryItemType struct { 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 mongoURI string = "mongodb://127.0.0.1:27017/default?replicaSet=rs0" var collectionName string = "inventory" var redirectFound string = os.Getenv("GOREDIRECT_FOUND") var redirectNotFound string = os.Getenv("GOREDIRECT_NOT_FOUND") func wrapper(coll *mongo.Collection) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { counterQueries.Inc() slug := r.URL.Path[1:] match := reValid.MatchString(slug) if !match { counterInvalidSlug.Inc() http.Error(w, "Invalid slug", 400) return } var doc inventoryItemType err := coll.FindOne(context.TODO(), bson.M{"shortener.slug": slug}).Decode(&doc) if err != nil { counterNotFound.Inc() if redirectNotFound == "" { http.NotFound(w, r) return } http.Redirect(w, r, strings.Replace(redirectNotFound, "%s", slug, 1), 302) return } 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) } } var ( counterQueries = promauto.NewCounter(prometheus.CounterOpts{ Name: "goredirect_queries", Help: "The total number of queries.", }) counterInvalidSlug = promauto.NewCounter(prometheus.CounterOpts{ Name: "goredirect_invalid_slug", Help: "The total number of queries that did not match regex.", }) counterNotFound = promauto.NewCounter(prometheus.CounterOpts{ Name: "goredirect_not_found", Help: "The total number of queries with unknown slug.", }) counterFound = promauto.NewCounter(prometheus.CounterOpts{ Name: "goredirect_found", Help: "The total number of queries that resulted in successful redirect.", }) ) func main() { if val := os.Getenv("GOREDIRECT_REGEX"); val != "" { reValid = regexp.MustCompile(val) } if val := os.Getenv("MONGO_URI"); val != "" { mongoURI = val } if val := os.Getenv("GOREDIRECT_COLLECTION"); val != "" { collectionName = val } cs, err := connstring.ParseAndValidate(mongoURI) client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI)) if err != nil { log.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) err = client.Connect(ctx) if err != nil { log.Fatal(err) } cancel() coll := client.Database(cs.Database).Collection(collectionName) defer client.Disconnect(ctx) http.Handle("/metrics", promhttp.Handler()) http.HandleFunc("/", wrapper(coll)) log.Printf("Starting HTTP server\n") err2 := http.ListenAndServe(":80", nil) if err2 != nil { log.Fatal("ListenAndServe: ", err2) } }