package server import ( "demo-server/internal/model" "demo-server/internal/server/ext" "demo-server/pkg/log" gzap "github.com/gin-contrib/zap" "github.com/gin-gonic/gin" "go.uber.org/zap" "net/http" "time" ) type handler func(s *Server) func (s *Server) init(hs ...handler) { for _, h := range hs { h(s) } } func (s *Server) Init() { s.init( InitEngine, // gin InitHttpSvr, // http InitExt, // ext InitRouter, // router ) } 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 = log.GinRouter 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) { s.router(s.engine) } func InitExt(s *Server) { ext.Init(s.engine, s.GetCfg()) } func InitHttpSvr(s *Server) { httpSvr := &http.Server{ Addr: s.cfg.Svr.Address, Handler: s.engine, ReadTimeout: 120 * time.Second, WriteTimeout: 120 * time.Second, MaxHeaderBytes: 1 << 20, } s.httpSvr = httpSvr }