mirror of
https://github.com/Theodor-Springmann-Stiftung/lenz-web.git
synced 2026-03-23 06:45:30 +00:00
Restart Init
This commit is contained in:
@@ -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()
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user