36 lines
918 B
Go
36 lines
918 B
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"list_app/controllers"
|
|
"list_app/public"
|
|
"list_app/web"
|
|
)
|
|
|
|
func New() http.Handler {
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("GET /{$}", web.Index)
|
|
mux.HandleFunc("POST /add", web.Add)
|
|
|
|
mux.HandleFunc("GET /api", apiIndex)
|
|
mux.HandleFunc("GET /api/{$}", apiIndex)
|
|
mux.HandleFunc("GET /api/media/{mediaType}", controllers.List)
|
|
|
|
// Poster images are fetched at runtime, so they're served from a real
|
|
// directory on disk rather than the embedded static assets below.
|
|
mux.Handle("/poster/", http.StripPrefix("/poster/", http.FileServer(http.Dir(controllers.PostersDir))))
|
|
|
|
mux.Handle("/", http.FileServerFS(public.FS))
|
|
|
|
return mux
|
|
}
|
|
|
|
func apiIndex(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "API is working"})
|
|
}
|