49 lines
995 B
Go
49 lines
995 B
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"path/filepath"
|
|
"os"
|
|
)
|
|
|
|
func pageHandler(w http.ResponseWriter, r *http.Request) {
|
|
path := strings.Trim(r.URL.Path, "/")
|
|
if path == "" {
|
|
path = "home"
|
|
}
|
|
|
|
exePath, err := os.Executable()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
exeDir := filepath.Dir(exePath)
|
|
templateDir := filepath.Join(exeDir, "templates")
|
|
|
|
tmpl, err := template.ParseFiles(
|
|
filepath.Join(templateDir, "layout.html"),
|
|
filepath.Join(templateDir, "header.html"),
|
|
filepath.Join(templateDir, "footer.html"),
|
|
filepath.Join(templateDir, path+".html"),
|
|
)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
err = tmpl.ExecuteTemplate(w, "layout", nil)
|
|
if 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))
|
|
}
|