summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmed AbdelHalim <[email protected]>2025-09-09 00:58:37 +0200
committerAhmed AbdelHalim <[email protected]>2025-09-09 00:58:37 +0200
commit81ca75de8ea7b010203a2036fc02779da8fdadfd (patch)
tree4e8717d46e1c775b521cd811ee9b547de1105324
parent85cce5965dceada18bd962a690eeab2647d72f3d (diff)
Fix embedding the templates in the static binary
-rw-r--r--main.go15
1 files 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)