51 lines
918 B
Go
51 lines
918 B
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/pkg/errors"
|
||
|
"reflect"
|
||
|
"runtime"
|
||
|
)
|
||
|
|
||
|
/*
|
||
|
init 包内处理项目初始化流程 包括:
|
||
|
|
||
|
通用
|
||
|
1. Config viper 读入
|
||
|
2. Logger zap
|
||
|
业务
|
||
|
3. Router gin
|
||
|
4. DB、client...
|
||
|
*/
|
||
|
|
||
|
// InitFundamental APP 非业务逻辑部分初始化
|
||
|
//
|
||
|
// func call return error 程序应该停止运行
|
||
|
func InitFundamental() error {
|
||
|
return mustSuccess(InitConfig, InitLogger)
|
||
|
}
|
||
|
|
||
|
func InitServer() error {
|
||
|
return mustSuccess()
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|