Templates

This commit is contained in:
2026-09-09 19:37:55 +02:00
parent 1d3cfdc7b7
commit 48b3f97298
8 changed files with 173 additions and 124 deletions
@@ -2,6 +2,7 @@ package server
import (
"database/sql"
"html/template"
"log"
"net/http"
@@ -30,7 +31,7 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
// static content
router.Static("/static", "../static")
router.StaticFile("/", "../static/index.html")
router.GET("/", home)
router.GET("/test", Test)
@@ -54,3 +55,38 @@ func Test(ctx *gin.Context) {
"message": "Test",
})
}
func home(ctx *gin.Context) {
/* load templates */
tmpl, err := template.ParseFiles(
"../templates/pages/index.html",
"../templates/layouts/navbar.html",
"../templates/layouts/properties.html",
"../templates/layouts/footer.html")
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
renderTemplate(ctx, tmpl, nil)
}
func renderTemplate(ctx *gin.Context, tmpl *template.Template, data interface{}) {
if tmpl == nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": "template is nil",
})
return
}
err := tmpl.Execute(ctx.Writer, data)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
}