package main import ( "net/http" "os" "log" "html/template" ) type Index struct { Title string } var data = &Index { Title: "Website", } func aboutHandler(w http.ResponseWriter, req *http.Request) { templates := template.Must(template.ParseFiles( "src/index.tmpl", "src/pages/about.tmpl", "src/components/navbar.tmpl", "src/components/footer.tmpl", )) err := templates.ExecuteTemplate(w, "base", data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } func indexHandler(w http.ResponseWriter, req *http.Request) { templates := template.Must(template.ParseFiles( "src/index.tmpl", "src/pages/home.tmpl", "src/components/navbar.tmpl", "src/components/footer.tmpl", )) err := templates.ExecuteTemplate(w, "base", data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } func main() { if _, err := os.Stat("src/index.tmpl"); os.IsNotExist(err) { log.Fatal("Template file does not exist:\n", err) } fs := http.FileServer(http.Dir("./src/")) http.Handle("/src/", http.StripPrefix("/src/", fs)) port := ":3939" http.HandleFunc("/", indexHandler) http.HandleFunc("/about", aboutHandler) log.Println("Server running at http://localhost" + port) http.ListenAndServe(port, nil) }