Complete project cleanup: remove unneeded files, organize static assets, update .gitignore
This commit is contained in:
@@ -4,14 +4,22 @@ import (
|
||||
"bookmann/internal/config"
|
||||
"bookmann/internal/database"
|
||||
"bookmann/internal/handlers"
|
||||
"embed"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
jwtgo "github.com/golang-jwt/jwt"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
|
||||
//go:embed static/*
|
||||
var staticFS embed.FS
|
||||
|
||||
// CustomValidator wraps the go-playground validator
|
||||
type CustomValidator struct {
|
||||
validator *validator.Validate
|
||||
@@ -52,8 +60,8 @@ func main() {
|
||||
SigningKey: []byte(cfg.JWTSecret),
|
||||
ContextKey: "user",
|
||||
SuccessHandler: func(c echo.Context) {
|
||||
token := c.Get("user").(*jwt.Token)
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
token := c.Get("user").(*jwtgo.Token)
|
||||
claims := token.Claims.(jwtgo.MapClaims)
|
||||
c.Set("user_id", claims["user_id"])
|
||||
},
|
||||
})
|
||||
@@ -65,8 +73,39 @@ func main() {
|
||||
// Routes
|
||||
handlers.SetupRoutes(protected, queries)
|
||||
|
||||
// Serve static files
|
||||
e.Static("/", "static")
|
||||
// Serve static files with SPA fallback from embedded FS
|
||||
e.GET("/*", func(c echo.Context) error {
|
||||
path := c.Request().URL.Path
|
||||
if strings.HasPrefix(path, "/api") {
|
||||
return c.String(http.StatusNotFound, "Not found")
|
||||
}
|
||||
// Try to serve the file from embedded FS
|
||||
filePath := "static" + path
|
||||
if file, err := staticFS.Open(filePath); err == nil {
|
||||
defer file.Close()
|
||||
contentType := mime.TypeByExtension(filepath.Ext(filePath))
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
return c.Stream(http.StatusOK, contentType, file)
|
||||
}
|
||||
// Try .html extension
|
||||
htmlPath := filePath + ".html"
|
||||
if file, err := staticFS.Open(htmlPath); err == nil {
|
||||
defer file.Close()
|
||||
return c.Stream(http.StatusOK, "text/html", file)
|
||||
}
|
||||
// Try /index.html
|
||||
indexPath := filePath + "/index.html"
|
||||
if file, err := staticFS.Open(indexPath); err == nil {
|
||||
defer file.Close()
|
||||
return c.Stream(http.StatusOK, "text/html", file)
|
||||
}
|
||||
// Fallback to index.html
|
||||
file, _ := staticFS.Open("static/index.html")
|
||||
defer file.Close()
|
||||
return c.Stream(http.StatusOK, "text/html", file)
|
||||
})
|
||||
|
||||
// Start server
|
||||
log.Printf("Starting server on port %s", cfg.ServerPort)
|
||||
|
||||
Reference in New Issue
Block a user