Update template
This commit is contained in:
34
internal/handlers/root.go
Normal file
34
internal/handlers/root.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"git.clustercockpit.org/moebiusband/go-http-skeleton/web"
|
||||
)
|
||||
|
||||
type PageData struct {
|
||||
Title string
|
||||
}
|
||||
|
||||
func RootHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
file, err := web.Templates.ReadFile("templates/index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
slog.Error("Error reading template", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.New("index.html").Parse(string(file)))
|
||||
|
||||
data := PageData{
|
||||
Title: "Home",
|
||||
}
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
slog.Error("Error executing template", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user