server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package app
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "sync"
  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. componentModule *ComponentModule // Renamed from appModule
  38. appModule *AppModule // Renamed from templateModule
  39. deploymentModule *DeploymentModule
  40. // Resource Handlers
  41. providerHandler *handlers.ProviderHandler
  42. // ticketHandler *handlers.TicketHandler
  43. stopped bool
  44. wg sync.WaitGroup
  45. // monitoringHandler *handlers.MonitoringHandler
  46. }
  47. type UserModule struct {
  48. Store *dbstore.UserStore
  49. Service *services.UserService
  50. Handler *handlers.UserHandler
  51. }
  52. type ClientModule struct {
  53. Store *dbstore.ClientStore
  54. Service *services.ClientService
  55. Handler *handlers.ClientHandler
  56. }
  57. type ComponentModule struct {
  58. Store *dbstore.ComponentStore // Renamed from AppStore
  59. Service *services.ComponentService // Renamed from AppService
  60. Handler *handlers.ComponentHandler // Renamed from AppHandler
  61. }
  62. type AppModule struct {
  63. Store *dbstore.AppStore // Renamed from TemplateStore
  64. Service *services.AppService // Renamed from TemplateService
  65. Handler *handlers.AppsHandler // Renamed from TemplateHandler
  66. }
  67. type DeploymentModule struct {
  68. Store *dbstore.DeploymentStore
  69. Service *services.DeploymentService
  70. Handler *handlers.DeploymentHandler
  71. }
  72. func NewApp(cnf *config.Config) (*App, error) {
  73. ctx, cancelFunc := context.WithCancel(context.Background())
  74. app := &App{
  75. entry: logrus.WithField("component", "app"),
  76. cnf: cnf,
  77. ctx: ctx,
  78. cancelFunc: cancelFunc,
  79. }
  80. // Initialize router first
  81. if cnf.Debug {
  82. gin.SetMode(gin.DebugMode)
  83. } else {
  84. // Set gin to release mode for production
  85. // This will disable debug logs and enable performance optimizations
  86. gin.SetMode(gin.ReleaseMode)
  87. }
  88. app.rtr = gin.New()
  89. // Disable automatic redirection of trailing slashes
  90. // This prevents 301 redirects that can cause CORS issues
  91. app.rtr.RedirectTrailingSlash = false
  92. app.rtr.RedirectFixedPath = false
  93. app.rtr.Use(gin.Recovery())
  94. app.rtr.Use(mw.Logger)
  95. // Add CORS middleware to handle cross-origin requests
  96. app.rtr.Use(mw.CORS())
  97. // Initialize services and handlers
  98. if err := app.initCommonServices(); err != nil {
  99. return nil, errors.Wrap(err, "initialize services")
  100. }
  101. if err := app.initHandlers(); err != nil {
  102. return nil, errors.Wrap(err, "initialize handlers")
  103. }
  104. // Set up routes after all handlers are initialized
  105. app.setupRoutes()
  106. return app, nil
  107. }
  108. func (a *App) Run() error {
  109. srv := &http.Server{
  110. Addr: fmt.Sprintf(":443"),
  111. Handler: a.rtr,
  112. }
  113. go func() {
  114. a.entry.WithField("address", srv.Addr).Info("Starting server on port 443 ")
  115. // Handle TLS if configured
  116. if a.cnf.Server.Tls.Enabled {
  117. a.entry.Info("Starting server with TLS...")
  118. err := srv.ListenAndServeTLS(a.cnf.Server.Tls.CertFile, a.cnf.Server.Tls.KeyFile)
  119. if err != nil && err != http.ErrServerClosed {
  120. a.entry.WithError(err).Fatal("Failed to start server")
  121. }
  122. } else {
  123. err := srv.ListenAndServe()
  124. if err != nil && err != http.ErrServerClosed {
  125. a.entry.WithError(err).Fatal("Failed to start server")
  126. }
  127. }
  128. a.entry.Info("Server stopped")
  129. }()
  130. quit := make(chan os.Signal, 1)
  131. signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
  132. <-quit
  133. a.entry.Info("Stopping server...")
  134. ctxTimeout, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
  135. defer cancelFunc()
  136. err := srv.Shutdown(ctxTimeout)
  137. if err != nil {
  138. return fmt.Errorf("shutdown server: %w", err)
  139. }
  140. a.entry.Info("Server stopped successfully")
  141. return nil
  142. }