server.go 3.8 KB

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