10
0
goredirect/main.go

124 lines
3.3 KiB
Go
Raw Normal View History

2021-05-25 20:28:00 +00:00
package main
import (
"context"
2021-06-17 10:52:17 +00:00
"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"
2021-07-08 10:48:58 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2021-06-17 10:52:17 +00:00
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
2021-05-25 20:28:00 +00:00
"log"
2021-06-17 10:52:17 +00:00
"net/http"
2021-05-25 20:28:00 +00:00
"os"
"regexp"
2021-06-17 10:52:17 +00:00
"strings"
2021-05-25 20:28:00 +00:00
"time"
)
2021-06-17 10:52:17 +00:00
type inventoryItemType struct {
2021-07-08 10:48:58 +00:00
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"`
2021-05-25 20:28:00 +00:00
}
2021-06-17 10:52:17 +00:00
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")
2021-05-25 20:28:00 +00:00
var redirectNotFound string = os.Getenv("GOREDIRECT_NOT_FOUND")
2021-06-17 10:52:17 +00:00
func wrapper(coll *mongo.Collection) func(w http.ResponseWriter, r *http.Request) {
2021-05-25 20:28:00 +00:00
return func(w http.ResponseWriter, r *http.Request) {
2021-06-17 10:52:17 +00:00
counterQueries.Inc()
2021-05-25 20:28:00 +00:00
slug := r.URL.Path[1:]
match := reValid.MatchString(slug)
2021-06-17 10:52:17 +00:00
if !match {
counterInvalidSlug.Inc()
http.Error(w, "Invalid slug", 400)
return
2021-05-25 20:28:00 +00:00
}
2021-06-17 10:52:17 +00:00
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
2021-05-25 20:28:00 +00:00
}
2021-06-17 10:52:17 +00:00
counterFound.Inc()
var u = doc.Shortener.URL
2021-07-08 10:48:58 +00:00
if u == "" {
u = strings.Replace(redirectFound, "%s", doc.ID.Hex(), 1)
}
2021-06-17 10:52:17 +00:00
http.Redirect(w, r, u, 302)
log.Printf("Redirecting %s to %s\n", slug, doc.Shortener.URL)
2021-05-25 20:28:00 +00:00
}
}
2021-06-17 10:52:17 +00:00
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.",
})
)
2021-05-25 20:28:00 +00:00
func main() {
2021-06-17 10:52:17 +00:00
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))
2021-05-25 20:28:00 +00:00
if err != nil {
log.Fatal(err)
}
2021-06-17 10:52:17 +00:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2021-05-25 20:28:00 +00:00
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
2021-06-17 10:52:17 +00:00
cancel()
coll := client.Database(cs.Database).Collection(collectionName)
2021-05-25 20:28:00 +00:00
defer client.Disconnect(ctx)
2021-06-17 10:52:17 +00:00
http.Handle("/metrics", promhttp.Handler())
2021-05-25 20:28:00 +00:00
http.HandleFunc("/", wrapper(coll))
2021-06-17 10:52:17 +00:00
2021-05-25 20:28:00 +00:00
log.Printf("Starting HTTP server\n")
2023-08-04 02:58:13 +00:00
err2 := http.ListenAndServe(":8080", nil)
2021-05-25 20:28:00 +00:00
if err2 != nil {
log.Fatal("ListenAndServe: ", err2)
}
2021-06-17 10:52:17 +00:00
2021-05-25 20:28:00 +00:00
}