This commit is contained in:
Simon Martens
2025-03-13 22:11:55 +01:00
parent f85dbab551
commit 534fabcb54
8 changed files with 332 additions and 162 deletions

7
server/functions.go Normal file
View File

@@ -0,0 +1,7 @@
package server
import "github.com/gofiber/fiber/v2"
func CacheFunc(c *fiber.Ctx) bool {
return c.Query("noCache") == "true" || c.Response().StatusCode() != fiber.StatusOK
}

View File

@@ -1,11 +1,21 @@
package server
import "time"
import (
"time"
"github.com/Theodor-Springmann-Stiftung/lenz-web/templating"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/storage/memory/v2"
)
const (
// INFO: This timeout is stupid. Uploads can take a long time, other routes might not. It's messy.
REQUEST_TIMEOUT = 16 * time.Second
SERVER_TIMEOUT = 16 * time.Second
// INFO: This timeout is stupid.
// Uploads can take a long time, other routes might not. It's messy.
REQUEST_TIMEOUT = 120 * time.Second
SERVER_TIMEOUT = 120 * time.Second
// INFO: Maybe this is too long/short?
CACHE_TIME = 24 * time.Hour
@@ -17,3 +27,56 @@ const (
ROUTES_FILEPATH = "./views/routes"
LAYOUT_FILEPATH = "./views/layouts"
)
type Server struct {
Engine *templating.Engine
Server *fiber.App
Cache *memory.Storage
}
func New(engine *templating.Engine, debug bool) Server {
c := memory.New(memory.Config{
GCInterval: CACHE_GC_INTERVAL,
})
server := fiber.New(fiber.Config{
AppName: "Lenz",
CaseSensitive: false,
ErrorHandler: fiber.DefaultErrorHandler,
WriteTimeout: REQUEST_TIMEOUT,
ReadTimeout: REQUEST_TIMEOUT,
PassLocalsToViews: true,
Views: engine,
EnablePrintRoutes: debug,
ViewsLayout: templating.DEFAULT_LAYOUT_NAME,
UnescapePath: true,
})
if debug {
server.Use(logger.New())
}
server.Use(recover.New())
if debug {
server.Use(cache.New(cache.Config{
Next: CacheFunc,
Expiration: CACHE_TIME,
CacheControl: false,
Storage: c,
}))
} else {
server.Use(cache.New(cache.Config{
Next: CacheFunc,
Expiration: CACHE_TIME,
CacheControl: true,
Storage: c,
}))
}
return Server{
Engine: engine,
Server: server,
Cache: c,
}
}