From 4b70a153aaeb1c58a0248628c306817b8d3cce9d Mon Sep 17 00:00:00 2001 From: rramiachraf <51409801+rramiachraf@users.noreply.github.com> Date: Wed, 26 Oct 2022 23:19:03 +0100 Subject: [PATCH] refactor render function and add home page --- lyrics.go | 11 ++--------- main.go | 1 + static/script.js | 3 ++- static/style.css | 29 +++++++++++++++++++++++++++++ utils.go | 6 ++++-- views/home.tmpl | 18 ++++++++++++++++++ 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 views/home.tmpl diff --git a/lyrics.go b/lyrics.go index 9ad7447..6516057 100644 --- a/lyrics.go +++ b/lyrics.go @@ -4,9 +4,7 @@ import ( "fmt" "net/http" "net/url" - "path" "strings" - "text/template" "github.com/PuerkitoBio/goquery" "github.com/gorilla/mux" @@ -75,7 +73,7 @@ func lyricsHandler(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] if data, err := getCache(id); err == nil { - render(w, data) + render("lyrics", w, data) return } @@ -101,12 +99,7 @@ func lyricsHandler(w http.ResponseWriter, r *http.Request) { s.parse(doc) w.Header().Set("content-type", "text/html") - t, err := template.ParseFiles(path.Join("views", "lyrics.tmpl")) - if err != nil { - write(w, http.StatusInternalServerError, []byte("something went wrong")) - return - } - t.Execute(w, s) + render("lyrics", w, s) setCache(id, s) } diff --git a/main.go b/main.go index 02d0799..7adc86f 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ func main() { r.Use(securityHeaders) + r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { render("home", w, nil) }) r.HandleFunc("/{id}-lyrics", lyricsHandler) r.HandleFunc("/images/{filename}.{ext}", proxyHandler) r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) diff --git a/static/script.js b/static/script.js index eb3ab1c..1a45d35 100644 --- a/static/script.js +++ b/static/script.js @@ -14,5 +14,6 @@ document.querySelectorAll("#lyrics a").forEach(item => { function getAnnotation(e) { e.preventDefault() - console.log(e.target.parentElement.getAttribute("href")) + //const uri = e.target.parentElement.getAttribute("href") + console.log("Annotations are not yet implemented!") } diff --git a/static/style.css b/static/style.css index aca1d5e..1a447fa 100644 --- a/static/style.css +++ b/static/style.css @@ -149,3 +149,32 @@ a { display: none; } +#home { + display: flex; + flex-direction: column; + gap: 1.5rem; + justify-content: center; + align-items: center; + padding: 2rem; +} + +#home div { + text-align: center; +} + +#home h1 { + font-weight: 600; + font-size: 2.2rem; + color: #222; +} + +#home p { + color: #333; +} + +#home code { + background-color: #eee; + padding: 0.3rem 1rem; + border-radius: .5rem; + color: #333; +} diff --git a/utils.go b/utils.go index eab3cbb..168c658 100644 --- a/utils.go +++ b/utils.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "log" "net/http" "path" @@ -59,8 +60,9 @@ func securityHeaders(next http.Handler) http.Handler { }) } -func render(w http.ResponseWriter, data any) { - t, err := template.ParseFiles(path.Join("views/lyrics.tmpl")) +func render(n string, w http.ResponseWriter, data any) { + tmpl := fmt.Sprintf("%s.tmpl",n ) + t, err := template.ParseFiles(path.Join("views", tmpl)) if err != nil { w.WriteHeader(http.StatusInternalServerError) return diff --git a/views/home.tmpl b/views/home.tmpl new file mode 100644 index 0000000..679f1e5 --- /dev/null +++ b/views/home.tmpl @@ -0,0 +1,18 @@ + + + + dumb + + + + +

DUMB

+
+
+

Welcome to dumb

+

An alternative frontend for genius.com

+
+ Just redirect Genius URLs to this instance and It's all good. +
+ +