viper-app/internal/server/init.go

54 lines
1002 B
Go

package server
import (
"demo-server/internal/model"
"github.com/gin-contrib/pprof"
gzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"runtime"
"time"
)
type handler func(s *Server)
func (s *Server) Init(hs ...handler) {
for _, h := range hs {
h(s)
}
}
func InitEngine(s *Server) {
config := s.cfg.Svr
if config.Address == "" {
config.Address = model.DefaultServerAddress
}
if !config.PprofOn {
gin.SetMode(gin.ReleaseMode)
}
eng := gin.New()
s.Engine = eng
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
zap.S().Infof("%v\t%v\t%v\t%v", httpMethod, absolutePath, handlerName, nuHandlers)
}
eng.Use(gzap.Ginzap(zap.L(), time.RFC3339, true))
eng.Use(gzap.RecoveryWithZap(zap.L(), true))
//zap.L().Info(gin.Mode())
}
func InitRouter(s *Server) {
if s.cfg.Svr.PprofOn {
runtime.SetMutexProfileFraction(1)
runtime.SetBlockProfileRate(1)
pprof.Register(s.Engine)
}
API(s)
GUI(s)
}