From 6d6ffa16ff145c28df268670170d6a9c2f336a7b Mon Sep 17 00:00:00 2001 From: rramiachraf <51409801+rramiachraf@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:01:04 +0100 Subject: [PATCH] feat: add instances handler --- Makefile | 2 ++ handlers/cache.go | 5 ++-- handlers/instances.go | 56 +++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + static/style.css | 7 +++++- views/footer.templ | 7 ++++-- 6 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 handlers/instances.go diff --git a/Makefile b/Makefile index 18c0e46..73839ca 100644 --- a/Makefile +++ b/Makefile @@ -4,3 +4,5 @@ gentempl: @command -v templ &> /dev/null || go install github.com/a-h/templ/cmd/templ@latest build:gentempl templ generate && go build -ldflags="-X 'github.com/rramiachraf/dumb/data.Version=$(VERSION)' -s -w" +fmt: + templ fmt . diff --git a/handlers/cache.go b/handlers/cache.go index af1d684..4874057 100644 --- a/handlers/cache.go +++ b/handlers/cache.go @@ -1,6 +1,7 @@ package handlers import ( + "context" "encoding/json" "time" @@ -9,10 +10,10 @@ import ( ) type cachable interface { - data.Album | data.Song | data.Annotation + data.Album | data.Song | data.Annotation | []byte } -var c, _ = bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 24)) +var c, _ = bigcache.New(context.Background(), bigcache.DefaultConfig(time.Hour*24)) func setCache(key string, entry interface{}) error { data, err := json.Marshal(&entry) diff --git a/handlers/instances.go b/handlers/instances.go new file mode 100644 index 0000000..8df70ec --- /dev/null +++ b/handlers/instances.go @@ -0,0 +1,56 @@ +package handlers + +import ( + "context" + "io" + "net/http" + + "github.com/rramiachraf/dumb/views" + "github.com/sirupsen/logrus" +) + +const ContentTypeJSON = "application/json" + +// TODO: move this to utils, so it can be used by other handlers. +func sendError(err error, status int, msg string, l *logrus.Logger, w http.ResponseWriter) { + l.Errorln(err) + w.WriteHeader(status) + if err := views.ErrorPage(status, msg).Render(context.Background(), w); err != nil { + l.Errorln(err) + } +} + +func Instances(l *logrus.Logger) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if instances, err := getCache[[]byte]("instances"); err == nil { + w.Header().Set("content-type", ContentTypeJSON) + _, err = w.Write(instances) + if err == nil { + return + } + } + + res, err := sendRequest("https://raw.githubusercontent.com/rramiachraf/dumb/main/instances.json") + if err != nil { + sendError(err, http.StatusInternalServerError, "something went wrong", l, w) + return + } + + defer res.Body.Close() + + instances, err := io.ReadAll(res.Body) + if err != nil { + sendError(err, http.StatusInternalServerError, "something went wrong", l, w) + return + } + + w.Header().Set("content-type", ContentTypeJSON) + if _, err = w.Write(instances); err != nil { + l.Errorln(err) + } + + if err = setCache("instances", instances); err != nil { + l.Errorln(err) + } + } +} diff --git a/main.go b/main.go index 39c9c21..67d06e9 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ func main() { r.HandleFunc("/images/{filename}.{ext}", handlers.ImageProxy(logger)).Methods("GET") r.HandleFunc("/search", handlers.Search(logger)).Methods("GET") r.HandleFunc("/{id}/{artist-song}/{verse}/annotations", handlers.Annotations(logger)).Methods("GET") + r.HandleFunc("/instances.json", handlers.Instances(logger)).Methods("GET") r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) diff --git a/static/style.css b/static/style.css index 6125f70..74e5968 100644 --- a/static/style.css +++ b/static/style.css @@ -245,7 +245,7 @@ footer { padding: 1rem 0; } -#footer_container { +#footer-container { width: 1024px; display: flex; justify-content: space-between; @@ -253,6 +253,11 @@ footer { margin: 0 auto; } +#footer-links { + display: flex; + gap: 1rem; +} + #version { font-size: 1.3rem; color: #1b1b1b; diff --git a/views/footer.templ b/views/footer.templ index 57760e0..0aee5da 100644 --- a/views/footer.templ +++ b/views/footer.templ @@ -4,8 +4,11 @@ import "github.com/rramiachraf/dumb/data" templ footer() {