From 7d5c8a26aa880d7218970c3bc36a921b83679f80 Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 27 May 2024 08:28:32 +0200 Subject: [PATCH] Configure Gin to use embedded templates --- cmd/serverFunc.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 26c739d..6777b93 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -1,6 +1,7 @@ package cmd import ( + "embed" "encoding/json" "fmt" "net/http" @@ -18,13 +19,26 @@ var ( } ) +//go:embed templates/* +var templatesFolder embed.FS + func server() { //gin.SetMode(gin.ReleaseMode) - router := gin.Default() - router.GET("/api/:localidad", returnWeather) - router.GET("/api/:localidad/:dia", returnWeather) + r := gin.Default() + LoadHTMLFromEmbedFS(r, templatesFolder, "templates/*.html") + r.StaticFileFS("/style.css", "./templates/style.css", http.FS(templatesFolder)) + + r.GET("/", returnIndex) + r.GET("/api/:localidad", returnWeather) + r.GET("/api/:localidad/:dia", returnWeather) fmt.Printf("Listening on port %v...\n", listenPort) - router.Run(":" + listenPort) + r.Run(":" + listenPort) +} + +func returnIndex(c *gin.Context) { + c.HTML(http.StatusOK, "templates/index.html", gin.H{ + "title": "Hello world!", + }) } func returnWeather(c *gin.Context) {