server.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package app
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. "git.linuxforward.com/byop/byop-engine/auth"
  11. "git.linuxforward.com/byop/byop-engine/config"
  12. "git.linuxforward.com/byop/byop-engine/dbmanager"
  13. "git.linuxforward.com/byop/byop-engine/dbstore"
  14. "git.linuxforward.com/byop/byop-engine/handlers"
  15. mw "git.linuxforward.com/byop/byop-engine/middleware"
  16. "git.linuxforward.com/byop/byop-engine/services"
  17. "github.com/gin-gonic/gin"
  18. "github.com/pkg/errors"
  19. "github.com/sirupsen/logrus"
  20. )
  21. type App struct {
  22. entry *logrus.Entry
  23. cnf *config.Config
  24. ctx context.Context
  25. cancelFunc context.CancelFunc
  26. rtr *gin.Engine
  27. // Database
  28. dbManager dbmanager.DbManager
  29. // Services
  30. authService auth.Service
  31. tokenStore auth.TokenStore
  32. // Modules
  33. authHandler *handlers.AuthHandler
  34. userModule *UserModule
  35. clientModule *ClientModule
  36. componentModule *ComponentModule // Renamed from appModule
  37. appModule *AppModule // Renamed from templateModule
  38. deploymentModule *DeploymentModule
  39. // Resource Handlers
  40. providerHandler *handlers.ProviderHandler
  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. type ComponentModule struct {
  55. Store *dbstore.ComponentStore // Renamed from AppStore
  56. Service *services.ComponentService // Renamed from AppService
  57. Handler *handlers.ComponentHandler // Renamed from AppHandler
  58. }
  59. type AppModule struct {
  60. Store *dbstore.AppStore // Renamed from TemplateStore
  61. Service *services.AppService // Renamed from TemplateService
  62. Handler *handlers.AppsHandler // Renamed from TemplateHandler
  63. }
  64. type DeploymentModule struct {
  65. Store *dbstore.DeploymentStore
  66. Service *services.DeploymentService
  67. Handler *handlers.DeploymentHandler
  68. }
  69. func NewApp(cnf *config.Config) (*App, error) {
  70. ctx, cancelFunc := context.WithCancel(context.Background())
  71. app := &App{
  72. entry: logrus.WithField("component", "app"),
  73. cnf: cnf,
  74. ctx: ctx,
  75. cancelFunc: cancelFunc,
  76. }
  77. // Initialize router first
  78. if cnf.Debug {
  79. gin.SetMode(gin.DebugMode)
  80. } else {
  81. // Set gin to release mode for production
  82. // This will disable debug logs and enable performance optimizations
  83. gin.SetMode(gin.ReleaseMode)
  84. }
  85. app.rtr = gin.New()
  86. app.rtr.Use(gin.Recovery())
  87. app.rtr.Use(mw.Logger)
  88. // Initialize services and handlers
  89. if err := app.initCommonServices(); err != nil {
  90. return nil, errors.Wrap(err, "initialize services")
  91. }
  92. if err := app.initHandlers(); err != nil {
  93. return nil, errors.Wrap(err, "initialize handlers")
  94. }
  95. // Set up routes after all handlers are initialized
  96. app.setupRoutes()
  97. return app, nil
  98. }
  99. func (a *App) Run() error {
  100. srv := &http.Server{
  101. Addr: fmt.Sprintf(":443"),
  102. Handler: a.rtr,
  103. }
  104. go func() {
  105. a.entry.WithField("address", srv.Addr).Info("Starting server on port 443 ")
  106. // Handle TLS if configured
  107. if a.cnf.Server.Tls.Enabled {
  108. a.entry.Info("Starting server with TLS...")
  109. err := srv.ListenAndServeTLS(a.cnf.Server.Tls.CertFile, a.cnf.Server.Tls.KeyFile)
  110. if err != nil && err != http.ErrServerClosed {
  111. a.entry.WithError(err).Fatal("Failed to start server")
  112. }
  113. } else {
  114. err := srv.ListenAndServe()
  115. if err != nil && err != http.ErrServerClosed {
  116. a.entry.WithError(err).Fatal("Failed to start server")
  117. }
  118. }
  119. a.entry.Info("Server stopped")
  120. }()
  121. quit := make(chan os.Signal, 1)
  122. signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
  123. <-quit
  124. a.entry.Info("Stopping server...")
  125. ctxTimeout, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
  126. defer cancelFunc()
  127. err := srv.Shutdown(ctxTimeout)
  128. if err != nil {
  129. return fmt.Errorf("shutdown server: %w", err)
  130. }
  131. a.entry.Info("Server stopped successfully")
  132. return nil
  133. }