package server import ( "fmt" "net" "github.com/gin-gonic/gin" "github.com/rs/zerolog" "github.com/spf13/viper" "kumoly.io/kumoly/app/history" "kumoly.io/kumoly/app/util" ) type SkipLogFunc func(c *gin.Context) bool type Service struct { Name string Server *gin.Engine API *gin.RouterGroup SkipLog SkipLogFunc Addr string Allow *net.IPNet l zerolog.Logger } var Server *gin.Engine var API *gin.RouterGroup func New(name string) *Service { if viper.GetBool("prod") { gin.SetMode(gin.ReleaseMode) } srv := &Service{ Name: name, Server: gin.New(), SkipLog: DefaultSkipLog, Addr: fmt.Sprintf("%s:%s", viper.GetString("server.host"), viper.GetString("server.port")), } srv.Server.Use(srv.Default) Server = srv.Server srv.API = srv.Server.Group("/api") API = srv.API if ipnetstr := viper.GetString("server.allow"); ipnetstr != "" { _, ipnet, err := net.ParseCIDR(ipnetstr) if err != nil { util.Klog.Panic().Str("service", "server.Service").Str("name", name).Err(err).Msg("ParseCIDR error") } srv.Allow = ipnet } return srv } func (srv *Service) Init() error { srv.l = util.Klog.With().Str("service", "server.Service").Str("name", srv.Name).Logger() return nil } func (srv *Service) Load() error { return nil } func (srv *Service) Main() error { srv.l.Info().Msgf("Server started on %s", srv.Addr) go func() { err := srv.Server.Run(srv.Addr) if err != nil { history.Send(history.Error().Nm("ServerStartError").Msg(err.Error())) srv.l.Panic().Err(err).Msg("Server.Run error") } }() return nil } func (srv *Service) Del() { } func (srv Service) Health() error { return nil } func (srv Service) GetName() string { return "server.Service" } func (srv Service) GetDependencies() []string { return nil } func (srv Service) IsService() bool { return true }