-
Notifications
You must be signed in to change notification settings - Fork 1
/
kid.go
148 lines (128 loc) · 3.4 KB
/
kid.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package kid
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/leor-w/kid/config"
"github.com/leor-w/kid/container"
"github.com/leor-w/kid/logger"
"github.com/leor-w/kid/middleware"
"github.com/leor-w/kid/plugin"
"github.com/leor-w/kid/plugin/logrus"
)
var (
loadConfigFlag = 0x1
loadLoggerFlag = 0x2
)
const CommandLogo = `
██╗ ██╗ ██╗ ███████╗
██║ ██╔╝ ██║ ██╔═══██╗
█████╔╝ ██║ ██║ ██║
██╔═██╗ ██║ ██║ ██║
██║ ██╗ ██║ ███████╔╝
╚═╝ ╚═╝ ╚═╝ ╚══════╝`
type Kid struct {
loadFlag int
*gin.Engine
RouterGroup
Logger logger.Logger
IocContainer *container.Container
Options *Options
}
var _defaultKid *Kid
type Option func(*Options)
func Registry(plugin plugin.Plugin, opts ...container.Option) *Kid {
return _defaultKid.Registry(plugin, opts...)
}
// Registry 将插件注入容器
func (kid *Kid) Registry(plugin plugin.Plugin, opts ...container.Option) *Kid {
if err := kid.IocContainer.Provide(plugin, opts...); err != nil {
logger.Fatal(err.Error())
}
return kid
}
func (kid *Kid) Get(plugin plugin.Plugin, name ...string) (interface{}, error) {
return kid.IocContainer.Get(plugin, name...)
}
func (kid *Kid) PopulateOne(plugin plugin.Plugin, opts ...container.Option) error {
complete, err := kid.IocContainer.PopulateSingle(plugin, opts...)
if err != nil || !complete {
return fmt.Errorf("PopulateOne: 失败: %w", err)
}
return nil
}
func (kid *Kid) Populate() error {
if kid.loadFlag < loadLoggerFlag|loadConfigFlag {
panic("kid.Launch: failed: you must be call loadLogger or loadConfig")
}
return kid.IocContainer.Populate()
}
func (kid *Kid) Init() error {
if err := kid.CommandLine(); err != nil {
return err
}
// 读取配置文件
if err := kid.loadConfig(); err != nil {
return err
}
if err := kid.loadLogger(); err != nil {
return err
}
runMode := config.GetString(kid.Options.RunMode)
if len(runMode) == 0 {
runMode = gin.TestMode
}
gin.SetMode(runMode)
kid.Engine = gin.New()
kid.Engine.Use(middleware.Logger())
kid.Engine.Use(middleware.Recovery)
kid.RouterGroup = RouterGroup{&(kid.Engine.RouterGroup)}
return nil
}
func (kid *Kid) loadConfig() error {
_ = config.New(
config.WithProviders(kid.Options.Configs),
config.WithDefault(true),
)
kid.loadFlag |= loadConfigFlag
return nil
}
func (kid *Kid) loadLogger() error {
logger.Init(logrus.Default(""))
kid.loadFlag |= loadLoggerFlag
logger.Infof("日志服务准备就绪...")
return nil
}
func (kid *Kid) Launch(hosts ...string) {
host := ":8080"
if len(hosts) > 0 {
host = hosts[0]
}
PrintLogo()
logger.Infof("HTTP 服务已启动: %s", host)
if err := kid.Run(host); err != nil {
logger.Error(err.Error())
}
}
func (kid *Kid) NoRoute(handleFunc HandleFunc) {
kid.Engine.NoRoute(convertHandleFunc(handleFunc))
}
func (kid *Kid) NoMethod(handleFunc HandleFunc) {
kid.Engine.NoMethod(convertHandleFunc(handleFunc))
}
func New(opts ...Option) *Kid {
opt := &Options{}
for _, o := range opts {
o(opt)
}
_defaultKid = &Kid{
IocContainer: container.New(),
Options: opt,
}
if err := _defaultKid.Init(); err != nil {
panic(err.Error())
}
return _defaultKid
}
func PrintLogo() {
logger.Info(CommandLogo)
}