refactor render function and add home page

This commit is contained in:
rramiachraf 2022-10-26 23:19:03 +01:00
parent e7e08aad6d
commit 4b70a153aa
6 changed files with 56 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

18
views/home.tmpl Normal file
View File

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