This commit is contained in:
cangui 2025-08-18 19:37:38 +02:00
parent 468e0bd888
commit 388c07dba3
2 changed files with 36 additions and 24 deletions

View File

@ -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

55
main.go
View File

@ -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")
}