feat: add instances handler

This commit is contained in:
rramiachraf 2024-03-06 12:01:04 +01:00
parent 53462a9412
commit 6d6ffa16ff
6 changed files with 73 additions and 5 deletions

View File

@ -4,3 +4,5 @@ gentempl:
@command -v templ &> /dev/null || go install github.com/a-h/templ/cmd/templ@latest @command -v templ &> /dev/null || go install github.com/a-h/templ/cmd/templ@latest
build:gentempl build:gentempl
templ generate && go build -ldflags="-X 'github.com/rramiachraf/dumb/data.Version=$(VERSION)' -s -w" templ generate && go build -ldflags="-X 'github.com/rramiachraf/dumb/data.Version=$(VERSION)' -s -w"
fmt:
templ fmt .

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"context"
"encoding/json" "encoding/json"
"time" "time"
@ -9,10 +10,10 @@ import (
) )
type cachable interface { 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 { func setCache(key string, entry interface{}) error {
data, err := json.Marshal(&entry) data, err := json.Marshal(&entry)

56
handlers/instances.go Normal file
View File

@ -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)
}
}
}

View File

@ -29,6 +29,7 @@ func main() {
r.HandleFunc("/images/{filename}.{ext}", handlers.ImageProxy(logger)).Methods("GET") r.HandleFunc("/images/{filename}.{ext}", handlers.ImageProxy(logger)).Methods("GET")
r.HandleFunc("/search", handlers.Search(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("/{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.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)

View File

@ -245,7 +245,7 @@ footer {
padding: 1rem 0; padding: 1rem 0;
} }
#footer_container { #footer-container {
width: 1024px; width: 1024px;
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
@ -253,6 +253,11 @@ footer {
margin: 0 auto; margin: 0 auto;
} }
#footer-links {
display: flex;
gap: 1rem;
}
#version { #version {
font-size: 1.3rem; font-size: 1.3rem;
color: #1b1b1b; color: #1b1b1b;

View File

@ -4,8 +4,11 @@ import "github.com/rramiachraf/dumb/data"
templ footer() { templ footer() {
<footer> <footer>
<div id="footer_container"> <div id="footer-container">
<div id="footer-links">
<a rel="noopener noreferrer" target="_blank" href="https://github.com/rramiachraf/dumb">Source Code</a> <a rel="noopener noreferrer" target="_blank" href="https://github.com/rramiachraf/dumb">Source Code</a>
<a rel="noopener noreferrer" target="_blank" href="/instances.json">Instances</a>
</div>
<p id="version">{ data.Version }</p> <p id="version">{ data.Version }</p>
</div> </div>
</footer> </footer>