Make go run dev work :3

This commit is contained in:
2025-08-30 20:28:19 +03:00
parent d36a853f94
commit c04ab43737

28
main.go
View File

@@ -9,19 +9,33 @@ import (
"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) {
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")
templateDir := getTemplateDir()
tmpl, err := template.ParseFiles(
filepath.Join(templateDir, "layout.html"),