server.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package app
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "strconv"
  9. "syscall"
  10. "time"
  11. "git.linuxforward.com/byop/byop-engine/auth"
  12. "git.linuxforward.com/byop/byop-engine/config"
  13. "git.linuxforward.com/byop/byop-engine/dbmanager"
  14. "git.linuxforward.com/byop/byop-engine/dbstore"
  15. "git.linuxforward.com/byop/byop-engine/handlers"
  16. mw "git.linuxforward.com/byop/byop-engine/middleware"
  17. "git.linuxforward.com/byop/byop-engine/services"
  18. "github.com/gin-gonic/gin"
  19. "github.com/pkg/errors"
  20. "github.com/sirupsen/logrus"
  21. )
  22. type App struct {
  23. entry *logrus.Entry
  24. cnf *config.Config
  25. ctx context.Context
  26. cancelFunc context.CancelFunc
  27. rtr *gin.Engine
  28. // Database
  29. dbManager dbmanager.DbManager
  30. // Services
  31. authService auth.Service
  32. tokenStore auth.TokenStore
  33. // Modules
  34. authHandler *handlers.AuthHandler
  35. userModule *UserModule
  36. clientModule *ClientModule
  37. // Resource Handlers
  38. providerHandler *handlers.ProviderHandler
  39. deploymentHandler *handlers.DeploymentHandler
  40. templateHandler *handlers.TemplateHandler
  41. ticketHandler *handlers.TicketHandler
  42. monitoringHandler *handlers.MonitoringHandler
  43. }
  44. type UserModule struct {
  45. Store *dbstore.UserStore
  46. Service *services.UserService
  47. Handler *handlers.UserHandler
  48. }
  49. type ClientModule struct {
  50. Store *dbstore.ClientStore
  51. Service *services.ClientService
  52. Handler *handlers.ClientHandler
  53. }
  54. func NewApp(cnf *config.Config) (*App, error) {
  55. ctx, cancelFunc := context.WithCancel(context.Background())
  56. app := &App{
  57. entry: logrus.WithField("component", "app"),
  58. cnf: cnf,
  59. ctx: ctx,
  60. cancelFunc: cancelFunc,
  61. }
  62. // Initialize router first
  63. if cnf.Debug {
  64. gin.SetMode(gin.DebugMode)
  65. } else {
  66. // Set gin to release mode for production
  67. // This will disable debug logs and enable performance optimizations
  68. gin.SetMode(gin.ReleaseMode)
  69. }
  70. app.rtr = gin.New()
  71. app.rtr.Use(gin.Recovery())
  72. app.rtr.Use(mw.Logger)
  73. // Initialize services and handlers
  74. if err := app.initCommonServices(); err != nil {
  75. return nil, errors.Wrap(err, "initialize services")
  76. }
  77. if err := app.initHandlers(); err != nil {
  78. return nil, errors.Wrap(err, "initialize handlers")
  79. }
  80. // Set up routes after all handlers are initialized
  81. app.setupRoutes()
  82. return app, nil
  83. }
  84. func (a *App) Run() error {
  85. srv := &http.Server{
  86. Addr: fmt.Sprintf(":%s", strconv.Itoa(a.cnf.Server.Port)),
  87. Handler: a.rtr,
  88. }
  89. go func() {
  90. a.entry.WithField("address", srv.Addr).Info("Starting server on port " + strconv.Itoa(a.cnf.Server.Port))
  91. // Handle TLS if configured
  92. if a.cnf.Server.Tls.Enabled {
  93. a.entry.Info("Starting server with TLS...")
  94. err := srv.ListenAndServeTLS(a.cnf.Server.Tls.CertFile, a.cnf.Server.Tls.KeyFile)
  95. if err != nil && err != http.ErrServerClosed {
  96. a.entry.WithError(err).Fatal("Failed to start server")
  97. }
  98. } else {
  99. err := srv.ListenAndServe()
  100. if err != nil && err != http.ErrServerClosed {
  101. a.entry.WithError(err).Fatal("Failed to start server")
  102. }
  103. }
  104. a.entry.Info("Server stopped")
  105. }()
  106. quit := make(chan os.Signal, 1)
  107. signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
  108. <-quit
  109. a.entry.Info("Stopping server...")
  110. ctxTimeout, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
  111. defer cancelFunc()
  112. err := srv.Shutdown(ctxTimeout)
  113. if err != nil {
  114. return fmt.Errorf("shutdown server: %w", err)
  115. }
  116. a.entry.Info("Server stopped successfully")
  117. return nil
  118. }