-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
108 lines (98 loc) · 2.19 KB
/
main.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 main
import (
"flag"
"net/http"
"os"
"strings"
"time"
"github.com/pelletier/go-toml/v2"
"github.com/sirupsen/logrus"
"github.com/lnsp/koala/router"
"github.com/lnsp/koala/security"
"github.com/lnsp/koala/webui"
)
var version = "dev"
type Configuration struct {
Addr string
Debug bool
CORS bool
ApplyCmd string `toml:"apply_cmd"`
APIRoot string `toml:"api_root"`
Zones []struct {
Name string
Path string
Origin string
TTL int64
}
Security struct {
Mode string
OIDC struct {
ClientID string `toml:"client_id"`
IdentityServer string `toml:"identity_server"`
}
JWT struct {
Secret string
}
}
}
var (
configPath = flag.String("config", "config.toml", "path to configuration")
)
func main() {
flag.Parse()
// Read config data
configData, err := os.ReadFile(*configPath)
if err != nil {
logrus.Fatal("read config file:", err)
}
// ... and unmarshal
var s Configuration
if err := toml.Unmarshal(configData, &s); err != nil {
logrus.Fatal("unmarshal config:", err)
}
// Configure debug level
if s.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("with configuration %+v", s)
}
// Set up security guard
var guard security.Guard
switch s.Security.Mode {
case "none":
guard = security.None()
case "jwt":
guard = security.JWT(s.Security.JWT.Secret)
case "oidc":
guard = security.OIDC(s.Security.OIDC.ClientID, s.Security.OIDC.IdentityServer)
default:
logrus.Fatal("unknown security guard:", s.Security)
}
// Generate zone list
routerZones := make([]router.Zone, len(s.Zones))
for i, zone := range s.Zones {
routerZones[i] = router.Zone(zone)
}
rtr, err := router.New(router.Config{
Zones: routerZones,
ApplyCmd: strings.Split(s.ApplyCmd, " "),
CORS: s.CORS,
Security: guard,
UI: webui.FS,
APIRoot: s.APIRoot,
})
if err != nil {
logrus.Fatal("setup router:", err)
}
srv := &http.Server{
Addr: s.Addr,
Handler: rtr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
logrus.WithFields(logrus.Fields{
"version": version,
}).Info("up and running")
if err := srv.ListenAndServe(); err != nil {
logrus.WithError(err).Fatal("could not serve")
}
}