feat: add instances handler
This commit is contained in:
parent
53462a9412
commit
6d6ffa16ff
2
Makefile
2
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 .
|
||||
|
@ -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)
|
||||
|
56
handlers/instances.go
Normal file
56
handlers/instances.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
1
main.go
1
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)
|
||||
|
@ -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;
|
||||
|
@ -4,8 +4,11 @@ import "github.com/rramiachraf/dumb/data"
|
||||
|
||||
templ 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="/instances.json">Instances</a>
|
||||
</div>
|
||||
<p id="version">{ data.Version }</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
Loading…
x
Reference in New Issue
Block a user