main: make handler dynamic
crispy-caesus crispy@crispy-caesus.eu
Fri, 27 Feb 2026 00:35:38 +0100
1 files changed,
11 insertions(+),
7 deletions(-)
jump to
M
cmd/server/main.go
→
cmd/server/main.go
@@ -6,18 +6,22 @@ "log"
"net/http" ) +func handler(w http.ResponseWriter, r *http.Request) { + title := r.URL.Path[len("/"):] + if title == "" { + title = "index" + } + log.Print(title) + http.ServeFile(w, r, "templates/" + title + ".html") +} + + func main() { fs := http.FileServer(http.Dir("static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "templates/index.html") - }) - - http.HandleFunc("/music", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "templates/music.html") - }) + http.HandleFunc("/", handler) fmt.Println("Running on :8080") log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))