refactor: organize routing a bit

This commit is contained in:
rramiachraf 2024-06-11 20:42:33 +01:00
parent c86b076188
commit 3bf802c9c2
3 changed files with 30 additions and 9 deletions

View File

@ -11,26 +11,47 @@ import (
"github.com/rramiachraf/dumb/views" "github.com/rramiachraf/dumb/views"
) )
type route struct {
Path string
Handler func(*utils.Logger) http.HandlerFunc
Method string
}
func New(logger *utils.Logger, staticFiles static) *mux.Router { func New(logger *utils.Logger, staticFiles static) *mux.Router {
r := mux.NewRouter() r := mux.NewRouter()
r.Use(utils.MustHeaders) r.Use(utils.MustHeaders)
r.Use(gorillaHandlers.CompressHandler) r.Use(gorillaHandlers.CompressHandler)
routes := []route{
{Path: "/albums/{artist}/{albumName}", Handler: album},
{Path: "/artists/{artist}", Handler: artist},
{Path: "/images/{filename}.{ext}", Handler: imageProxy},
{Path: "/search", Handler: search},
{Path: "/{annotation-id}/{artist-song}/{verse}/annotations", Handler: annotations},
{Path: "/instances.json", Handler: instances},
}
for _, rr := range routes {
method := rr.Method
if method == "" {
method = http.MethodGet
}
r.HandleFunc(rr.Path, rr.Handler(logger)).Methods(method)
}
r.PathPrefix("/static/").HandlerFunc(staticAssets(logger, staticFiles))
r.Handle("/", templ.Handler(views.HomePage())) r.Handle("/", templ.Handler(views.HomePage()))
r.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("User-agent: *\nDisallow: /\n")) w.Write([]byte("User-agent: *\nDisallow: /\n"))
}) })
r.HandleFunc("/albums/{artist}/{albumName}", album(logger)).Methods("GET")
r.HandleFunc("/artists/{artist}", artist(logger)).Methods("GET")
r.HandleFunc("/images/{filename}.{ext}", imageProxy(logger)).Methods("GET")
r.HandleFunc("/search", search(logger)).Methods("GET")
r.HandleFunc("/{annotation-id}/{artist-song}/{verse}/annotations", annotations(logger)).Methods("GET")
r.HandleFunc("/instances.json", instances(logger)).Methods("GET")
r.PathPrefix("/static/").HandlerFunc(staticAssets(logger, staticFiles))
r.PathPrefix("/{annotation-id}/{artist-song}-lyrics").HandlerFunc(lyrics(logger)).Methods("GET") r.PathPrefix("/{annotation-id}/{artist-song}-lyrics").HandlerFunc(lyrics(logger)).Methods("GET")
r.PathPrefix("/{annotation-id}/{artist-song}").HandlerFunc(lyrics(logger)).Methods("GET") r.PathPrefix("/{annotation-id}/{artist-song}").HandlerFunc(lyrics(logger)).Methods("GET")
r.PathPrefix("/{annotation-id}").HandlerFunc(lyrics(logger)).Methods("GET") r.PathPrefix("/{annotation-id}").HandlerFunc(lyrics(logger)).Methods("GET")
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
views.ErrorPage(404, "page not found").Render(context.Background(), w) views.ErrorPage(404, "page not found").Render(context.Background(), w)

View File

@ -6,7 +6,7 @@ import (
) )
templ AlbumPage(a data.Album) { templ AlbumPage(a data.Album) {
@layout(fmt.Sprintf("%s - %s", a.Artist, a.Name)) { @layout(fmt.Sprintf("%s - %s", a.Artist.Name, a.Name)) {
<div id="container" class="trio-split"> <div id="container" class="trio-split">
<div id="metadata"> <div id="metadata">
<img id="album-artwork" src={ data.ExtractImageURL(a.Image) } alt="Album image"/> <img id="album-artwork" src={ data.ExtractImageURL(a.Image) } alt="Album image"/>

View File

@ -35,7 +35,7 @@ templ ArtistPage(a data.Artist) {
<img <img
id="artwork-preview" id="artwork-preview"
src={ data.ExtractImageURL(album.Image) } src={ data.ExtractImageURL(album.Image) }
alt="Artist image" alt="Album image"
/> />
<p>{ album.Name }</p> <p>{ album.Name }</p>
</a> </a>