viper-app/cmd/init/common.go

64 lines
1.1 KiB
Go
Raw Normal View History

package init
2023-02-03 16:18:31 +08:00
import (
"fmt"
"github.com/pkg/errors"
"reflect"
"runtime"
)
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. DBclient...
*/
// InitFundamental APP 非业务逻辑部分初始化
//
// func call return error 程序应该停止运行
func InitFundamental() error {
return mustSuccess(initConfigWarpDefault, initLogger)
2023-02-03 16:18:31 +08:00
}
func InitServer() error {
return mustSuccess(
initConfigWarpDefault,
initLogger,
//initFileSystem,
//initDB,
//initDBTables,
//initService,
)
2023-02-03 16:18:31 +08:00
}
var (
// 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
}