2023-02-03 16:18:31 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-05-13 00:55:47 +08:00
|
|
|
"demo-server/internal/model"
|
2023-02-03 16:18:31 +08:00
|
|
|
"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)
|
|
|
|
}
|