From a9930c02a33fe94141cd651212b5a815095c77ba Mon Sep 17 00:00:00 2001 From: Emile Date: Fri, 16 Aug 2024 22:40:26 +0200 Subject: make template path configurable --- src/battle.go | 6 +++--- src/bot.go | 6 +++--- src/http.go | 3 ++- src/main.go | 4 +++- src/user.go | 12 ++++++------ 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/battle.go b/src/battle.go index 3d418f2..4c36327 100644 --- a/src/battle.go +++ b/src/battle.go @@ -364,7 +364,7 @@ func battlesHandler(w http.ResponseWriter, r *http.Request) { data["battles"] = battles // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) @@ -427,7 +427,7 @@ func battleNewHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) @@ -628,7 +628,7 @@ func battleSingleHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) diff --git a/src/bot.go b/src/bot.go index 1a0d342..7613180 100644 --- a/src/bot.go +++ b/src/bot.go @@ -384,7 +384,7 @@ func botsHandler(w http.ResponseWriter, r *http.Request) { data["bots"] = bots // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) @@ -521,7 +521,7 @@ func botSingleHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) @@ -706,7 +706,7 @@ func botNewHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) diff --git a/src/http.go b/src/http.go index 894542a..0b7481e 100644 --- a/src/http.go +++ b/src/http.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "html/template" "net/http" ) @@ -40,7 +41,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) diff --git a/src/main.go b/src/main.go index 22e5eb7..941b58e 100644 --- a/src/main.go +++ b/src/main.go @@ -15,6 +15,7 @@ var port int var logFilePath string var databasePath string var sessiondbPath string +var templatesPath string const salt = "oogha3AiH7taimohreeH8Lexoonea5zi" @@ -32,6 +33,7 @@ func initFlags() { flag.StringVar(&logFilePath, "logfilepath", "./server.log", "The path to the log file") flag.StringVar(&databasePath, "databasepath", "./main.db", "The path to the main database") flag.StringVar(&sessiondbPath, "sessiondbpath", "./sesions.db", "The path to the session database") + flag.StringVar(&sessiondbPath, "sessiondbpath", "./templates", "The path to the templates used") } func main() { @@ -71,7 +73,7 @@ func main() { r.HandleFunc("/", indexHandler) r.HandleFunc("/login", loginHandler) r.HandleFunc("/register", registerHandler) - r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) + // r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) // endpoints with auth needed auth_needed := r.PathPrefix("/").Subrouter() diff --git a/src/user.go b/src/user.go index 04a9fd4..f841cfa 100644 --- a/src/user.go +++ b/src/user.go @@ -264,7 +264,7 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { // get the template log.Println("[d] Getting the template") - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { log.Println("Error parsing the login template: ", err) w.WriteHeader(http.StatusInternalServerError) @@ -342,7 +342,7 @@ func registerHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) @@ -469,7 +469,7 @@ func userHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) @@ -513,7 +513,7 @@ func usersHandler(w http.ResponseWriter, r *http.Request) { data["users"] = users // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) @@ -533,7 +533,7 @@ func profileHandler(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(vars["id"]) if err != nil { w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte("500 - Error reading template file")) + w.Write([]byte("500 - Error reading the profile id")) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -589,7 +589,7 @@ func profileHandler(w http.ResponseWriter, r *http.Request) { } // get the template - t, err := template.ParseGlob("./templates/*.html") + t, err := template.ParseGlob(fmt.Sprintf("%s/*.html", templatesPath)) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("500 - Error reading template file")) -- cgit 1.4.1