From c04ab4373799b2d32f541ecb9a81da27021a6723 Mon Sep 17 00:00:00 2001 From: kisukalat Date: Sat, 30 Aug 2025 20:28:19 +0300 Subject: [PATCH] Make go run dev work :3 --- main.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 23e31b2..c15fdea 100644 --- a/main.go +++ b/main.go @@ -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"),