2023-05-13 00:55:47 +08:00
|
|
|
package init
|
2023-02-03 16:18:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2023-05-13 00:55:47 +08:00
|
|
|
var cfgPath string
|
|
|
|
|
|
|
|
func SetCfg(cfg string) {
|
|
|
|
cfgPath = cfg
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:18:31 +08:00
|
|
|
/*
|
|
|
|
init 包内处理项目初始化流程 包括:
|
|
|
|
|
|
|
|
通用
|
|
|
|
1. Config viper 读入
|
|
|
|
2. Logger zap
|
|
|
|
业务
|
|
|
|
3. Router gin
|
|
|
|
4. DB、client...
|
|
|
|
*/
|
|
|
|
|
|
|
|
// InitFundamental APP 非业务逻辑部分初始化
|
|
|
|
//
|
|
|
|
// func call return error 程序应该停止运行
|
|
|
|
func InitFundamental() error {
|
2023-05-13 00:55:47 +08:00
|
|
|
return mustSuccess(initConfigWarpDefault, initLogger)
|
2023-02-03 16:18:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitServer() error {
|
2023-05-13 00:55:47 +08:00
|
|
|
return mustSuccess(
|
|
|
|
initConfigWarpDefault,
|
|
|
|
initLogger,
|
|
|
|
//initFileSystem,
|
|
|
|
//initDB,
|
|
|
|
//initDBTables,
|
|
|
|
//initService,
|
|
|
|
)
|
2023-02-03 16:18:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-05-13 00:55:47 +08:00
|
|
|
// ErrorInitFundamental mean that initFundamental() return error then program continue running!
|
2023-02-03 16:18:31 +08:00
|
|
|
ErrorInitFundamental = errors.New("app fundamental init error! ")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Func func() error
|
|
|
|
|
|
|
|
func mustSuccess(fn ...Func) (err error) {
|
|
|
|
for _, f := range fn {
|
|
|
|
err = f()
|
|
|
|
if err != nil {
|
|
|
|
ptr := reflect.ValueOf(f).Pointer()
|
|
|
|
fi := runtime.FuncForPC(ptr)
|
|
|
|
err = fmt.Errorf("%s error! \ncause: %w", fi.Name(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|