diff --git a/main.go b/main.go index 544bf38..926d625 100644 --- a/main.go +++ b/main.go @@ -3,16 +3,15 @@ package main import ( "GoStart/src/api" "GoStart/src/routes" - "GoStart/src/util" + "GoStart/src/utils" "fmt" "net/http" ) - func main() { - // Load Configurations - cfg, err := util.LoadConfig() + // Load Configurations + cfg, err := utils.LoadConfig() if err != nil { fmt.Printf("Failed to load config: %v\n", err) return @@ -30,4 +29,4 @@ func main() { fmt.Printf("Server is running on http://localhost:%d\n", cfg.Port) http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), mux) -} \ No newline at end of file +} diff --git a/src/routes/example.go b/src/routes/example.go index 090389c..f04f1dd 100644 --- a/src/routes/example.go +++ b/src/routes/example.go @@ -10,5 +10,5 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) { } // Call renderTemplate with the specific template for this route - renderTemplate(w, data, "web/views/example.html") + renderTemplate(w, data, "example.html") } diff --git a/src/routes/root.go b/src/routes/root.go index 3627225..c823931 100644 --- a/src/routes/root.go +++ b/src/routes/root.go @@ -10,5 +10,5 @@ func RootHandler(w http.ResponseWriter, r *http.Request) { } // Call renderTemplate with the specific template for this route - renderTemplate(w, data, "web/views/index.html") + renderTemplate(w, data, "index.html") } diff --git a/src/routes/template.go b/src/routes/template.go index a534019..01c4bca 100644 --- a/src/routes/template.go +++ b/src/routes/template.go @@ -1,6 +1,7 @@ package routes import ( + "GoStart/src/utils" "html/template" "net/http" ) @@ -9,16 +10,26 @@ type PageData struct { Title string } +// Define the base directories for views and templates +const ( + viewsDir = "web/views/" + templatesDir = "web/views/templates/" +) + +// Define the common layout templates filenames +var layoutFiles = []string{ + "#layout.html", + "header.html", + "footer.html", +} + +// Initialize the common templates with full paths +var layout = utils.PrependDir(templatesDir, layoutFiles) + func renderTemplate(w http.ResponseWriter, data PageData, view string) { - // List of common templates - layout := []string{ - "web/views/template/#layout.html", - "web/views/template/#header.html", - "web/views/template/#footer.html", - } // Append the specific template for the route - templates := append(layout, view) + templates := append(layout, viewsDir+view) // Parse all templates tmpl, err := template.ParseFiles(templates...) diff --git a/src/util/config.go b/src/utils/config.go similarity index 88% rename from src/util/config.go rename to src/utils/config.go index 0ed29b0..ebdc13b 100644 --- a/src/util/config.go +++ b/src/utils/config.go @@ -1,4 +1,4 @@ -package util +package utils import ( "encoding/json" @@ -8,7 +8,7 @@ import ( type Config struct { Port int `json:"port"` - Development string `json:"development"` + Development string `json:"development"` } func LoadConfig() (*Config, error) { @@ -25,4 +25,4 @@ func LoadConfig() (*Config, error) { } return &config, nil -} \ No newline at end of file +} diff --git a/src/utils/helpers.go b/src/utils/helpers.go new file mode 100644 index 0000000..193a6d9 --- /dev/null +++ b/src/utils/helpers.go @@ -0,0 +1,10 @@ +package utils + +// Helper function to prepend a directory path to a list of filenames +func PrependDir(dir string, files []string) []string { + var fullPaths []string + for _, file := range files { + fullPaths = append(fullPaths, dir+file) + } + return fullPaths +} diff --git a/web/views/template/#layout.html b/web/views/templates/#layout.html similarity index 100% rename from web/views/template/#layout.html rename to web/views/templates/#layout.html diff --git a/web/views/template/#footer.html b/web/views/templates/footer.html similarity index 100% rename from web/views/template/#footer.html rename to web/views/templates/footer.html diff --git a/web/views/template/#header.html b/web/views/templates/header.html similarity index 100% rename from web/views/template/#header.html rename to web/views/templates/header.html