Skip to content

Commit

Permalink
refactoring: routers (#15)
Browse files Browse the repository at this point in the history
* Update router.go

* minor improvement and build fix

* add missing return type
  • Loading branch information
mertcandav authored Sep 21, 2024
1 parent a1c6683 commit e1d180a
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,62 @@ import (
"github.com/lareii/copl.uk/server/middlewares"
)

func setupAuthGroup(app *fiber.App) fiber.Router {
g := app.Group("/auth")
g.Post("/register", middlewares.RateLimiterMiddleware(1, 300), auth.Register)
g.Post("/login", auth.Login)
g.Post("/logout", auth.Logout)
return g
}

func setupMeGroup(app *fiber.App) fiber.Router {
g := app.Group("/me")
g.Get("/", middlewares.AuthMiddleware(), me.User)
g.Patch("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), me.UpdateUser)
g.Get("/feed", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), me.GetFeed)
g.Get("/notifications", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), me.GetNotifications)
g.Patch("/notifications/:id", middlewares.AuthMiddleware(), me.UpdateNotification)
g.Get("/notifications/unread", middlewares.AuthMiddleware(), me.GetUnreadNotifications)
return g
}

func setupUserGroup(app *fiber.App) fiber.Router {
g := app.Group("/users")
g.Get("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), users.GetUsers)
g.Get("/:slug", middlewares.AuthMiddleware(), users.GetUser)
g.Get("/:slug/follows", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), users.Follows)
g.Post("/:slug/follows", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), users.FollowUser)
g.Get("/:slug/posts", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), users.GetUserPosts)
return g
}

func setupPostGroup(app *fiber.App) fiber.Router {
g := app.Group("/posts")
g.Get("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), posts.GetPosts)
g.Post("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(5, 60), posts.CreatePost)
g.Get("/:id", middlewares.AuthMiddleware(), posts.GetPost)
g.Delete("/:id", middlewares.AuthMiddleware(), posts.DeletePost)
g.Patch("/:id", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), posts.UpdatePost)
return g
}

func setupCommentGroup(postGroup fiber.Router) fiber.Router {
g := postGroup.Group("/:post_id/comments")
g.Get("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), posts.GetPostComments)
g.Post("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(5, 60), comments.CreateComment)
g.Get("/:id", middlewares.AuthMiddleware(), comments.GetComment)
g.Delete("/:id", middlewares.AuthMiddleware(), comments.DeleteComment)
g.Patch("/:id", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), comments.UpdateComment)
return g
}

func SetupRouter(app *fiber.App) {
app.Get("/ping", handlers.Ping)

authGroup := app.Group("/auth")
authGroup.Post("/register", middlewares.RateLimiterMiddleware(1, 300), auth.Register)
authGroup.Post("/login", auth.Login)
authGroup.Post("/logout", auth.Logout)

meGroup := app.Group("/me")
meGroup.Get("/", middlewares.AuthMiddleware(), me.User)
meGroup.Patch("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), me.UpdateUser)
meGroup.Get("/feed", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), me.GetFeed)
meGroup.Get("/notifications", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), me.GetNotifications)
meGroup.Patch("/notifications/:id", middlewares.AuthMiddleware(), me.UpdateNotification)
meGroup.Get("/notifications/unread", middlewares.AuthMiddleware(), me.GetUnreadNotifications)

userGroup := app.Group("/users")
userGroup.Get("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), users.GetUsers)
userGroup.Get("/:slug", middlewares.AuthMiddleware(), users.GetUser)
userGroup.Get("/:slug/follows", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), users.Follows)
userGroup.Post("/:slug/follows", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), users.FollowUser)
userGroup.Get("/:slug/posts", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), users.GetUserPosts)

postGroup := app.Group("/posts")
postGroup.Get("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), posts.GetPosts)
postGroup.Post("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(5, 60), posts.CreatePost)
postGroup.Get("/:id", middlewares.AuthMiddleware(), posts.GetPost)
postGroup.Delete("/:id", middlewares.AuthMiddleware(), posts.DeletePost)
postGroup.Patch("/:id", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), posts.UpdatePost)

commentGroup := postGroup.Group("/:post_id/comments")
commentGroup.Get("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), posts.GetPostComments)
commentGroup.Post("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(5, 60), comments.CreateComment)
commentGroup.Get("/:id", middlewares.AuthMiddleware(), comments.GetComment)
commentGroup.Delete("/:id", middlewares.AuthMiddleware(), comments.DeleteComment)
commentGroup.Patch("/:id", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), comments.UpdateComment)
// Setup groups.
setupAuthGroup(app)
setupMeGroup(app)
setupUserGroup(app)
postGroup := setupPostGroup(app)
setupCommentGroup(postGroup)
}

0 comments on commit e1d180a

Please sign in to comment.