mirror of
https://github.com/Theodor-Springmann-Stiftung/lenz-web.git
synced 2025-10-28 16:55:32 +00:00
Server
This commit is contained in:
7
server/functions.go
Normal file
7
server/functions.go
Normal 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
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user