-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
93 lines (85 loc) · 1.75 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
package main
import (
"github.com/baetyl/baetyl-core/config"
"github.com/baetyl/baetyl-core/engine"
"github.com/baetyl/baetyl-core/node"
"github.com/baetyl/baetyl-core/store"
"github.com/baetyl/baetyl-core/sync"
"github.com/baetyl/baetyl-go/context"
"github.com/baetyl/baetyl-go/http"
routing "github.com/qiangxue/fasthttp-routing"
bh "github.com/timshannon/bolthold"
"github.com/valyala/fasthttp"
)
type core struct {
cfg config.Config
sto *bh.Store
sha *node.Node
eng *engine.Engine
syn *sync.Sync
svr *http.Server
}
// NewCore creats a new core
func NewCore(ctx context.Context) (*core, error) {
var cfg config.Config
err := ctx.LoadCustomConfig(&cfg)
if err != nil {
return nil, err
}
c := &core{}
c.sto, err = store.NewBoltHold(cfg.Store.Path)
if err != nil {
return nil, err
}
c.sha, err = node.NewNode(c.sto)
if err != nil {
c.Close()
return nil, err
}
c.eng, err = engine.NewEngine(cfg.Engine, c.sto, c.sha)
if err != nil {
c.Close()
return nil, err
}
c.eng.Start()
c.syn, err = sync.NewSync(cfg.Sync, c.sto, c.sha)
if err != nil {
c.Close()
return nil, err
}
c.syn.Start()
c.svr = http.NewServer(cfg.Server, c.initRouter())
c.svr.Start()
return c, nil
}
func (c *core) Close() {
if c.svr != nil {
c.svr.Close()
}
if c.eng != nil {
c.eng.Close()
}
if c.sto != nil {
c.sto.Close()
}
if c.syn != nil {
c.syn.Close()
}
}
func (c *core) initRouter() fasthttp.RequestHandler {
router := routing.New()
router.Get("/node/status", c.sha.GetStatus)
router.Get("/services/<service>/log", c.eng.GetServiceLog)
return router.HandleRequest
}
func main() {
context.Run(func(ctx context.Context) error {
c, err := NewCore(ctx)
if err != nil {
return err
}
defer c.Close()
ctx.Wait()
return nil
})
}