mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 08:37:13 +00:00
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
|
package web
|
||
|
|
||
|
import (
|
||
|
"html/template"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func RootHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
data := PageData{
|
||
|
Title: "GRAIN Relay",
|
||
|
}
|
||
|
RenderTemplate(w, data, "index.html")
|
||
|
}
|
||
|
type PageData struct {
|
||
|
Title string
|
||
|
Theme string
|
||
|
}
|
||
|
|
||
|
// Define the base directories for views and templates
|
||
|
const (
|
||
|
viewsDir = "web/views/"
|
||
|
templatesDir = "web/views/templates/"
|
||
|
)
|
||
|
|
||
|
// Define the common layout templates filenames
|
||
|
var templateFiles = []string{
|
||
|
"#layout.html",
|
||
|
"header.html",
|
||
|
"footer.html",
|
||
|
}
|
||
|
|
||
|
// Initialize the common templates with full paths
|
||
|
var layout = PrependDir(templatesDir, templateFiles)
|
||
|
|
||
|
func RenderTemplate(w http.ResponseWriter, data PageData, view string) {
|
||
|
|
||
|
// Append the specific template for the route
|
||
|
templates := append(layout, viewsDir+view)
|
||
|
|
||
|
// Parse all templates
|
||
|
tmpl, err := template.ParseFiles(templates...)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Execute the "layout" template
|
||
|
err = tmpl.ExecuteTemplate(w, "layout", data)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|