reCoreD-UI/server/server.go

49 lines
858 B
Go
Raw Normal View History

2024-04-03 09:05:12 +00:00
package server
import (
"net"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/database"
2024-04-03 09:05:12 +00:00
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
type Server struct {
2024-04-09 08:28:18 +00:00
webServer *gin.Engine
listen string
prefix string
2024-04-13 02:30:02 +00:00
debug bool
2024-04-03 09:05:12 +00:00
}
func NewServer(c *cli.Context) (*Server, error) {
2024-04-09 08:28:18 +00:00
if err := database.Connect(c.String("mysql-dsn")); err != nil {
2024-04-03 09:05:12 +00:00
return nil, err
}
2024-04-11 03:41:33 +00:00
if c.Bool("debug") {
database.Client = database.Client.Debug()
2024-04-19 01:44:37 +00:00
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
2024-04-11 03:41:33 +00:00
}
2024-04-03 09:05:12 +00:00
return &Server{
2024-04-09 08:28:18 +00:00
webServer: gin.New(),
2024-04-03 09:05:12 +00:00
listen: net.JoinHostPort(
c.String("listen"),
c.String("port"),
),
prefix: c.String("prefix"),
2024-04-13 02:30:02 +00:00
debug: c.Bool("debug"),
2024-04-03 09:05:12 +00:00
}, nil
}
func (s *Server) Run() error {
logrus.Debug("server running")
defer logrus.Debug("server exit")
s.setupRoute()
return s.webServer.Run(s.listen)
}