refactor render function and add home page

main
rramiachraf 2 years ago
parent e7e08aad6d
commit 4b70a153aa

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

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

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

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

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

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>dumb</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/static/style.css" />
</head>
<body>
<h1 id="nav">DUMB</h1>
<div id="home">
<div>
<h1>Welcome to dumb</h1>
<p>An alternative frontend for genius.com</p>
</div>
<code>Just redirect Genius URLs to this instance and It's all good.</code>
</div>
</body>
</html>
Loading…
Cancel
Save