From 0da968fd5a2d1b244ee1fe747312e47e9e35a7eb Mon Sep 17 00:00:00 2001 From: StarFleetCPTN Date: Sat, 8 Mar 2025 13:17:33 -0800 Subject: [PATCH] feat: Implement embedded static file serving - Use Go's embed.FS to serve static files directly from binary - Remove explicit static directory creation - Update static file serving method to use embedded filesystem - Improve static file serving configuration with error handling --- main.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 371b0bc..43a64f0 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,11 @@ package main import ( + "embed" "fmt" + "io/fs" "log" + "net/http" "os" "path/filepath" "time" @@ -16,6 +19,9 @@ import ( "golang.org/x/crypto/bcrypt" ) +//go:embed static +var staticFiles embed.FS + func main() { // Set Gin to release mode gin.SetMode(gin.ReleaseMode) @@ -34,10 +40,6 @@ func main() { dirs := []string{ cfg.DataDir, cfg.BackupDir, - // "templates", - "static", - "static/css", - "static/js", } for _, dir := range dirs { @@ -101,9 +103,13 @@ func main() { })) router.Use(gin.Recovery()) - // Serve static files - router.Static("/static", "./static") - log.Printf("Static file serving configured") + // Serve embedded static files + staticFS, err := fs.Sub(staticFiles, "static") + if err != nil { + log.Fatalf("Failed to create sub-filesystem: %v", err) + } + router.StaticFS("/static", http.FS(staticFS)) + log.Printf("Embedded static files configured for serving") // Initialize web handlers webHandler, err := web.NewHandler(database, scheduler, cfg.JWTSecret, dbPath, cfg.BackupDir, cfg)