routes.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. coreRtr.POST("/auth/login", userHandler.Login)
  25. // User init routes
  26. coreRtr.POST("/workspaces/owner", userHandler.InitWorkspaceOwner)
  27. coreRtr.PUT("/workspaces/owner", userHandler.CreateWorkspaceOwner)
  28. coreRtr.POST("/workspaces/invite", userHandler.CreateInvitedUser)
  29. coreRtr.GET("/workspaces/invite/validate", userHandler.ValidateInvitedUser)
  30. // Logged in user routes
  31. auth := coreRtr.Group("/")
  32. auth.Use(authMiddleware(jwtSvc))
  33. // User management
  34. auth.POST("/users/invite", userHandler.InviteUser)
  35. //add user to workspace
  36. auth.POST("/users/workspaces", userHandler.AddUserToWorkspace)
  37. // auth.GET("/users/invitations", userHandler.ListInvitations)
  38. // auth.DELETE("/users/invitations/:id", userHandler.CancelInvitation)
  39. // Workspace management
  40. auth.POST("/workspaces", workspaceHandler.CreateWorkspace)
  41. // Profile management
  42. auth.GET("/profiles", profileHandler.GetProfiles)
  43. auth.POST("/profiles", profileHandler.CreateProfile)
  44. auth.GET("/profiles/:id", profileHandler.GetProfile)
  45. auth.PUT("/profiles/:id", profileHandler.UpdateProfile)
  46. auth.DELETE("/profiles/:id", profileHandler.DeleteProfile)
  47. auth.GET("/profiles/workspaces", profileHandler.GetProfilesByWorkspace)
  48. // User profile
  49. auth.GET("/users/me", userHandler.GetCurrentUser)
  50. auth.PUT("/users/me", userHandler.UpdateCurrentUser)
  51. }
  52. func authMiddleware(jwtSvc *jwtutils.Service) gin.HandlerFunc {
  53. return func(c *gin.Context) {
  54. authHeader := c.GetHeader("Authorization")
  55. if authHeader == "" {
  56. fmt.Println("No auth header")
  57. c.AbortWithStatus(http.StatusUnauthorized)
  58. return
  59. }
  60. // Get token part, using SplitAfter to preserve the delimiter
  61. token := strings.TrimPrefix(authHeader, "Bearer ")
  62. token = strings.TrimSpace(token)
  63. // Validate token without any cleaning since we now know it's clean
  64. claims, err := jwtSvc.ValidateToken(token)
  65. if err != nil {
  66. fmt.Println(err)
  67. c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
  68. return
  69. }
  70. // Store claims in the context using the exported key from jwtutils
  71. c.Set("claims", claims)
  72. c.Next()
  73. }
  74. }