Restart Init

This commit is contained in:
Simon Martens
2026-02-18 13:41:44 +01:00
parent 938cdeb27b
commit 4f4288905d
2955 changed files with 4795 additions and 53375 deletions

View File

@@ -1,27 +0,0 @@
package server
import (
"time"
"github.com/gofiber/fiber/v2"
"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,
}),
}
}
func KeyGenerator(c *fiber.Ctx) string {
return c.Path() + c.Context().QueryArgs().String()
}

View File

@@ -1,15 +0,0 @@
package server
import (
"log/slog"
"strings"
"github.com/gofiber/fiber/v2"
)
func CacheFunc(c *fiber.Ctx) bool {
path := c.Path()
// INFO: for now, css and js files are excluded from caching; they get cached via ETag to enable reloading on style changes.
slog.Debug("CacheFunc:", "path", path)
return c.Query("noCache") == "true" || c.Response().StatusCode() != fiber.StatusOK || strings.HasPrefix(path, "/assets")
}

View File

@@ -1,87 +0,0 @@
package server
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"
)
const (
// 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
)
const (
STATIC_FILEPATH = "./views/assets"
ROUTES_FILEPATH = "./views/routes"
LAYOUT_FILEPATH = "./views/layouts"
)
type Server struct {
Engine *templating.Engine
Server *fiber.App
Cache fiber.Storage
}
func New(engine *templating.Engine, storage fiber.Storage, debug bool) Server {
server := fiber.New(fiber.Config{
AppName: "Lenz",
CaseSensitive: false,
ErrorHandler: fiber.DefaultErrorHandler,
WriteTimeout: REQUEST_TIMEOUT,
ReadTimeout: REQUEST_TIMEOUT,
Views: engine,
EnablePrintRoutes: debug,
ViewsLayout: templating.DEFAULT_LAYOUT_NAME,
UnescapePath: true,
// BUG: does not work rn:
PassLocalsToViews: true,
})
if debug {
server.Use(cache.New(cache.Config{
Next: CacheFunc,
Expiration: CACHE_TIME,
CacheControl: false,
Storage: storage,
KeyGenerator: KeyGenerator,
}))
server.Use(logger.New())
}
if !debug {
server.Use(cache.New(cache.Config{
Next: CacheFunc,
Expiration: CACHE_TIME,
CacheControl: false,
Storage: storage,
KeyGenerator: KeyGenerator,
}))
server.Use(recover.New())
}
return Server{
Engine: engine,
Server: server,
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()
}