/
server.go
108 lines (93 loc) · 2.29 KB
/
server.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package server
import (
"errors"
"fmt"
"github.com/labstack/echo/v4"
emw "github.com/labstack/echo/v4/middleware"
"github.com/myOmikron/echotools/color"
"github.com/myOmikron/echotools/execution"
mw "github.com/myOmikron/echotools/middleware"
"github.com/myOmikron/echotools/worker"
"github.com/pelletier/go-toml"
"github.com/pnp-zone/common/conf"
"html/template"
"io/fs"
"io/ioutil"
"os"
"time"
)
func StartServer(configPath string) {
config := &conf.Config{}
if configBytes, err := ioutil.ReadFile(configPath); errors.Is(err, fs.ErrNotExist) {
color.Printf(color.RED, "Config was not found at %s\n", configPath)
b, _ := toml.Marshal(config)
fmt.Print(string(b))
os.Exit(1)
} else {
if err := toml.Unmarshal(configBytes, config); err != nil {
panic(err)
}
}
// Check for valid config values
if err := config.CheckConfig(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
// Plugin definition
plugins := loadPlugins(config)
// Database
db := initializeDatabase(config, plugins)
// Worker pool
wp := worker.NewPool(&worker.PoolConfig{
NumWorker: 10,
QueueSize: 100,
})
wp.Start()
for _, plugin := range plugins {
if err := plugin.WorkerPoolHook(wp); err != nil {
return
}
}
// Web server
e := echo.New()
e.HideBanner = true
// Template rendering
renderer := &TemplateRenderer{
templates: template.Must(template.ParseGlob("templates/*.gohtml")),
}
e.Renderer = renderer
// Middleware definition
e.Use(emw.Logger())
e.Use(emw.Recover())
allowedHosts := make([]mw.AllowedHost, 0)
for _, host := range config.Server.AllowedHosts {
allowedHosts = append(allowedHosts, mw.AllowedHost{
Host: host.Host,
Https: host.Https,
})
}
secConfig := &mw.SecurityConfig{
AllowedHosts: allowedHosts,
UseForwardedProtoHeader: config.Server.UseForwardedProtoHeader,
}
e.Use(mw.Security(secConfig))
cookieAge := time.Hour * 24
e.Use(mw.Session(db, &mw.SessionConfig{
CookieName: "sessionid",
CookieAge: &cookieAge,
CookiePath: "/",
DisableLogging: false,
}))
// Router
defineRoutes(e, db, config, plugins)
// Start server
execution.SignalStart(e, config.Server.ListenAddress, &execution.Config{
ReloadFunc: func() {
StartServer(configPath)
},
StopFunc: func() {
},
TerminateFunc: func() {
},
})
}