init.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package app
  2. import (
  3. "fmt"
  4. "time"
  5. "git.linuxforward.com/byop/byop-engine/auth"
  6. "git.linuxforward.com/byop/byop-engine/cloud"
  7. "git.linuxforward.com/byop/byop-engine/dbmanager"
  8. "git.linuxforward.com/byop/byop-engine/dbstore"
  9. "git.linuxforward.com/byop/byop-engine/handlers"
  10. mw "git.linuxforward.com/byop/byop-engine/middleware"
  11. "git.linuxforward.com/byop/byop-engine/models"
  12. "git.linuxforward.com/byop/byop-engine/services"
  13. "github.com/pkg/errors"
  14. "github.com/gin-gonic/gin"
  15. )
  16. func (a *App) initCommonServices() error {
  17. // Initialize token store
  18. a.tokenStore = auth.NewMemoryTokenStore(time.Duration(a.cnf.Auth.CleanupInterval))
  19. // Initialize authentication service
  20. a.authService = auth.NewJWTService(
  21. []byte(a.cnf.Auth.PrivateKey),
  22. time.Duration(a.cnf.Auth.TokenDuration),
  23. a.tokenStore,
  24. )
  25. // Initialize providers
  26. if err := a.loadProviders(); err != nil {
  27. return errors.Wrap(err, "load providers")
  28. }
  29. // Initialize database manager
  30. switch a.cnf.Database.Type {
  31. case "sqlite":
  32. var err error
  33. a.dbManager, err = dbmanager.NewSQLiteManager(a.cnf.Database.Sqlite.File)
  34. if err != nil {
  35. return fmt.Errorf("create sqlite manager: %w", err)
  36. }
  37. // case "postgres":
  38. // a.dbManager = dbmanager.NewPostgresDbManager(a.cnf.Db.Host, a.cnf.Db.Port, a.cnf.Db.User, a.cnf.Db.Password, a.cnf.Db.Name)
  39. // case "mysql":
  40. // a.dbManager = dbmanager.NewMySQLDbManager(a.cnf.Db.Host, a.cnf.Db.Port, a.cnf.Db.User, a.cnf.Db.Password, a.cnf.Db.Name)
  41. default:
  42. return fmt.Errorf("unsupported database type: %s", a.cnf.Database.Type)
  43. }
  44. if err := a.dbManager.Connect(); err != nil {
  45. return fmt.Errorf("connect to database: %w", err)
  46. }
  47. // Auto migrate database schema
  48. if err := a.dbManager.Migrate(
  49. &models.User{},
  50. &models.Client{},
  51. &models.App{},
  52. &models.Template{},
  53. &models.Deployment{},
  54. &models.DeployedApp{},
  55. &models.DeployedAppResource{},
  56. // Add other models here
  57. ); err != nil {
  58. return fmt.Errorf("migrate database: %w", err)
  59. }
  60. a.entry.Info("Services initialized successfully, including authentication and database manager")
  61. return nil
  62. }
  63. func (a *App) initHandlers() error {
  64. // Initialize UserModule
  65. userStore := dbstore.NewUserStore(a.dbManager)
  66. userService := services.NewUserService(userStore)
  67. userHandler := handlers.NewUserHandler(userService)
  68. a.userModule = &UserModule{
  69. Store: userStore,
  70. Service: userService,
  71. Handler: userHandler,
  72. }
  73. // Initialize ClientModule
  74. clientStore := dbstore.NewClientStore(a.dbManager)
  75. clientService := services.NewClientService(clientStore)
  76. clientHandler := handlers.NewClientHandler(clientService)
  77. a.clientModule = &ClientModule{
  78. Store: clientStore,
  79. Service: clientService,
  80. Handler: clientHandler,
  81. }
  82. // Initialize AppModule
  83. appStore := dbstore.NewAppStore(a.dbManager)
  84. appService := services.NewAppService(appStore)
  85. appHandler := handlers.NewAppHandler(appService)
  86. a.appModule = &AppModule{
  87. Store: appStore,
  88. Service: appService,
  89. Handler: appHandler,
  90. }
  91. // Initialize TemplateModule
  92. templateStore := dbstore.NewTemplateStore(a.dbManager)
  93. templateService := services.NewTemplateService(templateStore)
  94. templateHandler := handlers.NewTemplateHandler(templateService)
  95. a.templateModule = &TemplateModule{
  96. Store: templateStore,
  97. Service: templateService,
  98. Handler: templateHandler,
  99. }
  100. // Initialize DeploymentModule
  101. deploymentStore := dbstore.NewDeploymentStore(a.dbManager)
  102. deploymentService := services.NewDeploymentService(
  103. deploymentStore,
  104. appStore,
  105. templateStore,
  106. clientStore,
  107. )
  108. deploymentHandler := handlers.NewDeploymentHandler(deploymentService)
  109. a.deploymentModule = &DeploymentModule{
  110. Store: deploymentStore,
  111. Service: deploymentService,
  112. Handler: deploymentHandler,
  113. }
  114. // Initialize authentication handler
  115. a.authHandler = handlers.NewAuthHandler(a.authService, a.userModule.Store)
  116. // Initialize resource handlers
  117. a.providerHandler = handlers.NewProviderHandler()
  118. // Initialize other handlers...
  119. a.entry.Info("Handlers initialized successfully")
  120. return nil
  121. }
  122. func (a *App) loadProviders() error {
  123. for name, config := range a.cnf.Providers {
  124. provider, ok := cloud.GetProvider(name)
  125. if !ok {
  126. return fmt.Errorf("provider %s not found", name)
  127. }
  128. err := provider.Initialize(config)
  129. if err != nil {
  130. return fmt.Errorf("initialize provider %s: %w", name, err)
  131. }
  132. a.entry.WithField("provider", name).Info("Provider initialized")
  133. }
  134. a.entry.Info("All providers loaded successfully")
  135. return nil
  136. }
  137. func (a *App) setupRoutes() {
  138. // API version group
  139. v1 := a.rtr.Group("/api/v1")
  140. // Auth routes - no middleware required
  141. // Public routes (no authentication required)
  142. public := v1.Group("/")
  143. public.POST("/users", a.userModule.Handler.CreateUser) // Allow user registration without authentication
  144. // Auth routes - no middleware required
  145. public.POST("/login", a.authHandler.Login) // Allow login without authentication
  146. public.POST("/refresh-token", a.authHandler.RefreshToken) // Allow token refresh without authentication
  147. public.POST("/logout", a.authHandler.Logout) // Allow logout without authentication
  148. public.GET("/health", func(c *gin.Context) {
  149. c.JSON(200, gin.H{"status": "ok"})
  150. })
  151. // Protected routes - require authentication
  152. protected := v1.Group("/")
  153. protected.Use(mw.Auth(a.authService)) // Auth middleware with service dependency
  154. // Register resource routes
  155. providers := protected.Group("/providers")
  156. a.providerHandler.RegisterRoutes(providers)
  157. // Client routes
  158. clients := protected.Group("/clients")
  159. clients.GET("/", a.clientModule.Handler.ListClients)
  160. clients.POST("/", a.clientModule.Handler.CreateClient)
  161. clients.GET("/:id", a.clientModule.Handler.GetClient)
  162. clients.PUT("/:id", a.clientModule.Handler.UpdateClient)
  163. clients.DELETE("/:id", a.clientModule.Handler.DeleteClient)
  164. clients.GET("/:id/deployments", a.clientModule.Handler.GetClientDeployments)
  165. // User routes
  166. users := protected.Group("/users")
  167. users.GET("/", a.userModule.Handler.ListUsers)
  168. users.GET("/:id", a.userModule.Handler.GetUser)
  169. users.PUT("/:id", a.userModule.Handler.UpdateUser)
  170. users.DELETE("/:id", a.userModule.Handler.DeleteUser)
  171. users.GET("/:id/deployments", a.userModule.Handler.GetUserDeployments)
  172. // App routes
  173. apps := protected.Group("/apps")
  174. apps.GET("/", a.appModule.Handler.ListApps)
  175. apps.POST("/", a.appModule.Handler.CreateApp)
  176. apps.GET("/:id", a.appModule.Handler.GetApp)
  177. apps.PUT("/:id", a.appModule.Handler.UpdateApp)
  178. apps.DELETE("/:id", a.appModule.Handler.DeleteApp)
  179. apps.GET("/:id/deployments", a.appModule.Handler.GetAppDeployments)
  180. // Template routes
  181. templates := protected.Group("/templates")
  182. templates.GET("/", a.templateModule.Handler.ListTemplates)
  183. templates.POST("/", a.templateModule.Handler.CreateTemplate)
  184. templates.GET("/:id", a.templateModule.Handler.GetTemplate)
  185. templates.PUT("/:id", a.templateModule.Handler.UpdateTemplate)
  186. templates.DELETE("/:id", a.templateModule.Handler.DeleteTemplate)
  187. templates.GET("/:id/deployments", a.templateModule.Handler.GetTemplateDeployments)
  188. // Deployment routes
  189. deployments := protected.Group("/deployments")
  190. a.deploymentModule.Handler.RegisterRoutes(deployments)
  191. a.entry.Info("Routes configured successfully")
  192. }