forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanzobase.go
328 lines (277 loc) · 9.05 KB
/
hanzobase.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package hanzobase
import (
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"github.com/hanzoai/backendPB/cmd"
"github.com/hanzoai/backendPB/core"
"github.com/hanzoai/backendPB/tools/hook"
"github.com/hanzoai/backendPB/tools/list"
"github.com/hanzoai/backendPB/tools/routine"
"github.com/spf13/cobra"
_ "github.com/hanzoai/backendPB/migrations"
)
var _ core.App = (*HanzoBase)(nil)
// Version of HanzoBase
var Version = "(untracked)"
// HanzoBase defines a HanzoBase app launcher.
//
// It implements [core.App] via embedding and all of the app interface methods
// could be accessed directly through the instance (eg. HanzoBase.DataDir()).
type HanzoBase struct {
core.App
devFlag bool
dataDirFlag string
encryptionEnvFlag string
queryTimeout int
hideStartBanner bool
// RootCmd is the main console command
RootCmd *cobra.Command
}
// Config is the HanzoBase initialization config struct.
type Config struct {
// hide the default console server info on app startup
HideStartBanner bool
// optional default values for the console flags
DefaultDev bool
DefaultDataDir string // if not set, it will fallback to "./hb_data"
DefaultEncryptionEnv string
DefaultQueryTimeout time.Duration // default to core.DefaultQueryTimeout (in seconds)
// optional DB configurations
DataMaxOpenConns int // default to core.DefaultDataMaxOpenConns
DataMaxIdleConns int // default to core.DefaultDataMaxIdleConns
AuxMaxOpenConns int // default to core.DefaultAuxMaxOpenConns
AuxMaxIdleConns int // default to core.DefaultAuxMaxIdleConns
DBConnect core.DBConnectFunc // default to core.dbConnect
}
// New creates a new HanzoBase instance with the default configuration.
// Use [NewWithConfig] if you want to provide a custom configuration.
//
// Note that the application will not be initialized/bootstrapped yet,
// aka. DB connections, migrations, app settings, etc. will not be accessible.
// Everything will be initialized when [HanzoBase.Start] is executed.
// If you want to initialize the application before calling [HanzoBase.Start],
// then you'll have to manually call [HanzoBase.Bootstrap].
func New() *HanzoBase {
_, isUsingGoRun := inspectRuntime()
return NewWithConfig(Config{
DefaultDev: isUsingGoRun,
})
}
// NewWithConfig creates a new HanzoBase instance with the provided config.
//
// Note that the application will not be initialized/bootstrapped yet,
// aka. DB connections, migrations, app settings, etc. will not be accessible.
// Everything will be initialized when [HanzoBase.Start] is executed.
// If you want to initialize the application before calling [HanzoBase.Start],
// then you'll have to manually call [HanzoBase.Bootstrap].
func NewWithConfig(config Config) *HanzoBase {
// initialize a default data directory based on the executable baseDir
if config.DefaultDataDir == "" {
baseDir, _ := inspectRuntime()
config.DefaultDataDir = filepath.Join(baseDir, "hb_data")
}
if config.DefaultQueryTimeout == 0 {
config.DefaultQueryTimeout = core.DefaultQueryTimeout
}
executableName := filepath.Base(os.Args[0])
hb := &HanzoBase{
RootCmd: &cobra.Command{
Use: executableName,
Short: executableName + " CLI",
Version: Version,
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
// no need to provide the default cobra completion command
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
},
devFlag: config.DefaultDev,
dataDirFlag: config.DefaultDataDir,
encryptionEnvFlag: config.DefaultEncryptionEnv,
hideStartBanner: config.HideStartBanner,
}
// replace with a colored stderr writer
hb.RootCmd.SetErr(newErrWriter())
// parse base flags
// (errors are ignored, since the full flags parsing happens on Execute())
hb.eagerParseFlags(&config)
// initialize the app instance
hb.App = core.NewBaseApp(core.BaseAppConfig{
IsDev: hb.devFlag,
DataDir: hb.dataDirFlag,
EncryptionEnv: hb.encryptionEnvFlag,
QueryTimeout: time.Duration(hb.queryTimeout) * time.Second,
DataMaxOpenConns: config.DataMaxOpenConns,
DataMaxIdleConns: config.DataMaxIdleConns,
AuxMaxOpenConns: config.AuxMaxOpenConns,
AuxMaxIdleConns: config.AuxMaxIdleConns,
DBConnect: config.DBConnect,
})
// hide the default help command (allow only `--help` flag)
hb.RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
// https://github.com/hanzoai/backendPB/issues/6136
hb.OnBootstrap().Bind(&hook.Handler[*core.BootstrapEvent]{
Id: ModerncDepsCheckHookId,
Func: func(be *core.BootstrapEvent) error {
if err := be.Next(); err != nil {
return err
}
// run separately to avoid blocking
app := be.App
routine.FireAndForget(func() {
checkModerncDeps(app)
})
return nil
},
})
return hb
}
// Start starts the application, aka. registers the default system
// commands (serve, superuser, version) and executes hb.RootCmd.
func (hb *HanzoBase) Start() error {
// register system commands
hb.RootCmd.AddCommand(cmd.NewSuperuserCommand(hb))
hb.RootCmd.AddCommand(cmd.NewServeCommand(hb, !hb.hideStartBanner))
return hb.Execute()
}
// Execute initializes the application (if not already) and executes
// the hb.RootCmd with graceful shutdown support.
//
// This method differs from hb.Start() by not registering the default
// system commands!
func (hb *HanzoBase) Execute() error {
if !hb.skipBootstrap() {
if err := hb.Bootstrap(); err != nil {
return err
}
}
done := make(chan bool, 1)
// listen for interrupt signal to gracefully shutdown the application
go func() {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)
<-sigch
done <- true
}()
// execute the root command
go func() {
// note: leave to the commands to decide whether to print their error
hb.RootCmd.Execute()
done <- true
}()
<-done
// trigger cleanups
event := new(core.TerminateEvent)
event.App = hb
return hb.OnTerminate().Trigger(event, func(e *core.TerminateEvent) error {
return e.App.ResetBootstrapState()
})
}
// eagerParseFlags parses the global app flags before calling pb.RootCmd.Execute().
// so we can have all HanzoBase flags ready for use on initialization.
func (hb *HanzoBase) eagerParseFlags(config *Config) error {
hb.RootCmd.PersistentFlags().StringVar(
&hb.dataDirFlag,
"dir",
config.DefaultDataDir,
"the HanzoBase data directory",
)
hb.RootCmd.PersistentFlags().StringVar(
&hb.encryptionEnvFlag,
"encryptionEnv",
config.DefaultEncryptionEnv,
"the env variable whose value of 32 characters will be used \nas encryption key for the app settings (default none)",
)
hb.RootCmd.PersistentFlags().BoolVar(
&hb.devFlag,
"dev",
config.DefaultDev,
"enable dev mode, aka. printing logs and sql statements to the console",
)
hb.RootCmd.PersistentFlags().IntVar(
&hb.queryTimeout,
"queryTimeout",
int(config.DefaultQueryTimeout.Seconds()),
"the default SELECT queries timeout in seconds",
)
return hb.RootCmd.ParseFlags(os.Args[1:])
}
// skipBootstrap eagerly checks if the app should skip the bootstrap process:
// - already bootstrapped
// - is unknown command
// - is the default help command
// - is the default version command
//
// https://github.com/hanzoissues/404
// https://github.com/hanzodiscussions/1267
func (hb *HanzoBase) skipBootstrap() bool {
flags := []string{
"-h",
"--help",
"-v",
"--version",
}
if hb.IsBootstrapped() {
return true // already bootstrapped
}
cmd, _, err := hb.RootCmd.Find(os.Args[1:])
if err != nil {
return true // unknown command
}
for _, arg := range os.Args {
if !list.ExistInSlice(arg, flags) {
continue
}
// ensure that there is no user defined flag with the same name/shorthand
trimmed := strings.TrimLeft(arg, "-")
if len(trimmed) > 1 && cmd.Flags().Lookup(trimmed) == nil {
return true
}
if len(trimmed) == 1 && cmd.Flags().ShorthandLookup(trimmed) == nil {
return true
}
}
return false
}
// inspectRuntime tries to find the base executable directory and how it was run.
//
// note: we are using os.Args[0] and not os.Executable() since it could
// break existing aliased binaries (eg. the community maintained homebrew package)
func inspectRuntime() (baseDir string, withGoRun bool) {
if strings.HasPrefix(os.Args[0], os.TempDir()) {
// probably ran with go run
withGoRun = true
baseDir, _ = os.Getwd()
} else {
// probably ran with go build
withGoRun = false
baseDir = filepath.Dir(os.Args[0])
}
return
}
// newErrWriter returns a red colored stderr writter.
func newErrWriter() *coloredWriter {
return &coloredWriter{
w: os.Stderr,
c: color.New(color.FgRed),
}
}
// coloredWriter is a small wrapper struct to construct a [color.Color] writter.
type coloredWriter struct {
w io.Writer
c *color.Color
}
// Write writes the p bytes using the colored writer.
func (colored *coloredWriter) Write(p []byte) (n int, err error) {
colored.c.SetWriter(colored.w)
defer colored.c.UnsetWriter(colored.w)
return colored.c.Print(string(p))
}