Make go run dev work :3
This commit is contained in:
28
main.go
28
main.go
@@ -9,19 +9,33 @@ import (
|
|||||||
"os"
|
"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 := strings.Trim(r.URL.Path, "/")
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = "home"
|
path = "home"
|
||||||
}
|
}
|
||||||
|
|
||||||
exePath, err := os.Executable()
|
|
||||||
if err != nil {
|
templateDir := getTemplateDir()
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
exeDir := filepath.Dir(exePath)
|
|
||||||
templateDir := filepath.Join(exeDir, "templates")
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(
|
tmpl, err := template.ParseFiles(
|
||||||
filepath.Join(templateDir, "layout.html"),
|
filepath.Join(templateDir, "layout.html"),
|
||||||
|
|||||||
Reference in New Issue
Block a user