10
0
goredirect/main.go

171 lines
4.0 KiB
Go
Raw Normal View History

2021-05-25 20:28:00 +00:00
package main
import (
"context"
2024-09-13 18:48:41 +00:00
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
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
)
2021-06-17 10:52:17 +00:00
type inventoryItemType struct {
2024-09-13 18:48:41 +00:00
ID primitive.ObjectID `bson:"_id" json:"_id,omitempty"`
2021-07-08 10:48:58 +00:00
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
}
2024-09-13 18:48:41 +00:00
var (
mongoURI = "mongodb://127.0.0.1:27017/default?replicaSet=rs0"
mongoCollection = "inventory"
)
var (
regexValid = regexp.MustCompile("^[a-zA-Z0-9]{4,6}$")
redirectFound = os.Getenv("GOREDIRECT_FOUND")
redirectNotFound = os.Getenv("GOREDIRECT_NOT_FOUND")
redirectNoPath = os.Getenv("GOREDIRECT_NOPATH")
)
2021-05-25 20:28:00 +00:00
func lookupHandler(ctx context.Context, 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:]
2024-09-13 18:48:41 +00:00
// Prevalidation
if slug == "" && redirectNoPath != "" {
counterNoPath.Inc()
log.Printf("Redirecting empty slug to %s\n", redirectNoPath)
http.Redirect(w, r, redirectNoPath, 302)
return
}
if match := regexValid.MatchString(slug); !match {
2021-06-17 10:52:17 +00:00
counterInvalidSlug.Inc()
2024-09-13 18:48:41 +00:00
2021-06-17 10:52:17 +00:00
http.Error(w, "Invalid slug", 400)
return
2021-05-25 20:28:00 +00:00
}
2024-09-13 18:48:41 +00:00
// Query
2021-06-17 10:52:17 +00:00
var doc inventoryItemType
if err := coll.FindOne(ctx, bson.M{"shortener.slug": slug}).Decode(&doc); err != nil {
2021-06-17 10:52:17 +00:00
counterNotFound.Inc()
2021-06-17 10:52:17 +00:00
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()
2024-09-13 18:48:41 +00:00
redirURL := doc.Shortener.URL
if redirURL == "" {
redirURL = strings.Replace(redirectFound, "%s", doc.ID.Hex(), 1)
2021-07-08 10:48:58 +00:00
}
2024-09-13 18:48:41 +00:00
http.Redirect(w, r, redirURL, 302)
log.Printf("Redirecting %s to %s\n", slug, redirURL)
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.",
})
2024-09-13 18:48:41 +00:00
counterNoPath = promauto.NewCounter(prometheus.CounterOpts{
Name: "goredirect_not_found",
Help: "The total number of queries with unknown slug.",
})
2021-06-17 10:52:17 +00:00
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() {
ctx := context.Background()
2021-06-17 10:52:17 +00:00
if val := os.Getenv("GOREDIRECT_REGEX"); val != "" {
regexValid = regexp.MustCompile(val)
2021-06-17 10:52:17 +00:00
}
if val := os.Getenv("MONGO_URI"); val != "" {
mongoURI = val
}
if val := os.Getenv("GOREDIRECT_COLLECTION"); val != "" {
mongoCollection = val
2021-06-17 10:52:17 +00:00
}
// Mongo database //
2021-06-17 10:52:17 +00:00
cs, err := connstring.ParseAndValidate(mongoURI)
2021-05-25 20:28:00 +00:00
if err != nil {
log.Fatal(err)
os.Exit(1)
2021-05-25 20:28:00 +00:00
}
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
2021-05-25 20:28:00 +00:00
if err != nil {
log.Fatal(err)
os.Exit(1)
}
connectCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
if err := client.Connect(connectCtx); err != nil {
log.Fatal(err)
os.Exit(1)
2021-05-25 20:28:00 +00:00
}
2021-06-17 10:52:17 +00:00
cancel()
coll := client.Database(cs.Database).Collection(mongoCollection)
2021-05-25 20:28:00 +00:00
defer client.Disconnect(ctx)
// HTTP Server //
2021-06-17 10:52:17 +00:00
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", lookupHandler(ctx, coll))
2021-06-17 10:52:17 +00:00
log.Printf("Starting HTTP server on :8080\n")
2021-06-17 10:52:17 +00:00
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe: ", err)
os.Exit(1)
}
2021-05-25 20:28:00 +00:00
}