10
0
goredirect/main.go

151 lines
3.8 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
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:]
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
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()
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() {
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
}