tracks can be edited! + major template overhaul

This commit is contained in:
ari melody 2024-08-31 15:25:44 +01:00
parent 99b6a21179
commit 63122eb428
Signed by: ari
GPG key ID: CF99829C92678188
21 changed files with 674 additions and 221 deletions

View file

@ -12,6 +12,7 @@ import (
"arimelody.me/arimelody.me/discord"
"arimelody.me/arimelody.me/global"
musicController "arimelody.me/arimelody.me/music/controller"
musicModel "arimelody.me/arimelody.me/music/model"
)
@ -27,6 +28,19 @@ func Handler() http.Handler {
mux.Handle("/logout", MustAuthorise(LogoutHandler()))
mux.Handle("/static/", http.StripPrefix("/static", staticHandler()))
mux.Handle("/release/", MustAuthorise(http.StripPrefix("/release", serveRelease())))
mux.Handle("/track/", MustAuthorise(http.StripPrefix("/track", serveTrack())))
mux.Handle("/createtrack", MustAuthorise(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
track := musicModel.Track{ Title: "Untitled Track" }
trackID, err := musicController.CreateTrackDB(global.DB, &track)
if err != nil {
fmt.Printf("Failed to create track: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
track.ID = trackID
global.Tracks = append(global.Tracks, &track)
http.Redirect(w, r, fmt.Sprintf("/admin/track/%s", trackID), http.StatusTemporaryRedirect)
})))
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
@ -61,11 +75,16 @@ func Handler() http.Handler {
})
}
serveTemplate("index.html", IndexData{
err := pages["index"].Execute(w, IndexData{
Releases: global.Releases,
Artists: global.Artists,
Tracks: tracks,
}).ServeHTTP(w, r)
})
if err != nil {
fmt.Printf("Error executing template: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}))
return mux
@ -144,7 +163,7 @@ func LoginHandler() http.Handler {
code := r.URL.Query().Get("code")
if code == "" {
serveTemplate("login.html", loginData{DiscordURI: discord.REDIRECT_URI}).ServeHTTP(w, r)
pages["login"].Execute(w, loginData{DiscordURI: discord.REDIRECT_URI})
return
}
@ -183,7 +202,12 @@ func LoginHandler() http.Handler {
cookie.Path = "/"
http.SetCookie(w, &cookie)
serveTemplate("login.html", loginData{Token: session.Token}).ServeHTTP(w, r)
err = pages["login"].Execute(w, loginData{Token: session.Token})
if err != nil {
fmt.Printf("Error rendering admin login page: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
})
}
@ -207,78 +231,15 @@ func LogoutHandler() http.Handler {
return new_sessions
}(session.Token)
serveTemplate("logout.html", nil).ServeHTTP(w, r)
err := pages["logout"].Execute(w, nil)
if err != nil {
fmt.Printf("Error rendering admin logout page: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
})
}
func serveTemplate(page string, data any) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lp_layout := filepath.Join("admin", "views", "layout.html")
lp_prideflag := filepath.Join("views", "prideflag.html")
fp := filepath.Join("admin", "views", filepath.Clean(page))
info, err := os.Stat(fp)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
}
if info.IsDir() {
http.NotFound(w, r)
return
}
template, err := template.ParseFiles(lp_layout, lp_prideflag, fp)
if err != nil {
fmt.Printf("Error parsing template files: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = template.ExecuteTemplate(w, "layout.html", data)
if err != nil {
fmt.Printf("Error executing template: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
})
}
func serveComponent(page string, data any) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fp := filepath.Join("admin", "components", filepath.Clean(page))
info, err := os.Stat(fp)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
}
if info.IsDir() {
http.NotFound(w, r)
return
}
template, err := template.ParseFiles(fp)
if err != nil {
fmt.Printf("Error parsing template files: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = template.Execute(w, data);
if err != nil {
fmt.Printf("Error executing template: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
})
}
func staticHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
info, err := os.Stat(filepath.Join("admin", "static", filepath.Clean(r.URL.Path)))