init.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/services"
  12. "github.com/pkg/errors"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func (a *App) initCommonServices() error {
  16. // Initialize token store
  17. a.tokenStore = auth.NewMemoryTokenStore(time.Duration(a.cnf.Auth.CleanupInterval))
  18. // Initialize authentication service
  19. a.authService = auth.NewJWTService(
  20. []byte(a.cnf.Auth.PrivateKey),
  21. time.Duration(a.cnf.Auth.TokenDuration),
  22. a.tokenStore,
  23. )
  24. // Initialize providers
  25. if err := a.loadProviders(); err != nil {
  26. return errors.Wrap(err, "load providers")
  27. }
  28. // Initialize database manager
  29. switch a.cnf.Database.Type {
  30. case "sqlite":
  31. var err error
  32. a.dbManager, err = dbmanager.NewSQLiteManager(a.cnf.Database.Sqlite.File)
  33. if err != nil {
  34. return fmt.Errorf("create sqlite manager: %w", err)
  35. }
  36. // case "postgres":
  37. // a.dbManager = dbmanager.NewPostgresDbManager(a.cnf.Db.Host, a.cnf.Db.Port, a.cnf.Db.User, a.cnf.Db.Password, a.cnf.Db.Name)
  38. // case "mysql":
  39. // a.dbManager = dbmanager.NewMySQLDbManager(a.cnf.Db.Host, a.cnf.Db.Port, a.cnf.Db.User, a.cnf.Db.Password, a.cnf.Db.Name)
  40. default:
  41. a.dbManager = dbmanager.NewMemoryDbManager()
  42. }
  43. if err := a.dbManager.Connect(); err != nil {
  44. return fmt.Errorf("connect to database: %w", err)
  45. }
  46. a.entry.Info("Services initialized successfully, including authentication and database manager")
  47. return nil
  48. }
  49. func (a *App) initHandlers() error {
  50. // Initialize UserModule
  51. userStore := dbstore.NewUserStore(a.dbManager)
  52. if err := userStore.CreateTable(); err != nil {
  53. return fmt.Errorf("failed to create users table: %w", err)
  54. }
  55. userService := services.NewUserService(userStore)
  56. userHandler := handlers.NewUserHandler(userService)
  57. a.userModule = &UserModule{
  58. Store: userStore,
  59. Service: userService,
  60. Handler: userHandler,
  61. }
  62. // Initialize ClientModule
  63. clientStore := dbstore.NewClientStore(a.dbManager)
  64. if err := clientStore.CreateTable(); err != nil {
  65. return fmt.Errorf("failed to create clients table: %w", err)
  66. }
  67. clientService := services.NewClientService(clientStore)
  68. clientHandler := handlers.NewClientHandler(clientService)
  69. a.clientModule = &ClientModule{
  70. Store: clientStore,
  71. Service: clientService,
  72. Handler: clientHandler,
  73. }
  74. // Initialize authentication handler
  75. a.authHandler = handlers.NewAuthHandler(a.authService, a.userModule.Store)
  76. // Initialize resource handlers
  77. a.providerHandler = handlers.NewProviderHandler()
  78. // Initialize other handlers...
  79. a.entry.Info("Handlers initialized successfully")
  80. return nil
  81. }
  82. func (a *App) loadProviders() error {
  83. for name, config := range a.cnf.Providers {
  84. provider, ok := cloud.GetProvider(name)
  85. if !ok {
  86. return fmt.Errorf("provider %s not found", name)
  87. }
  88. err := provider.Initialize(config)
  89. if err != nil {
  90. return fmt.Errorf("initialize provider %s: %w", name, err)
  91. }
  92. a.entry.WithField("provider", name).Info("Provider initialized")
  93. }
  94. a.entry.Info("All providers loaded successfully")
  95. return nil
  96. }
  97. func (a *App) setupRoutes() {
  98. // API version group
  99. v1 := a.rtr.Group("/api/v1")
  100. // Auth routes - no middleware required
  101. // Public routes (no authentication required)
  102. public := v1.Group("/")
  103. public.POST("/users", a.userModule.Handler.CreateUser) // Allow user registration without authentication
  104. // Auth routes - no middleware required
  105. public.POST("/login", a.authHandler.Login) // Allow login without authentication
  106. public.POST("/refresh-token", a.authHandler.RefreshToken) // Allow token refresh without authentication
  107. public.POST("/logout", a.authHandler.Logout) // Allow logout without authentication
  108. public.GET("/health", func(c *gin.Context) {
  109. c.JSON(200, gin.H{"status": "ok"})
  110. })
  111. // Protected routes - require authentication
  112. protected := v1.Group("/")
  113. protected.Use(mw.Auth(a.authService)) // Auth middleware with service dependency
  114. // Register resource routes
  115. providers := protected.Group("/providers")
  116. a.providerHandler.RegisterRoutes(providers)
  117. clients := protected.Group("/clients")
  118. clients.GET("/", a.clientModule.Handler.ListClients)
  119. clients.POST("/", a.clientModule.Handler.CreateClient)
  120. clients.GET("/:id", a.clientModule.Handler.GetClient)
  121. clients.PUT("/:id", a.clientModule.Handler.UpdateClient)
  122. clients.DELETE("/:id", a.clientModule.Handler.DeleteClient)
  123. clients.GET("/:id/deployments", a.clientModule.Handler.GetClientDeployments)
  124. users := protected.Group("/users")
  125. users.GET("/", a.userModule.Handler.ListUsers)
  126. users.GET("/:id", a.userModule.Handler.GetUser)
  127. users.PUT("/:id", a.userModule.Handler.UpdateUser)
  128. users.DELETE("/:id", a.userModule.Handler.DeleteUser)
  129. users.GET("/:id/deployments", a.userModule.Handler.GetUserDeployments)
  130. // Register other resource routes...
  131. a.entry.Info("Routes configured successfully")
  132. }