fix the stupid code

This commit is contained in:
2025-09-23 19:04:03 +03:00
parent 79769661cd
commit 050ff9060e

40
main.go
View File

@@ -4,44 +4,20 @@ import (
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"strings"
"path/filepath" "path/filepath"
"os"
) )
func getTemplateDir() string {
exePath, err := os.Executable()
if err != nil {
log.Fatal(err)
}
exeDir := filepath.Dir(exePath)
testPath := filepath.Join(exeDir, "templates")
if _, err := os.Stat(testPath); err == nil {
return testPath
}
// if running using: go run use wd
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return filepath.Join(wd, "templates")
}
func pageHandler(w http.ResponseWriter, r *http.Request) { func pageHandler(w http.ResponseWriter, r *http.Request) {
path := strings.Trim(r.URL.Path, "/") path := r.URL.Path
if path == "" { if path == "/" {
path = "home" path = "/home"
} }
templateDir := getTemplateDir()
tmpl, err := template.ParseFiles( tmpl, err := template.ParseFiles(
filepath.Join(templateDir, "layout.html"), filepath.Join("templates/layout.html"),
filepath.Join(templateDir, "header.html"), filepath.Join("templates/header.html"),
filepath.Join(templateDir, "footer.html"), filepath.Join("templates/footer.html"),
filepath.Join(templateDir, path+".html"), filepath.Join("templates/"+path+".html"),
) )
if err != nil { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)