forked from rookie-ninja/rk-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot.go
214 lines (173 loc) · 6.07 KB
/
boot.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
// Package rkboot is bootstrapper for rk style application
package rkboot
import (
"context"
"github.com/rookie-ninja/rk-echo/boot"
"github.com/rookie-ninja/rk-entry/entry"
rkgf "github.com/rookie-ninja/rk-gf/boot"
"github.com/rookie-ninja/rk-gin/boot"
"github.com/rookie-ninja/rk-grpc/boot"
"github.com/rookie-ninja/rk-prom"
)
// Boot is a structure for bootstrapping rk style application
type Boot struct {
BootConfigPath string `yaml:"bootConfigPath" json:"bootConfigPath"`
}
// BootOption is used as options while bootstrapping from code
type BootOption func(*Boot)
// WithBootConfigPath provide boot config yaml file.
func WithBootConfigPath(filePath string) BootOption {
return func(boot *Boot) {
boot.BootConfigPath = filePath
}
}
// NewBoot create a bootstrapper.
func NewBoot(opts ...BootOption) *Boot {
boot := &Boot{}
for i := range opts {
opts[i](boot)
}
if len(boot.BootConfigPath) < 1 {
boot.BootConfigPath = "boot.yaml"
}
// Register and bootstrap internal entries with boot config.
rkentry.RegisterInternalEntriesFromConfig(boot.BootConfigPath)
// Register external entries.
regFuncList := rkentry.ListEntryRegFunc()
for i := range regFuncList {
regFuncList[i](boot.BootConfigPath)
}
return boot
}
// Bootstrap entries in rkentry.GlobalAppCtx including bellow:
//
// Internal entries:
// 1: rkentry.AppInfoEntry
// 2: rkentry.ConfigEntry
// 3: rkentry.ZapLoggerEntry
// 4: rkentry.EventLoggerEntry
// 5: rkentry.CertEntry
//
// External entries:
// User defined entries
func (boot *Boot) Bootstrap(ctx context.Context) {
// Bootstrap external entries
for _, entry := range rkentry.GlobalAppCtx.ListEntries() {
entry.Bootstrap(ctx)
}
}
// WaitForShutdownSig wait for shutdown signal.
// 1: Call shutdown hook function added by user.
// 2: Call interrupt function of entries in rkentry.GlobalAppCtx.
func (boot *Boot) WaitForShutdownSig(ctx context.Context) {
rkentry.GlobalAppCtx.WaitForShutdownSig()
// Call shutdown hook function
for _, f := range rkentry.GlobalAppCtx.ListShutdownHooks() {
f()
}
// Call interrupt
boot.interrupt(ctx)
}
// AddShutdownHookFunc add shutdown hook function
func (boot *Boot) AddShutdownHookFunc(name string, f rkentry.ShutdownHook) {
rkentry.GlobalAppCtx.AddShutdownHook(name, f)
}
// Interrupt entries in rkentry.GlobalAppCtx including bellow:
//
// Internal entries:
// 1: rkentry.AppInfoEntry
// 2: rkentry.ConfigEntry
// 3: rkentry.ZapLoggerEntry
// 4: rkentry.EventLoggerEntry
// 5: rkentry.CertEntry
//
// External entries:
// User defined entries
func (boot *Boot) interrupt(ctx context.Context) {
// Interrupt internal entries
rkentry.GlobalAppCtx.GetAppInfoEntry().Interrupt(ctx)
boot.interruptHelper(ctx, rkentry.GlobalAppCtx.ListZapLoggerEntriesRaw())
boot.interruptHelper(ctx, rkentry.GlobalAppCtx.ListEventLoggerEntriesRaw())
boot.interruptHelper(ctx, rkentry.GlobalAppCtx.ListConfigEntriesRaw())
boot.interruptHelper(ctx, rkentry.GlobalAppCtx.ListCertEntriesRaw())
// Interrupt external entries
boot.interruptHelper(ctx, rkentry.GlobalAppCtx.ListEntries())
}
// Helper function which all interrupt() function for each entry.
func (boot *Boot) interruptHelper(ctx context.Context, m map[string]rkentry.Entry) {
for _, entry := range m {
entry.Interrupt(ctx)
}
}
// GetAppInfoEntry returns rkentry.AppInfoEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetAppInfoEntry() *rkentry.AppInfoEntry {
return rkentry.GlobalAppCtx.GetAppInfoEntry()
}
// GetZapLoggerEntry returns rkentry.ZapLoggerEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetZapLoggerEntry(name string) *rkentry.ZapLoggerEntry {
return rkentry.GlobalAppCtx.GetZapLoggerEntry(name)
}
// GetZapLoggerEntryDefault returns default rkentry.ZapLoggerEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetZapLoggerEntryDefault() *rkentry.ZapLoggerEntry {
return rkentry.GlobalAppCtx.GetZapLoggerEntryDefault()
}
// GetEventLoggerEntry returns rkentry.EventLoggerEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetEventLoggerEntry(name string) *rkentry.EventLoggerEntry {
return rkentry.GlobalAppCtx.GetEventLoggerEntry(name)
}
// GetEventLoggerEntryDefault returns default rkentry.EventLoggerEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetEventLoggerEntryDefault() *rkentry.EventLoggerEntry {
return rkentry.GlobalAppCtx.GetEventLoggerEntryDefault()
}
// GetConfigEntry returns rkentry.ConfigEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetConfigEntry(name string) *rkentry.ConfigEntry {
return rkentry.GlobalAppCtx.GetConfigEntry(name)
}
// GetCertEntry returns rkentry.CertEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetCertEntry(name string) *rkentry.CertEntry {
return rkentry.GlobalAppCtx.GetCertEntry(name)
}
// GetGinEntry returns rkgin.GinEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetGinEntry(name string) *rkgin.GinEntry {
entryRaw := rkentry.GlobalAppCtx.GetEntry(name)
if entry, ok := entryRaw.(*rkgin.GinEntry); ok {
return entry
}
return nil
}
// GetGrpcEntry returns rkgrpc.GrpcEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetGrpcEntry(name string) *rkgrpc.GrpcEntry {
entryRaw := rkentry.GlobalAppCtx.GetEntry(name)
if entry, ok := entryRaw.(*rkgrpc.GrpcEntry); ok {
return entry
}
return nil
}
// GetEchoEntry returns rkecho.EchoEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetEchoEntry(name string) *rkecho.EchoEntry {
entryRaw := rkentry.GlobalAppCtx.GetEntry(name)
if entry, ok := entryRaw.(*rkecho.EchoEntry); ok {
return entry
}
return nil
}
// GetGfEntry returns rkgf.GfEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetGfEntry(name string) *rkgf.GfEntry {
entryRaw := rkentry.GlobalAppCtx.GetEntry(name)
if entry, ok := entryRaw.(*rkgf.GfEntry); ok {
return entry
}
return nil
}
// GetPromEntry returns rkprom.PromEntry from rkentry.GlobalAppCtx.
func (boot *Boot) GetPromEntry(name string) *rkprom.PromEntry {
entryRaw := rkentry.GlobalAppCtx.GetEntry(name)
if entry, ok := entryRaw.(*rkprom.PromEntry); ok {
return entry
}
return nil
}