-
Notifications
You must be signed in to change notification settings - Fork 0
/
slovo.go
81 lines (74 loc) · 2.25 KB
/
slovo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Package slovo contains code for preparing and serving web pages for the site --
the front-end.
*/
package slovo
import (
"net/http/cgi"
"github.com/kberov/slovo2/model"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)
const VERSION = "2024.04.11-alpha-015"
const CODENAME = "U+2C16 GLAGOLITIC CAPITAL LETTER UKU (Ⱆ)"
func initEcho(logger *log.Logger) *echo.Echo {
e := echo.New()
e.Debug = Cfg.Debug
e.Logger = logger
model.Logger = logger
model.DSN = Cfg.DB.DSN
CfgR := Cfg.Renderer
e.Renderer = GledkiMust(
CfgR.TemplateRoots,
CfgR.Ext,
CfgR.Tags,
CfgR.LoadFiles,
logger,
)
// Use our binder which embeds echo.DefaultBinder
e.Binder = &Binder{}
// Add middleware to the Echo instance
e.Pre(middleware.RewriteWithConfig(Cfg.Rewrite.ToRewriteRules()))
// Request ID middleware generates a unique id for a request.
e.Use(PreferDomainStaticFiles)
e.Use(middleware.RequestID())
// Add directories in which the files will be served as they are.
for _, path := range Cfg.StaticRoutes {
e.Static(path.Prefix, path.Root)
}
// TODO add Validator and other needed stugff. See
// https://echo.labstack.com/docs/customization
// e.GET("/", hello)...
loadRoutes(e)
return e
}
// Add routes, specified in DefaultConfig.Routes to echo's routes handler. See
// https://echo.labstack.com/docs/routing
func loadRoutes(e *echo.Echo) {
for _, route := range Cfg.Routes {
// find middleware and attach to the route if specified in configuration
var definedMFuncs []echo.MiddlewareFunc
for _, funcName := range route.MiddlewareFuncs {
// e.Logger.Debugf("route:%s;MiddlewareFunc: %s", route.Path, funcName)
if f, ok := middlewareFuncs[funcName]; ok {
definedMFuncs = append(definedMFuncs, f)
}
}
if route.Method == ANY {
e.Any(route.Path, handlerFuncs[route.Handler], definedMFuncs...)
continue
}
e.Add(route.Method, route.Path, handlerFuncs[route.Handler], definedMFuncs...).Name = route.Name
}
}
// StartCGI starts Echo in CGI mode.
func StartCGI(logger *log.Logger) {
if err := cgi.Serve(initEcho(logger)); err != nil {
logger.Fatal(err)
}
}
// Start starts Echo in server mode.
func Start(logger *log.Logger) {
logger.Fatal(initEcho(logger).Start(Cfg.Serve.Location))
}