34 lines
716 B
Go
34 lines
716 B
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"path/filepath"
|
|
)
|
|
|
|
func pageHandler(w http.ResponseWriter, r *http.Request) {
|
|
path := r.URL.Path
|
|
if path == "/" {
|
|
path = "/home"
|
|
}
|
|
t, err := template.Must(template.ParseGlob("templates/*.html")).Clone()
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if _, err := t.ParseFiles(filepath.Join("pages", path+".html")); err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if err := t.ExecuteTemplate(w, "layout", nil); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/", pageHandler)
|
|
log.Println("Server running at http://localhost:14888")
|
|
log.Fatal(http.ListenAndServe(":14888", nil))
|
|
}
|