diff --git a/README.md b/README.md index 6d3164f..4f22efd 100644 --- a/README.md +++ b/README.md @@ -9,20 +9,26 @@ Short is a very simple url shortener build in golang using gorilla/mux for url r ```shell Usage of short: -addr string - Address to listen for connections (default "localhost") + Address to listen for connections (default "localhost") + -cleanup int + Cleanup interval in hours, default 1 (default 1) -domain string - Domain to write to the URLs (default "localhost") - -dump string - Path to the file to dump the kv db (default "urls.json") + Domain to write to the URLs (default "localhost") + -dumpFile string + urls.json (default "Path to the file to dump the kv db") + -exp int + Default expiration time in hours, default 240 (default 240) + -http + proto to the base URL (HTTPS://localhost/path/... no real https here just to set the url (for like a proxy offloading https -path string - Path to the base URL (https://localhost/PATH/... remember to append a / at the end - -port string - Port to listen for connections (default "8080") - -proto string - proto to the base URL (HTTPS://localhost/path/... no real https here just to set the url (for like a proxy offloading https (default "https") - -urlsize int - Define the size of the shortened String, default 10 (default 10) - -v prints current version + Path to the base URL (https://localhost/PATH/... + -port int + Port to listen for connections (default 8080) + -size int + Define the size of the shortened String (default 10) + -urlPort int + Port to use for building URLs (default 443) + -v prints current version ``` Includes a Dockerfile to for a standalone docker image. diff --git a/internal/shortie/shortie.go b/internal/shortie/shortie.go index c94c9ab..257d601 100644 --- a/internal/shortie/shortie.go +++ b/internal/shortie/shortie.go @@ -184,7 +184,7 @@ func internalError(msg string, err error) body { } } -// IndexHandler return a fasthttp.RequestHandler function that genetares the index page +// IndexHandler return a fasthttp.RequestHandler function that generates the index page func IndexHandler(t *template.Template) func(ctx *fasthttp.RequestCtx) { return func(ctx *fasthttp.RequestCtx) { ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html")) @@ -196,7 +196,7 @@ func IndexHandler(t *template.Template) func(ctx *fasthttp.RequestCtx) { } } -// Short return a fasthttp.RequestHandler function that genetares the shortener page +// Short return a fasthttp.RequestHandler function that generates the shortener page func Short(t *template.Template) func(ctx *fasthttp.RequestCtx) { return func(ctx *fasthttp.RequestCtx) { ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html")) @@ -227,7 +227,7 @@ func Short(t *template.Template) func(ctx *fasthttp.RequestCtx) { } } -// Redir return a fasthttp.RequestHandler function that genetares the shortener redirect page +// Redir return a fasthttp.RequestHandler function that generates the shortener redirect page func Redir(t *template.Template) func(ctx *fasthttp.RequestCtx) { return func(ctx *fasthttp.RequestCtx) { ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html")) diff --git a/main.go b/main.go index 168d7a8..cad3e9a 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "flag" "fmt" "html/template" @@ -56,6 +57,33 @@ func getHTTP(ctx *fasthttp.RequestCtx) string { return "HTTP/1.0" } +func healthz() func(ctx *fasthttp.RequestCtx) { + r := struct { + Status string `json:"status"` + StatusCode int `json:"status_code"` + }{ + Status: "ok", + StatusCode: 200, + } + + t := time.Now() + shortie.Pool.Set("status", t.Unix(), -1) + s, f := shortie.Pool.Get("status") + + if !f || s != t.Unix() { + r.Status = "error" + r.StatusCode = 500 + } + + return func(ctx *fasthttp.RequestCtx) { + ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("application/json")) + ctx.Response.SetStatusCode(r.StatusCode) + if err := json.NewEncoder(ctx).Encode(r); err != nil { + ctx.Error(err.Error(), fasthttp.StatusInternalServerError) + } + } +} + func main() { var ( addr = flag.String("addr", "localhost", "Address to listen for connections") @@ -121,6 +149,7 @@ func main() { r.GET("/", shortie.IndexHandler(t)) r.POST("/", shortie.Short(t)) r.GET("/{key}", shortie.Redir(t)) + r.GET("/healthz", healthz()) r.GET("/v1/toFile", shortie.ToFile(t)) r.GET("/v1/fromFile", shortie.FromFile(t)) r.GET("/v1/count", func(ctx *fasthttp.RequestCtx) { fmt.Fprintf(ctx, "%v", shortie.Pool.ItemCount()) })