-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
108 lines (88 loc) · 2.15 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 app
import (
"context"
"net"
"os"
"os/signal"
"github.com/labstack/echo/v4"
"golang.org/x/net/http2"
"github.com/reearth/reearth-cms/server/internal/usecase/gateway"
"github.com/reearth/reearth-cms/server/internal/usecase/repo"
"github.com/reearth/reearthx/account/accountusecase/accountgateway"
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
"github.com/reearth/reearthx/log"
)
func Start(debug bool, version string) {
log.Infof("reearth-cms %s", version)
ctx := context.Background()
// Load config
conf, cerr := ReadConfig(debug)
if cerr != nil {
log.Fatal(cerr)
}
log.Infof("config: %s", conf.Print())
// Init repositories
repos, gateways, acRepos, acGateways := initReposAndGateways(ctx, conf)
// Start web server
NewServer(ctx, &ServerConfig{
Config: conf,
Debug: debug,
Repos: repos,
Gateways: gateways,
AcRepos: acRepos,
AcGateways: acGateways,
}).Run(ctx)
}
type WebServer struct {
address string
appServer *echo.Echo
}
type ServerConfig struct {
Config *Config
Debug bool
Repos *repo.Container
Gateways *gateway.Container
AcRepos *accountrepo.Container
AcGateways *accountgateway.Container
}
func NewServer(ctx context.Context, cfg *ServerConfig) *WebServer {
port := cfg.Config.Port
if port == "" {
port = "8080"
}
host := cfg.Config.ServerHost
if host == "" {
if cfg.Debug {
host = "localhost"
} else {
host = "0.0.0.0"
}
}
address := host + ":" + port
w := &WebServer{
address: address,
}
w.appServer = initEcho(cfg)
return w
}
func (w *WebServer) Run(ctx context.Context) {
defer log.Infof("server: shutdown")
debugLog := ""
if w.appServer.Debug {
debugLog += " with debug mode"
}
log.Infof("server: started%s at http://%s", debugLog, w.address)
go func() {
err := w.appServer.StartH2CServer(w.address, &http2.Server{})
log.Fatalc(ctx, err.Error())
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
}
func (w *WebServer) Serve(l net.Listener) error {
return w.appServer.Server.Serve(l)
}
func (w *WebServer) Shutdown(ctx context.Context) error {
return w.appServer.Shutdown(ctx)
}