package main import ( "html/template" "log" "net/http" "time" ) // PageData holds data to be rendered in templates type PageData struct { Title string Page string // Used by index.html to decide which template to include on full load Time string } // Global template variable var tmpl *template.Template func main() { // 1. Parse Templates // standard lib 'Must' panics if parsing fails (good for startup) tmpl = template.Must(template.ParseGlob("public/templates/*.html")) tmpl = template.Must(tmpl.ParseGlob("public/templates/*/*.html")) // 2. Setup Router (Go 1.22+ Standard Library Mux) mux := http.NewServeMux() // 3. Static Files // "GET /static/" matches any path starting with /static/ fileServer := http.FileServer(http.Dir("./public/assets")) mux.Handle("GET /public/assets/", http.StripPrefix("/public/assets/", fileServer)) // 4. Routes mux.HandleFunc("GET /", homeHandler) mux.HandleFunc("GET /explore", exploreHandler) mux.HandleFunc("GET /songon", songonHandler) mux.HandleFunc("GET /deposer-annonce", deposerAnnonceHandler) mux.HandleFunc("GET /inscription", inscriptionHandler) mux.HandleFunc("GET /contact", contactHandler) // Example POST to show HTMX form handling mux.HandleFunc("POST /contact", contactPostHandler) log.Println("Starting server on :8080") if err := http.ListenAndServe(":8080", mux); err != nil { log.Fatal(err) } } // Helper function to render pages with "If Else" logic for HTMX func render(w http.ResponseWriter, r *http.Request, pageName string, data PageData) { // Check if request is from HTMX isHtmx := r.Header.Get("HX-Request") == "true" // Ensure the Page field is set for the layout to use data.Page = pageName if isHtmx { // IF HTMX: Render only the partial (e.g., "home", "songon") // We set the content type slightly differently usually, but HTML is fine. err := tmpl.ExecuteTemplate(w, pageName, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else { // ELSE: Render the full layout ("index"), which includes the partial err := tmpl.ExecuteTemplate(w, "index", data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } // --- Handlers --- func homeHandler(w http.ResponseWriter, r *http.Request) { // Prevent catch-all "/" from matching non-existent paths if r.URL.Path != "/" { http.NotFound(w, r) return } data := PageData{ Title: "Home", Time: time.Now().Format(time.Kitchen), } render(w, r, "home", data) } func exploreHandler(w http.ResponseWriter, r *http.Request) { data := PageData{ Title: "Explore", Time: time.Now().Format(time.Kitchen), } render(w, r, "explore", data) } func songonHandler(w http.ResponseWriter, r *http.Request) { data := PageData{ Title: "Songon", Time: time.Now().Format(time.Kitchen), } render(w, r, "songon", data) } func deposerAnnonceHandler(w http.ResponseWriter, r *http.Request) { data := PageData{ Title: "Déposer", Time: time.Now().Format(time.Kitchen), } render(w, r, "deposer-annonce", data) } func inscriptionHandler(w http.ResponseWriter, r *http.Request) { data := PageData{ Title: "Inscription", Time: time.Now().Format(time.Kitchen), } render(w, r, "inscription", data) } func contactHandler(w http.ResponseWriter, r *http.Request) { data := PageData{ Title: "Contact", } render(w, r, "contact", data) } func contactPostHandler(w http.ResponseWriter, r *http.Request) { // Emulate processing time.Sleep(500 * time.Millisecond) // Return a snippet to replace the form w.Write([]byte(`

Message Sent!

Thank you for contacting us.

`)) }