routes.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package app
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "git.linuxforward.com/byom/byom-core/handlers"
  7. "git.linuxforward.com/byom/byom-core/jwtutils"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func addRoutes(
  11. rtr *gin.Engine,
  12. jwtSvc *jwtutils.Service,
  13. userHandler *handlers.UserHandler,
  14. workspaceHandler *handlers.WorkspaceHandler,
  15. profileHandler *handlers.ProfileHandler,
  16. ) {
  17. //group routes behind /api/v1/core
  18. coreRtr := rtr.Group("/api/v1/core")
  19. // Health check
  20. coreRtr.GET("/health", func(c *gin.Context) {
  21. c.JSON(http.StatusOK, gin.H{"status": "ok"})
  22. })
  23. // Auth routes
  24. authRoutes := coreRtr.Group("/auth")
  25. authRoutes.POST("/login", userHandler.Login)
  26. // Workspace onboarding routes
  27. workspaceOnboarding := coreRtr.Group("/workspaces")
  28. workspaceOnboarding.POST("/owners/init", userHandler.InitOwner)
  29. workspaceOnboarding.PUT("/owners", userHandler.CreateOwner)
  30. // Invitation management (public)
  31. invitationRoutes := coreRtr.Group("/invitations")
  32. invitationRoutes.POST("/accept", userHandler.AcceptInvitation)
  33. invitationRoutes.GET("/validate", userHandler.ValidateInvitation)
  34. // Authenticated routes
  35. auth := coreRtr.Group("/")
  36. auth.Use(authMiddleware(jwtSvc))
  37. // Workspace management
  38. workspaces := auth.Group("/workspaces")
  39. workspaces.POST("", workspaceHandler.Create)
  40. workspaces.POST("/:id/members", userHandler.AddMember)
  41. workspaces.GET("/:id/profiles", profileHandler.ListByWorkspace)
  42. // Profile management
  43. profiles := auth.Group("/profiles")
  44. profiles.GET("", profileHandler.List)
  45. profiles.POST("", profileHandler.Create)
  46. profiles.GET("/:id", profileHandler.Get)
  47. profiles.PUT("/:id", profileHandler.Update)
  48. profiles.DELETE("/:id", profileHandler.Delete)
  49. // User management
  50. users := auth.Group("/users")
  51. users.GET("/current", userHandler.GetCurrent)
  52. users.PUT("/current", userHandler.UpdateCurrent)
  53. // Invitation management (authenticated)
  54. authInvitations := auth.Group("/invitations")
  55. authInvitations.POST("", userHandler.CreateInvitation)
  56. }
  57. func authMiddleware(jwtSvc *jwtutils.Service) gin.HandlerFunc {
  58. return func(c *gin.Context) {
  59. authHeader := c.GetHeader("Authorization")
  60. if authHeader == "" {
  61. fmt.Println("No auth header")
  62. c.AbortWithStatus(http.StatusUnauthorized)
  63. return
  64. }
  65. // Get token part, using SplitAfter to preserve the delimiter
  66. token := strings.TrimPrefix(authHeader, "Bearer ")
  67. token = strings.TrimSpace(token)
  68. // Validate token without any cleaning since we now know it's clean
  69. claims, err := jwtSvc.ValidateToken(token)
  70. if err != nil {
  71. fmt.Println(err)
  72. c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
  73. return
  74. }
  75. // Store claims in the context using the exported key from jwtutils
  76. c.Set("claims", claims)
  77. c.Next()
  78. }
  79. }