Merge branch 'main' of github.com:Theodor-Springmann-Stiftung/lenz-web

This commit is contained in:
Simon Martens
2025-03-19 21:36:26 +01:00
2 changed files with 37 additions and 13 deletions

21
server/cache.go Normal file
View File

@@ -0,0 +1,21 @@
package server
import (
"time"
"github.com/gofiber/storage/memory/v2"
)
type Cache struct {
*memory.Storage
}
const (
CACHE_GC_INTERVAL = 24 * time.Hour
)
func NewCache() Cache {
return Cache{memory.New(memory.Config{
GCInterval: CACHE_GC_INTERVAL,
})}
}

View File

@@ -8,7 +8,6 @@ import (
"github.com/gofiber/fiber/v2/middleware/cache" "github.com/gofiber/fiber/v2/middleware/cache"
"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/storage/memory/v2"
) )
const ( const (
@@ -18,8 +17,7 @@ const (
SERVER_TIMEOUT = 120 * time.Second SERVER_TIMEOUT = 120 * time.Second
// INFO: Maybe this is too long/short? // INFO: Maybe this is too long/short?
CACHE_TIME = 24 * time.Hour CACHE_TIME = 24 * time.Hour
CACHE_GC_INTERVAL = 120 * time.Second
) )
const ( const (
@@ -31,21 +29,16 @@ const (
type Server struct { type Server struct {
Engine *templating.Engine Engine *templating.Engine
Server *fiber.App Server *fiber.App
Cache *memory.Storage Cache fiber.Storage
} }
func New(engine *templating.Engine, debug bool) Server { func New(engine *templating.Engine, storage fiber.Storage, debug bool) Server {
c := memory.New(memory.Config{
GCInterval: CACHE_GC_INTERVAL,
})
server := fiber.New(fiber.Config{ server := fiber.New(fiber.Config{
AppName: "Lenz", AppName: "Lenz",
CaseSensitive: false, CaseSensitive: false,
ErrorHandler: fiber.DefaultErrorHandler, ErrorHandler: fiber.DefaultErrorHandler,
WriteTimeout: REQUEST_TIMEOUT, WriteTimeout: REQUEST_TIMEOUT,
ReadTimeout: REQUEST_TIMEOUT, ReadTimeout: REQUEST_TIMEOUT,
PassLocalsToViews: true,
Views: engine, Views: engine,
EnablePrintRoutes: debug, EnablePrintRoutes: debug,
ViewsLayout: templating.DEFAULT_LAYOUT_NAME, ViewsLayout: templating.DEFAULT_LAYOUT_NAME,
@@ -63,20 +56,30 @@ func New(engine *templating.Engine, debug bool) Server {
Next: CacheFunc, Next: CacheFunc,
Expiration: CACHE_TIME, Expiration: CACHE_TIME,
CacheControl: false, CacheControl: false,
Storage: c, Storage: storage,
})) }))
} else { } else {
server.Use(cache.New(cache.Config{ server.Use(cache.New(cache.Config{
Next: CacheFunc, Next: CacheFunc,
Expiration: CACHE_TIME, Expiration: CACHE_TIME,
CacheControl: true, CacheControl: true,
Storage: c, Storage: storage,
})) }))
} }
return Server{ return Server{
Engine: engine, Engine: engine,
Server: server, Server: server,
Cache: c, Cache: storage,
} }
} }
func (s *Server) Start(addr string) error {
s.Cache.Reset()
return s.Server.Listen(addr)
}
func (s *Server) Stop() error {
s.Cache.Close()
return s.Server.Shutdown()
}