From 388c07dba3ca65d45036f6bef47dccf8acad6970 Mon Sep 17 00:00:00 2001 From: cangui Date: Mon, 18 Aug 2025 19:37:38 +0200 Subject: [PATCH] up --- docker-compose.yml | 5 ++--- main.go | 55 ++++++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index cc7fab1..4e8307e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,9 +18,8 @@ services: - SHELFY_DATA_DIR=/app/data # si ton code lit cette var (cf. patch précédent) volumes: - shelfy_upload:/app/upload - - shelfy_data:/app/data # <-- volume pour SQLite - - shelfy_logs:/var/log/shelfy - - ./web:/app/web:ro # <--- MONTE TON FRONT (lecture seule) + - shelfy_data:/app/data + - shelfy_logs:/var/log/shelfy # <--- MONTE TON FRONT (lecture seule) # <-- logs lus par Fail2ban labels: - traefik.http.routers.shelfy.middlewares=webdav-allow-methods@docker diff --git a/main.go b/main.go index 19f7562..601f149 100644 --- a/main.go +++ b/main.go @@ -8,11 +8,14 @@ import ( "crypto/rand" "crypto/rsa" "crypto/x509" + "embed" "encoding/pem" "fmt" "io" + "io/fs" "log" "net" + "net/http" "os" "path/filepath" "strings" @@ -232,33 +235,43 @@ func startSFTPServer(base string) { // ---------- HTTP (Gin) ---------- +//go:embed web/* web/**/* +// toutes les ressources front embarquées +var webFS embed.FS + func startHTTP() { - bd := db.InitDB() - app := gin.Default() + bd := db.InitDB() + app := gin.Default() - api := app.Group("/api/v1") - routes.AddRoutes(api, bd) - utils.CreateDefaultFolder(bd) + api := app.Group("/api/v1") + routes.AddRoutes(api, bd) + utils.CreateDefaultFolder(bd) - // Sert tes fichiers statiques sous /static - app.Static("/static", "./web") + // Sous-FS pointé sur "web" + sub, err := fs.Sub(webFS, "web") + if err != nil { + log.Fatalf("[HTTP] fs.Sub: %v", err) + } - // Sert l'index sur "/" explicitement - app.GET("/", func(c *gin.Context) { - c.File("./web/index.html") - }) + // /static -> ressources front + app.StaticFS("/static", http.FS(sub)) - // Fallback SPA : toute route non-API renvoie index.html - app.NoRoute(func(c *gin.Context) { - if strings.HasPrefix(c.Request.URL.Path, "/api/") { - c.JSON(404, gin.H{"error": "Not found"}) - return - } - c.File("./web/index.html") - }) + // "/" -> index.html embarqué + app.GET("/", func(c *gin.Context) { + c.FileFromFS("index.html", http.FS(sub)) + }) - log.Println("[HTTP] Serveur Gin sur http://0.0.0.0:8080") - _ = app.Run(":8080") + // Fallback SPA pour toutes les routes non-API + app.NoRoute(func(c *gin.Context) { + if strings.HasPrefix(c.Request.URL.Path, "/api/") { + c.JSON(404, gin.H{"error": "Not found"}) + return + } + c.FileFromFS("index.html", http.FS(sub)) + }) + + log.Println("[HTTP] Serveur Gin sur http://0.0.0.0:8080") + _ = app.Run(":8080") }