package page import ( "log" "net/http" "path/filepath" "strings" ) func SetupHandlers() { http.HandleFunc("/rebuildIndex/", RebuildIndexHandler) http.Handle("/static/", StaticHandler()) http.HandleFunc("/", PageHandler) } func PageHandler(w http.ResponseWriter, r *http.Request) { u := r.URL.Path if u == "/" { u = "/index" } u = filepath.Join(".", u) log.Println(u) p := &Page{Path: u} err := p.Render(w) if err != nil { if strings.HasSuffix(err.Error(), "no such file or directory") { log.Printf("Page '%s' not found, trying 404", p.Path) p.Path = "404" w.WriteHeader(404) err := p.Render(w) if err != nil { log.Println(err) http.Error(w, "Internal server error", 500) return } return } else { log.Println(err) http.Error(w, "Internal server error", 500) return } } } func RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { pageIndex = nil p := &Page{Path: "index"} _, _ = p.Index() } return } func StaticHandler() (h http.Handler) { return http.StripPrefix("/static/", http.FileServer(http.Dir("static"))) }