init.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package app
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "git.linuxforward.com/byop/byop-engine/auth"
  8. "git.linuxforward.com/byop/byop-engine/cloud"
  9. "git.linuxforward.com/byop/byop-engine/dbstore"
  10. "git.linuxforward.com/byop/byop-engine/handlers"
  11. mw "git.linuxforward.com/byop/byop-engine/middleware"
  12. "git.linuxforward.com/byop/byop-engine/services"
  13. "github.com/pkg/errors"
  14. "github.com/sirupsen/logrus"
  15. "github.com/gin-gonic/gin"
  16. )
  17. func (a *App) initCommonServices() error {
  18. // Initialize database
  19. db, err := dbstore.NewSQLiteStore(a.cnf.Database.DSN)
  20. if err != nil {
  21. return errors.Wrap(err, "initialize database")
  22. }
  23. a.database = db
  24. // Initialize token store
  25. a.tokenStore = auth.NewMemoryTokenStore(time.Duration(a.cnf.Auth.CleanupInterval))
  26. // Initialize authentication service
  27. a.authService = auth.NewJWTService(
  28. []byte(a.cnf.Auth.PrivateKey),
  29. time.Duration(a.cnf.Auth.TokenDuration),
  30. a.tokenStore,
  31. a.database, // Added database argument
  32. )
  33. // Initialize providers
  34. if err := a.loadProviders(); err != nil {
  35. return errors.Wrap(err, "load providers")
  36. }
  37. // If debug mode, create default admin user
  38. if a.cnf.Debug {
  39. db.CreateDefaultAdmin(context.Background()) // Added context argument
  40. a.entry.Info("Debug mode enabled, default admin user created")
  41. // Output the admin credentials
  42. a.entry.WithFields(logrus.Fields{
  43. "email": "admin@byop.local",
  44. "password": "admin123",
  45. }).Info("Default admin credentials")
  46. }
  47. // Get Ovh provider
  48. ovhProvider, _ := cloud.GetProvider("ovh")
  49. a.previewService = services.NewPreviewServiceManager(a.database, ovhProvider, a.cnf.LocalPreview, a.cnf, a.registryClient, a.cnf.ReistryUrl, "", "")
  50. a.builderService = services.NewBuilderService(a.database, a.buildkitClient, a.registryClient, 5)
  51. a.entry.Info("Services initialized successfully, including authentication and database manager")
  52. return nil
  53. }
  54. func (a *App) initHandlers() error {
  55. // Initialize UserModule
  56. a.userHandler = handlers.NewUserHandler(a.database)
  57. // Initialize ClientModule
  58. a.clientHandler = handlers.NewClientHandler(a.database)
  59. // Initialize ComponentModule
  60. a.componentHandler = handlers.NewComponentHandler(a.database, a.builderService, a.cnf.ReistryUrl)
  61. // Initialize AppModule
  62. a.appHandler = handlers.NewAppsHandler(a.database, a.previewService)
  63. // Initialize DeploymentModule
  64. a.deploymentHandler = handlers.NewDeploymentHandler(a.database)
  65. // Initialize authentication handler
  66. a.authHandler = handlers.NewAuthHandler(a.authService, a.database)
  67. // Initialize resource handlers
  68. a.providerHandler = handlers.NewProviderHandler()
  69. // Initialize PreviewHandler
  70. a.previewHandler = handlers.NewPreviewHandler(a.previewService, a.database)
  71. a.entry.Info("Handlers initialized successfully")
  72. return nil
  73. }
  74. // Updated loadProviders method
  75. func (a *App) loadProviders() error {
  76. for name, cnf := range a.cnf.Providers {
  77. // Use the new InitializeProvider function instead of GetProvider + Initialize
  78. err := cloud.InitializeProvider(name, cnf)
  79. if err != nil {
  80. return fmt.Errorf("initialize provider %s: %w", name, err)
  81. }
  82. a.entry.WithField("provider", name).Info("Provider initialized")
  83. }
  84. a.entry.Info("All providers loaded successfully")
  85. return nil
  86. }
  87. func (a *App) setupRoutes() {
  88. // Add CORS middleware to handle all cross-origin requests
  89. a.rtr.Use(func(c *gin.Context) {
  90. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  91. c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
  92. c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
  93. c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
  94. // Allow redirects
  95. c.Writer.Header().Set("Access-Control-Expose-Headers", "Location")
  96. c.Writer.Header().Set("Access-Control-Max-Age", "86400") // Cache preflight response for 24 hours
  97. c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
  98. // Handle preflight OPTIONS requests
  99. if c.Request.Method == http.MethodOptions {
  100. c.AbortWithStatus(http.StatusNoContent)
  101. return
  102. }
  103. c.Next()
  104. })
  105. // API version group
  106. v1 := a.rtr.Group("/api/v1")
  107. // Auth routes - no middleware required
  108. // Public routes (no authentication required)
  109. public := v1.Group("/")
  110. public.POST("/users", a.userHandler.CreateUser) // Allow user registration without authentication
  111. // Auth routes - no middleware required
  112. public.POST("/login", a.authHandler.Login) // Allow login without authentication
  113. public.POST("/refresh-token", a.authHandler.RefreshToken) // Allow token refresh without authentication
  114. public.POST("/logout", a.authHandler.Logout) // Allow logout without authentication
  115. public.GET("/health", func(c *gin.Context) {
  116. c.JSON(200, gin.H{"status": "ok"})
  117. })
  118. // Protected routes - require authentication
  119. protected := v1.Group("/")
  120. protected.Use(mw.Auth(a.authService)) // Auth middleware with service dependency
  121. // Register resource routes
  122. providers := protected.Group("/providers")
  123. a.providerHandler.RegisterRoutes(providers)
  124. // Client routes - registering both with and without trailing slash
  125. clients := protected.Group("/clients")
  126. clients.GET("", a.clientHandler.ListClients)
  127. clients.POST("", a.clientHandler.CreateClient)
  128. clients.GET("/:id", a.clientHandler.GetClient)
  129. clients.PUT("/:id", a.clientHandler.UpdateClient)
  130. clients.DELETE("/:id", a.clientHandler.DeleteClient)
  131. clients.GET("/:id/deployments", a.clientHandler.GetClientDeployments)
  132. // User routes - registering both with and without trailing slash
  133. users := protected.Group("/users")
  134. users.GET("", a.userHandler.ListUsers)
  135. users.GET("/:id", a.userHandler.GetUser)
  136. users.PUT("/:id", a.userHandler.UpdateUser)
  137. users.DELETE("/:id", a.userHandler.DeleteUser)
  138. users.GET("/:id/deployments", a.userHandler.GetUserDeployments)
  139. // Component routes (formerly Apps)
  140. components := protected.Group("/components")
  141. components.GET("", a.componentHandler.ListComponents)
  142. components.POST("", a.componentHandler.CreateComponent)
  143. components.GET("/:id", a.componentHandler.GetComponent)
  144. components.PUT("/:id", a.componentHandler.UpdateComponent)
  145. components.DELETE("/:id", a.componentHandler.DeleteComponent)
  146. components.GET("/:id/deployments", a.componentHandler.GetComponentDeployments)
  147. // App routes (formerly Templates)
  148. apps := protected.Group("/apps")
  149. apps.GET("", a.appHandler.ListApps)
  150. apps.POST("", a.appHandler.CreateApp)
  151. apps.GET("/:id", a.appHandler.GetApp)
  152. apps.PUT("/:id", a.appHandler.UpdateApp)
  153. apps.DELETE("/:id", a.appHandler.DeleteApp)
  154. apps.GET("/:id/deployments", a.appHandler.GetAppDeployments)
  155. // App preview route
  156. apps.POST("/:id/preview", a.appHandler.CreateAppPreview)
  157. apps.GET("/:id/preview", a.appHandler.GetAppPreview)
  158. apps.DELETE("/:id/preview", a.appHandler.DeleteAppPreview)
  159. // Deployment routes - need to handle both versions
  160. deployments := protected.Group("/deployments")
  161. deployments.GET("", a.deploymentHandler.ListDeployments)
  162. deployments.POST("", a.deploymentHandler.CreateDeployment)
  163. deployments.GET("/:id", a.deploymentHandler.GetDeployment)
  164. deployments.PUT("/:id", a.deploymentHandler.UpdateDeployment)
  165. deployments.DELETE("/:id", a.deploymentHandler.DeleteDeployment)
  166. deployments.PUT("/:id/status", a.deploymentHandler.UpdateDeploymentStatus)
  167. // deployments.GET("/by-client/:clientId", a.deploymentHandler.GetDeploymentsByClient)
  168. // deployments.GET("/by-app/:appId", a.deploymentHandler.GetDeploymentsByTemplate) // Was by-template
  169. // deployments.GET("/by-user/:userId", a.deploymentHandler.GetDeploymentsByUser)
  170. // // Add other resource routes as needed
  171. a.entry.Info("Routes configured successfully")
  172. }