Miglior gestione dei file statici

feat/db
Antonio De Lucreziis 2 years ago
parent 6cccf78e45
commit 63ec0d559b

@ -3,9 +3,11 @@ package server
import ( import (
"time" "time"
"git.phc.dm.unipi.it/phc/website/config"
"git.phc.dm.unipi.it/phc/website/handler" "git.phc.dm.unipi.it/phc/website/handler"
"git.phc.dm.unipi.it/phc/website/model" "git.phc.dm.unipi.it/phc/website/model"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/redirect/v2" "github.com/gofiber/redirect/v2"
@ -52,9 +54,28 @@ func routes(h handler.Service, r fiber.Router) {
}, },
})) }))
// Better static file serving setup
r.Route("/public", func(r fiber.Router) {
// Cache client side files based on checksum
r.Use(etag.New())
staticConfig := fiber.Static{}
if config.Mode == "development" {
// if no "Cache-Control" is present the browser will cache heuristically (and we don't want that)
r.Use(func(c *fiber.Ctx) error {
c.Set("Cache-Control", "no-cache")
return c.Next()
})
staticConfig.CacheDuration = -1
staticConfig.MaxAge = -1
}
r.Static("/", "./_frontend/out", staticConfig)
r.Static("/", "./_public", staticConfig)
})
// Serve generated css and js files and the static "./_public" folder // Serve generated css and js files and the static "./_public" folder
r.Static("/public/", "./_frontend/out")
r.Static("/public/", "./_public")
// Process all request and add user to the request context if there is a session cookie // Process all request and add user to the request context if there is a session cookie
r.Use(UserMiddleware(h)) r.Use(UserMiddleware(h))

Loading…
Cancel
Save