optimised templates, broke tracks, improved music gateway UX. we ball

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-03-21 05:19:18 +00:00
parent 6ec813dd58
commit 18c13699af
17 changed files with 593 additions and 496 deletions

77
main.go
View file

@ -30,13 +30,12 @@ var mime_types = map[string]string{
"js": "application/javascript",
}
var templates = template.Must(template.ParseFiles(
var base_template = template.Must(template.ParseFiles(
"views/base.html",
"views/header.html",
"views/footer.html",
"views/index.html",
"views/music.html",
"views/music-gateway.html",
))
))
var htmx_template = template.Must(template.New("root").Parse(`<head>{{block "head" .}}{{end}}</head>{{block "content" .}}{{end}}`))
func log_request(req *http.Request, code int, start_time time.Time) {
now := time.Now()
@ -56,31 +55,41 @@ func log_request(req *http.Request, code int, start_time time.Time) {
)
}
func web_handler(writer http.ResponseWriter, req *http.Request) {
func handle_request(writer http.ResponseWriter, req *http.Request) {
uri := req.URL.Path
start_time := time.Now()
if req.URL.Path == "/" {
code := index_handler(writer, req)
log_request(req, code, start_time)
return
}
if uri == "/music" {
code := music_handler(writer, req)
log_request(req, code, start_time)
return
}
if strings.HasPrefix(uri, "/music/") {
code := music_gateway_handler(writer, req)
log_request(req, code, start_time)
return
}
code := static_handler(writer, req)
hx_boosted := len(req.Header["Hx-Boosted"]) > 0 && req.Header["Hx-Boosted"][0] == "true"
code := func(writer http.ResponseWriter, req *http.Request) int {
var root *template.Template
if hx_boosted {
root = template.Must(htmx_template.Clone())
} else {
root = template.Must(base_template.Clone())
}
if req.URL.Path == "/" {
return index_handler(writer, root)
}
if uri == "/music" {
return music_directory_handler(writer, root)
}
if strings.HasPrefix(uri, "/music/") {
return music_gateway_handler(writer, req, root)
}
return static_handler(writer, req)
}(writer, req)
log_request(req, code, start_time)
}
func index_handler(writer http.ResponseWriter, req *http.Request) int {
err := templates.ExecuteTemplate(writer, "index.html", nil)
func index_handler(writer http.ResponseWriter, root *template.Template) int {
index_template := template.Must(root.ParseFiles("views/index.html"))
err := index_template.Execute(writer, nil)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return 500
@ -88,8 +97,10 @@ func index_handler(writer http.ResponseWriter, req *http.Request) int {
return 200
}
func music_handler(writer http.ResponseWriter, req *http.Request) int {
err := templates.ExecuteTemplate(writer, "music.html", music.QueryAllAlbums())
func music_directory_handler(writer http.ResponseWriter, root *template.Template) int {
music_template := template.Must(root.ParseFiles("views/music.html"))
music := music.QueryAllMusic()
err := music_template.Execute(writer, music)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return 500
@ -97,7 +108,7 @@ func music_handler(writer http.ResponseWriter, req *http.Request) int {
return 200
}
func music_gateway_handler(writer http.ResponseWriter, req *http.Request) int {
func music_gateway_handler(writer http.ResponseWriter, req *http.Request, root *template.Template) int {
if len(req.URL.Path) <= len("/music/") {
http.Error(writer, "400 bad request", http.StatusBadRequest)
return 400
@ -106,12 +117,13 @@ func music_gateway_handler(writer http.ResponseWriter, req *http.Request) int {
id := req.URL.Path[len("/music/"):]
// http.Redirect(writer, req, "https://mellodoot.com/music/"+title, 302)
// return
album, ok := music.GetAlbum(id)
release, ok := music.GetRelease(id)
if !ok {
http.Error(writer, "404 not found", http.StatusNotFound)
return 404
}
err := templates.ExecuteTemplate(writer, "music-gateway.html", album)
gateway_template := template.Must(root.ParseFiles("views/music-gateway.html"))
err := gateway_template.Execute(writer, release)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return 500
@ -180,22 +192,21 @@ func parse_markdown(md []byte) []byte {
func push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later() {
db := InitDatabase()
defer db.Close()
for _, artist := range music.QueryAllArtists() {
PushArtist(db, artist)
}
for _, album := range music.QueryAllAlbums() {
for _, album := range music.QueryAllMusic() {
PushRelease(db, album)
}
defer db.Close()
}
func main() {
push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later()
http.HandleFunc("/", web_handler)
http.HandleFunc("/", handle_request)
fmt.Printf("now serving at http://127.0.0.1:%d\n", PORT)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil))