mirror of
https://github.com/rramiachraf/dumb.git
synced 2025-04-04 21:37:38 +03:00
fix cached annotations not displaying
This commit is contained in:
parent
903d4ca99b
commit
679f34678c
4 changed files with 20 additions and 20 deletions
|
@ -3,13 +3,13 @@ package data
|
|||
type AnnotationsResponse struct {
|
||||
Response struct {
|
||||
Referent struct {
|
||||
Annotations []Annotation `json:"annotations"`
|
||||
Annotations []struct {
|
||||
Body Annotation `json:"body"`
|
||||
} `json:"annotations"`
|
||||
} `json:"referent"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
type Annotation struct {
|
||||
Body struct {
|
||||
Html string `json:"html"`
|
||||
} `json:"body"`
|
||||
HTML string `json:"html"`
|
||||
}
|
||||
|
|
|
@ -18,14 +18,14 @@ import (
|
|||
func Annotations(l *logrus.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := mux.Vars(r)["id"]
|
||||
|
||||
if data, err := getCache[data.Annotation](id); err == nil {
|
||||
if a, err := getCache[data.Annotation]("annotation:" + id); err == nil {
|
||||
encoder := json.NewEncoder(w)
|
||||
|
||||
w.Header().Set("content-type", "application/json")
|
||||
if err = encoder.Encode(&data); err != nil {
|
||||
if err = encoder.Encode(&a); err != nil {
|
||||
l.Errorln(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -65,25 +65,20 @@ func Annotations(l *logrus.Logger) http.HandlerFunc {
|
|||
return
|
||||
}
|
||||
|
||||
w.Header().Set("content-type", "application/json")
|
||||
body := data.Response.Referent.Annotations[0].Body
|
||||
body.Html = cleanBody(body.Html)
|
||||
response, err := json.Marshal(body)
|
||||
body.HTML = cleanBody(body.HTML)
|
||||
|
||||
if err != nil {
|
||||
l.Errorf("could not marshal json: %s\n", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
||||
w.Header().Set("content-type", "application/json")
|
||||
encoder := json.NewEncoder(w)
|
||||
|
||||
if err = encoder.Encode(&body); err != nil {
|
||||
l.Errorln("Error sending response: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = setCache(id, body); err != nil {
|
||||
if err = setCache("annotation:"+id, body); err != nil {
|
||||
l.Errorln(err)
|
||||
}
|
||||
|
||||
if _, err = w.Write(response); err != nil {
|
||||
l.Errorln("Error sending response: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,10 @@ import (
|
|||
"github.com/rramiachraf/dumb/data"
|
||||
)
|
||||
|
||||
type cachable interface {
|
||||
data.Album | data.Song | data.Annotation
|
||||
}
|
||||
|
||||
var c, _ = bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 24))
|
||||
|
||||
func setCache(key string, entry interface{}) error {
|
||||
|
@ -19,7 +23,7 @@ func setCache(key string, entry interface{}) error {
|
|||
return c.Set(key, data)
|
||||
}
|
||||
|
||||
func getCache[v data.Album | data.Song | data.Annotation](key string) (v, error) {
|
||||
func getCache[v cachable](key string) (v, error) {
|
||||
var decoded v
|
||||
|
||||
data, err := c.Get(key)
|
||||
|
|
|
@ -14,6 +14,7 @@ document.querySelectorAll("#lyrics a").forEach(item => {
|
|||
|
||||
function getAnnotation(e) {
|
||||
e.preventDefault()
|
||||
//document.querySelector('.annotation')?.remove()
|
||||
const uri = e.target.parentElement.getAttribute("href")
|
||||
const presentAnnotation = document.getElementById(uri)
|
||||
if (presentAnnotation) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue