viper-app/cmd/inits/common.go

64 lines
1.1 KiB
Go

package inits
import (
"fmt"
"github.com/pkg/errors"
"reflect"
"runtime"
)
var cfgPath string
func SetCfg(cfg string) {
cfgPath = cfg
}
/*
init 包内处理项目初始化流程 包括:
通用
1. Config viper 读入
2. Logger zap
业务
3. Router gin
4. DB、client...
*/
// InitFundamental APP 非业务逻辑部分初始化
//
// func call return error 程序应该停止运行
func InitFundamental() error {
return mustSuccess(initConfigWarpDefault, initLogger)
}
func InitServer() error {
return mustSuccess(
initConfigWarpDefault,
initLogger,
//initFileSystem,
//initDB,
//initDBTables,
//initService,
)
}
var (
// ErrorInitFundamental mean that initFundamental() return error then program continue running!
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
}