From 81ca75de8ea7b010203a2036fc02779da8fdadfd Mon Sep 17 00:00:00 2001 From: Ahmed AbdelHalim Date: Tue, 9 Sep 2025 00:58:37 +0200 Subject: Fix embedding the templates in the static binary --- main.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index fdecd70..cc89361 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,21 @@ package main import ( + "embed" "encoding/json" "fmt" "html/template" + "io/fs" "log" "net/http" "strings" + "wg-portal/internal" ) +//go:embed templates/* static/* +var embeddedAssets embed.FS + // APIResponse represents a standard API response structure type APIResponse struct { Success bool `json:"success"` @@ -27,8 +33,8 @@ type Server struct { // NewServer creates a new server instance func NewServer(config *internal.Config) (*Server, error) { - // Parse templates - templates, err := template.ParseFiles("templates/index.html", "templates/login.html") + // Parse embedded templates + templates, err := template.ParseFS(embeddedAssets, "templates/index.html", "templates/login.html") if err != nil { return nil, fmt.Errorf("failed to parse templates: %w", err) } @@ -45,8 +51,9 @@ func NewServer(config *internal.Config) (*Server, error) { // setupRoutes configures all HTTP routes func (s *Server) setupRoutes() { - // Serve static files (no auth required) - s.mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) + // Serve embedded static files (no auth required) + staticFS, _ := fs.Sub(embeddedAssets, "static") + s.mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) // Auth routes (no auth required) s.mux.HandleFunc("/login", s.handleLogin) -- cgit v1.2.3