-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkboot.go
More file actions
85 lines (72 loc) · 1.97 KB
/
Copy pathkboot.go
File metadata and controls
85 lines (72 loc) · 1.97 KB
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
package kboot
import (
"context"
"fmt"
"strings"
"github.com/guestin/log"
"github.com/ooopSnake/assert.go"
"github.com/spf13/viper"
"go.uber.org/zap"
)
// HideBanner hide the bootstrap banner
func HideBanner() {
_gCtx.hideBanner = true
}
func Version() string {
return __version
}
func GetContext() Context {
return _gCtx
}
func GetViper() *viper.Viper {
return GetContext().GetViper()
}
func GetActivatedProfile() string {
return GetContext().GetActivatedProfile()
}
func GetRootLogger() *zap.Logger {
return _gCtx.rootLogger
}
func GetTaggedZapLogger(tag string, opt ...log.Opt) log.ZapLog {
return GetContext().GetTaggedZapLogger(tag, opt...)
}
func GetTaggedLogger(tag string, opt ...log.Opt) log.ClassicLog {
return GetContext().GetTaggedLogger(tag, opt...)
}
func UnmarshalSubConfig(key string, i interface{}, options ...CfgOption) (err error) {
return GetContext().UnmarshalSubConfig(key, i, options...)
}
func RegisterUnit(name string, fn InitFunc, options ...UnitOption) {
assert.Must(len(strings.TrimSpace(name)) != 0, "name must not empty or blank").Panic()
assert.Must(fn != nil, "init func must not be nil").Panic()
for _, u := range _gCtx.units {
if u.GetName() == name {
assert.Must(false, fmt.Sprintf("name '%s' already exist", name)).Panic()
}
}
unit := &unitImpl{
rootCtx: _gCtx,
name: name,
initFunc: fn,
}
for _, opt := range options {
opt.apply(unit)
}
_gCtx.units = append(_gCtx.units, unit)
}
func Bootstrap(ctx context.Context, app Application, options ...BootOption) {
if !_gCtx.hideBanner {
fmt.Print(_BANNER)
}
_gCtx.logger.Info("Bootstrap ... ", zap.String("app", app.GetAppName()), zap.String("tz", app.GetTimezone().String()))
assert.Must(ctx != nil, "root ctx must not be nil").Panic()
assert.Must(app != nil, "app must not be nil").Panic()
_ctx, cancel := context.WithCancel(ctx)
_gCtx.ctx = _ctx
_gCtx.cancel = cancel
_gCtx.Application = app
for _, opt := range options {
opt.apply(_gCtx)
}
_gCtx.bootStrap()
}