diff --git a/backend/entities/categories/base/routes.go b/backend/entities/categories/base/routes.go index a171c0074..f41980ee0 100644 --- a/backend/entities/categories/base/routes.go +++ b/backend/entities/categories/base/routes.go @@ -4,7 +4,6 @@ import ( "github.com/GenerateNU/sac/backend/auth" "github.com/GenerateNU/sac/backend/entities/categories/tags" "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" "github.com/gofiber/fiber/v2" ) @@ -24,7 +23,7 @@ func Category(categoryParams types.RouteParams) fiber.Router { categories := categoryParams.Router.Group("/categories") categories.Post("/", categoryParams.AuthMiddleware.Authorize(auth.CreateAll), categoryController.CreateCategory) - categories.Get("/", fiberpaginate.New(), categoryController.GetCategories) + categories.Get("/", categoryParams.UtilityMiddleware.Paginator, categoryController.GetCategories) // api/v1/categories/:categoryID/* categoryID := categories.Group("/:categoryID") diff --git a/backend/entities/categories/tags/routes.go b/backend/entities/categories/tags/routes.go index 816dcc9eb..f4a0d4cb4 100644 --- a/backend/entities/categories/tags/routes.go +++ b/backend/entities/categories/tags/routes.go @@ -2,7 +2,6 @@ package tags import ( "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func CategoryTag(categoryParams types.RouteParams) { @@ -11,6 +10,6 @@ func CategoryTag(categoryParams types.RouteParams) { // api/v1/categories/:categoryID/tags/* categoryTags := categoryParams.Router.Group("/tags") - categoryTags.Get("/", fiberpaginate.New(), categoryTagController.GetTagsByCategory) + categoryTags.Get("/", categoryParams.UtilityMiddleware.Paginator, categoryTagController.GetTagsByCategory) categoryTags.Get("/:tagID", categoryTagController.GetTagByCategory) } diff --git a/backend/entities/clubs/base/routes.go b/backend/entities/clubs/base/routes.go index 5d89fd9ee..7633698b2 100644 --- a/backend/entities/clubs/base/routes.go +++ b/backend/entities/clubs/base/routes.go @@ -8,8 +8,7 @@ import ( "github.com/GenerateNU/sac/backend/entities/clubs/members" "github.com/GenerateNU/sac/backend/entities/clubs/pocs" "github.com/GenerateNU/sac/backend/entities/clubs/tags" - "github.com/GenerateNU/sac/backend/middleware" - "github.com/garrettladley/fiberpaginate" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" "github.com/gofiber/fiber/v2" @@ -32,7 +31,7 @@ func ClubRouter(clubParams types.RouteParams) fiber.Router { // api/v1/clubs/* clubs := clubParams.Router.Group("/clubs") - clubs.Get("/", fiberpaginate.New(), clubController.GetClubs) + clubs.Get("/", clubParams.UtilityMiddleware.Paginator, clubController.GetClubs) clubs.Post("/", clubParams.AuthMiddleware.Authorize(p.CreateAll), clubController.CreateClub) // api/v1/clubs/:clubID/* @@ -41,7 +40,7 @@ func ClubRouter(clubParams types.RouteParams) fiber.Router { clubID.Get("/", clubController.GetClub) clubID.Patch( "/", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubController.UpdateClub, ) clubID.Delete("/", clubParams.AuthMiddleware.Authorize(p.DeleteAll), clubController.DeleteClub) diff --git a/backend/entities/clubs/contacts/routes.go b/backend/entities/clubs/contacts/routes.go index 358408bfb..252fcfe52 100644 --- a/backend/entities/clubs/contacts/routes.go +++ b/backend/entities/clubs/contacts/routes.go @@ -1,7 +1,7 @@ package contacts import ( - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" ) @@ -14,7 +14,7 @@ func ClubContact(clubParams types.RouteParams) { clubContacts.Get("/", clubContactController.GetClubContacts) clubContacts.Put( "/", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubContactController.PutContact, ) } diff --git a/backend/entities/clubs/events/routes.go b/backend/entities/clubs/events/routes.go index 4aefdf69a..1a0bd42dd 100644 --- a/backend/entities/clubs/events/routes.go +++ b/backend/entities/clubs/events/routes.go @@ -2,7 +2,6 @@ package events import ( "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func ClubEvent(clubParams types.RouteParams) { @@ -11,5 +10,5 @@ func ClubEvent(clubParams types.RouteParams) { // api/v1/clubs/:clubID/events/* events := clubParams.Router.Group("/events") - events.Get("/", fiberpaginate.New(), clubEventController.GetClubEvents) + events.Get("/", clubParams.UtilityMiddleware.Paginator, clubEventController.GetClubEvents) } diff --git a/backend/entities/clubs/followers/routes.go b/backend/entities/clubs/followers/routes.go index a67a4744e..81f9e3a61 100644 --- a/backend/entities/clubs/followers/routes.go +++ b/backend/entities/clubs/followers/routes.go @@ -2,7 +2,6 @@ package followers import ( "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func ClubFollower(clubParams types.RouteParams) { @@ -11,5 +10,5 @@ func ClubFollower(clubParams types.RouteParams) { clubFollowers := clubParams.Router.Group("/followers") // api/clubs/:clubID/followers/* - clubFollowers.Get("/", fiberpaginate.New(), clubFollowerController.GetClubFollowers) + clubFollowers.Get("/", clubParams.UtilityMiddleware.Paginator, clubFollowerController.GetClubFollowers) } diff --git a/backend/entities/clubs/members/routes.go b/backend/entities/clubs/members/routes.go index 891ec3a7d..660a83db8 100644 --- a/backend/entities/clubs/members/routes.go +++ b/backend/entities/clubs/members/routes.go @@ -1,9 +1,8 @@ package members import ( - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func ClubMember(clubParams types.RouteParams) { @@ -12,15 +11,15 @@ func ClubMember(clubParams types.RouteParams) { clubMembers := clubParams.Router.Group("/members") // api/v1/clubs/:clubID/members/* - clubMembers.Get("/", fiberpaginate.New(), clubMemberController.GetClubMembers) + clubMembers.Get("/", clubParams.UtilityMiddleware.Paginator, clubMemberController.GetClubMembers) clubMembers.Post( "/:userID", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubMemberController.CreateClubMember, ) clubMembers.Delete( "/:userID", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubMemberController.DeleteClubMember, ) } diff --git a/backend/entities/clubs/pocs/routes.go b/backend/entities/clubs/pocs/routes.go index 55594beef..bd57f188c 100644 --- a/backend/entities/clubs/pocs/routes.go +++ b/backend/entities/clubs/pocs/routes.go @@ -1,7 +1,7 @@ package pocs import ( - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" ) @@ -15,22 +15,22 @@ func ClubPointOfContact(clubParams types.RouteParams) { clubPointOfContacts.Get("/:pocID", clubPointOfContactController.GetClubPointOfContact) clubPointOfContacts.Post( "/", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubPointOfContactController.CreateClubPointOfContact, ) clubPointOfContacts.Patch( "/:pocID", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubPointOfContactController.UpdateClubPointOfContact, ) clubPointOfContacts.Patch( "/:pocID/photo", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubPointOfContactController.UpdateClubPointOfContactPhoto, ) clubPointOfContacts.Delete( "/:pocID", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubPointOfContactController.DeleteClubPointOfContact, ) } diff --git a/backend/entities/clubs/tags/routes.go b/backend/entities/clubs/tags/routes.go index b3a5a140b..4384d1ee3 100644 --- a/backend/entities/clubs/tags/routes.go +++ b/backend/entities/clubs/tags/routes.go @@ -1,7 +1,7 @@ package tags import ( - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" ) @@ -14,12 +14,12 @@ func ClubTag(clubParams types.RouteParams) { clubTags.Get("/", clubTagController.GetClubTags) clubTags.Post( "/", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubTagController.CreateClubTags, ) clubTags.Delete( "/:tagID", - middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), clubTagController.DeleteClubTag, ) } diff --git a/backend/entities/contacts/base/routes.go b/backend/entities/contacts/base/routes.go index a68d7d15c..453705783 100644 --- a/backend/entities/contacts/base/routes.go +++ b/backend/entities/contacts/base/routes.go @@ -3,7 +3,6 @@ package base import ( "github.com/GenerateNU/sac/backend/auth" "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func Contact(contactParams types.RouteParams) { @@ -12,7 +11,7 @@ func Contact(contactParams types.RouteParams) { // api/v1/contacts/* contacts := contactParams.Router.Group("/contacts") - contacts.Get("/", fiberpaginate.New(), contactController.GetContacts) + contacts.Get("/", contactParams.UtilityMiddleware.Paginator, contactController.GetContacts) contacts.Get("/:contactID", contactController.GetContact) contacts.Delete("/:contactID", contactParams.AuthMiddleware.Authorize(auth.DeleteAll), contactController.DeleteContact) } diff --git a/backend/entities/events/base/routes.go b/backend/entities/events/base/routes.go index fa554a762..c0be2cb84 100644 --- a/backend/entities/events/base/routes.go +++ b/backend/entities/events/base/routes.go @@ -5,9 +5,8 @@ import ( "github.com/GenerateNU/sac/backend/entities/events/tags" "github.com/gofiber/fiber/v2" - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func EventRoutes(eventParams types.RouteParams) { @@ -23,10 +22,10 @@ func EventRouter(eventParams types.RouteParams) fiber.Router { // api/v1/events/* events := eventParams.Router.Group("/events") - events.Get("/", fiberpaginate.New(), eventController.GetAllEvents) + events.Get("/", eventParams.UtilityMiddleware.Paginator, eventController.GetAllEvents) events.Post( "/", - middleware.AttachExtractor(eventParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")), + authMiddleware.AttachExtractor(eventParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), eventController.CreateEvent, ) diff --git a/backend/entities/events/series/routes.go b/backend/entities/events/series/routes.go index aaa97677d..b4310c2da 100644 --- a/backend/entities/events/series/routes.go +++ b/backend/entities/events/series/routes.go @@ -2,7 +2,7 @@ package series import ( "github.com/GenerateNU/sac/backend/extractors" - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" ) @@ -14,7 +14,7 @@ func EventSeries(eventParams types.RouteParams) { eventSeries.Post( "/", - middleware.AttachExtractor(eventParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromBody("host")), + authMiddleware.AttachExtractor(eventParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromBody("host")), eventSeriesController.CreateEventSeries, ) @@ -23,9 +23,9 @@ func EventSeries(eventParams types.RouteParams) { eventSeriedID.Get("/", eventSeriesController.GetEventSeries) eventSeriedID.Delete( "/", - middleware.AttachExtractor( + authMiddleware.AttachExtractor( eventParams.AuthMiddleware.ClubAuthorizeById, - middleware.FromExtractFromParamIntoQuery("seriesID", eventParams.ServiceParams.DB, extractors.GetSeriesHost), + authMiddleware.FromExtractFromParamIntoQuery("seriesID", eventParams.ServiceParams.DB, extractors.GetSeriesHost), ), eventSeriesController.DeleteEventSeries, ) diff --git a/backend/entities/events/tags/routes.go b/backend/entities/events/tags/routes.go index 823647869..7ba3bb9c4 100644 --- a/backend/entities/events/tags/routes.go +++ b/backend/entities/events/tags/routes.go @@ -1,7 +1,7 @@ package tags import ( - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" "github.com/GenerateNU/sac/backend/types" ) @@ -13,12 +13,12 @@ func EventTags(eventParams types.RouteParams) { eventTags.Get("/", eventTagController.GetEventTags) eventTags.Post( "/", - middleware.AttachExtractor(eventParams.AuthMiddleware.EventAuthorizeById, middleware.ExtractFromParams("eventID")), + authMiddleware.AttachExtractor(eventParams.AuthMiddleware.EventAuthorizeById, authMiddleware.ExtractFromParams("eventID")), eventTagController.CreateEventTags, ) eventTags.Delete( "/:tagID", - middleware.AttachExtractor(eventParams.AuthMiddleware.EventAuthorizeById, middleware.ExtractFromParams("eventID")), + authMiddleware.AttachExtractor(eventParams.AuthMiddleware.EventAuthorizeById, authMiddleware.ExtractFromParams("eventID")), eventTagController.DeleteEventTag, ) } diff --git a/backend/entities/files/base/routes.go b/backend/entities/files/base/routes.go index 23d8baac4..c52205db5 100644 --- a/backend/entities/files/base/routes.go +++ b/backend/entities/files/base/routes.go @@ -2,7 +2,6 @@ package base import ( "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func File(fileParams types.RouteParams) { @@ -10,7 +9,7 @@ func File(fileParams types.RouteParams) { file := fileParams.Router.Group("/files") - file.Get("/", fiberpaginate.New(), fileController.GetFiles) + file.Get("/", fileParams.UtilityMiddleware.Paginator, fileController.GetFiles) file.Get("/:fileID", fileController.GetFile) file.Post("/", fileController.CreateFile) file.Delete("/:fileID", fileController.DeleteFile) diff --git a/backend/entities/pocs/base/routes.go b/backend/entities/pocs/base/routes.go index 9ec008cab..907459002 100644 --- a/backend/entities/pocs/base/routes.go +++ b/backend/entities/pocs/base/routes.go @@ -2,7 +2,6 @@ package base import ( "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" ) func PointOfContact(pointOfContactParams types.RouteParams) { @@ -11,7 +10,7 @@ func PointOfContact(pointOfContactParams types.RouteParams) { // api/v1/pocs/* pointofContact := pointOfContactParams.Router.Group("/pocs") - pointofContact.Get("/", fiberpaginate.New(), pointOfContactController.GetPointOfContacts) + pointofContact.Get("/", pointOfContactParams.UtilityMiddleware.Paginator, pointOfContactController.GetPointOfContacts) pointofContact.Get("/:pocID", pointOfContactController.GetPointOfContact) // pointOfContact.Get("/:pocID/file", pointOfContactController.GetPointOfContacFileInfo)) } diff --git a/backend/entities/users/base/routes.go b/backend/entities/users/base/routes.go index 8c4753cf0..898a981ba 100644 --- a/backend/entities/users/base/routes.go +++ b/backend/entities/users/base/routes.go @@ -6,7 +6,6 @@ import ( "github.com/GenerateNU/sac/backend/entities/users/members" "github.com/GenerateNU/sac/backend/entities/users/tags" "github.com/GenerateNU/sac/backend/types" - "github.com/garrettladley/fiberpaginate" "github.com/gofiber/fiber/v2" ) @@ -27,7 +26,7 @@ func UsersRouter(userParams types.RouteParams) fiber.Router { // api/v1/users/* users := userParams.Router.Group("/users") - users.Get("/", userParams.AuthMiddleware.Authorize(p.ReadAll), fiberpaginate.New(), userController.GetUsers) + users.Get("/", userParams.AuthMiddleware.Authorize(p.ReadAll), userParams.UtilityMiddleware.Paginator, userController.GetUsers) users.Get("/me", userParams.AuthMiddleware.Authorize(p.UserRead), userController.GetMe) // api/v1/users/:userID/* diff --git a/backend/middleware/auth.go b/backend/middleware/auth/auth.go similarity index 78% rename from backend/middleware/auth.go rename to backend/middleware/auth/auth.go index 7538efdd3..1a0962766 100644 --- a/backend/middleware/auth.go +++ b/backend/middleware/auth/auth.go @@ -1,11 +1,8 @@ -package middleware +package auth import ( - "fmt" - "net/http" "slices" "strings" - "time" "github.com/GenerateNU/sac/backend/auth" "github.com/GenerateNU/sac/backend/utilities" @@ -15,7 +12,6 @@ import ( "github.com/golang-jwt/jwt" "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/limiter" ) func (m *AuthMiddlewareService) IsSuper(c *fiber.Ctx) bool { @@ -102,19 +98,3 @@ func (m *AuthMiddlewareService) Authorize(requiredPermissions ...auth.Permission return c.Next() } } - -// TODO: implement rate limiting with redis -func (m *AuthMiddlewareService) Limiter(rate int, expiration time.Duration) func(c *fiber.Ctx) error { - return limiter.New(limiter.Config{ - Max: rate, - Expiration: expiration, - KeyGenerator: func(c *fiber.Ctx) string { - return fmt.Sprintf("%s-%s", c.IP(), c.Path()) - }, - LimitReached: func(c *fiber.Ctx) error { - return c.Status(http.StatusTooManyRequests).JSON(fiber.Map{ - "error": "Too many requests", - }) - }, - }) -} diff --git a/backend/middleware/club.go b/backend/middleware/auth/club.go similarity index 98% rename from backend/middleware/club.go rename to backend/middleware/auth/club.go index 4c9560696..5a084fd7b 100644 --- a/backend/middleware/club.go +++ b/backend/middleware/auth/club.go @@ -1,4 +1,4 @@ -package middleware +package auth import ( "slices" diff --git a/backend/middleware/event.go b/backend/middleware/auth/event.go similarity index 98% rename from backend/middleware/event.go rename to backend/middleware/auth/event.go index b8a371fa5..542f6e7c2 100644 --- a/backend/middleware/event.go +++ b/backend/middleware/auth/event.go @@ -1,4 +1,4 @@ -package middleware +package auth import ( "slices" diff --git a/backend/middleware/extractor.go b/backend/middleware/auth/extractor.go similarity index 98% rename from backend/middleware/extractor.go rename to backend/middleware/auth/extractor.go index a9b4bcc37..8b10c29f5 100644 --- a/backend/middleware/extractor.go +++ b/backend/middleware/auth/extractor.go @@ -1,4 +1,4 @@ -package middleware +package auth import ( "errors" diff --git a/backend/middleware/middleware.go b/backend/middleware/auth/middleware.go similarity index 63% rename from backend/middleware/middleware.go rename to backend/middleware/auth/middleware.go index 5d3e6aabe..97b3cafa5 100644 --- a/backend/middleware/middleware.go +++ b/backend/middleware/auth/middleware.go @@ -1,8 +1,6 @@ -package middleware +package auth import ( - "time" - "github.com/GenerateNU/sac/backend/auth" "github.com/GenerateNU/sac/backend/config" "github.com/go-playground/validator/v10" @@ -14,10 +12,8 @@ type AuthMiddlewareInterface interface { ClubAuthorizeById(c *fiber.Ctx) error UserAuthorizeById(c *fiber.Ctx) error Authenticate(c *fiber.Ctx) error - Authorize(requiredPermissions ...auth.Permission) func(c *fiber.Ctx) error - Skip(h fiber.Handler) fiber.Handler + Authorize(requiredPermissions ...auth.Permission) fiber.Handler IsSuper(c *fiber.Ctx) bool - Limiter(rate int, duration time.Duration) func(c *fiber.Ctx) error } type AuthMiddlewareService struct { @@ -26,7 +22,7 @@ type AuthMiddlewareService struct { Auth config.AuthSettings } -func NewAuthMiddlewareService(db *gorm.DB, validate *validator.Validate, authSettings config.AuthSettings) *AuthMiddlewareService { +func New(db *gorm.DB, validate *validator.Validate, authSettings config.AuthSettings) *AuthMiddlewareService { return &AuthMiddlewareService{ DB: db, Validate: validate, diff --git a/backend/middleware/user.go b/backend/middleware/auth/user.go similarity index 97% rename from backend/middleware/user.go rename to backend/middleware/auth/user.go index 62b040bd9..bf489faa2 100644 --- a/backend/middleware/user.go +++ b/backend/middleware/auth/user.go @@ -1,4 +1,4 @@ -package middleware +package auth import ( "github.com/GenerateNU/sac/backend/auth" diff --git a/backend/middleware/utility/limiter.go b/backend/middleware/utility/limiter.go new file mode 100644 index 000000000..dc6ed2887 --- /dev/null +++ b/backend/middleware/utility/limiter.go @@ -0,0 +1,24 @@ +package utility + +import ( + "errors" + "fmt" + "time" + + "github.com/GenerateNU/sac/backend/utilities" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/limiter" +) + +func (u *UtilityMiddlewareService) Limiter(rate int, expiration time.Duration) func(c *fiber.Ctx) error { + return limiter.New(limiter.Config{ + Max: rate, + Expiration: expiration, + KeyGenerator: func(c *fiber.Ctx) string { + return fmt.Sprintf("%s-%s", c.IP(), c.Path()) + }, + LimitReached: func(c *fiber.Ctx) error { + return utilities.NewAPIError(fiber.StatusTooManyRequests, errors.New("too many requests")) + }, + }) +} diff --git a/backend/middleware/utility/middleware.go b/backend/middleware/utility/middleware.go new file mode 100644 index 000000000..cd940ea93 --- /dev/null +++ b/backend/middleware/utility/middleware.go @@ -0,0 +1,22 @@ +package utility + +import ( + "time" + + "github.com/gofiber/fiber/v2" +) + +type UtilityMiddlewareInterface interface { + Paginator(c *fiber.Ctx) error + Limiter(rate int, duration time.Duration) fiber.Handler +} + +type UtilityMiddlewareService struct { + paginator fiber.Handler +} + +func New(paginator fiber.Handler) *UtilityMiddlewareService { + return &UtilityMiddlewareService{ + paginator: paginator, + } +} diff --git a/backend/middleware/utility/paginator.go b/backend/middleware/utility/paginator.go new file mode 100644 index 000000000..0e2827fda --- /dev/null +++ b/backend/middleware/utility/paginator.go @@ -0,0 +1,9 @@ +package utility + +import ( + "github.com/gofiber/fiber/v2" +) + +func (u *UtilityMiddlewareService) Paginator(c *fiber.Ctx) error { + return u.paginator(c) +} diff --git a/backend/server/server.go b/backend/server/server.go index 2c20e81c2..fcd95b3d6 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -3,6 +3,7 @@ package server import ( "fmt" + "github.com/garrettladley/fiberpaginate" go_json "github.com/goccy/go-json" authenticator "github.com/GenerateNU/sac/backend/auth" @@ -17,7 +18,8 @@ import ( tags "github.com/GenerateNU/sac/backend/entities/tags/base" users "github.com/GenerateNU/sac/backend/entities/users/base" "github.com/GenerateNU/sac/backend/integrations" - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" + utilityMiddleware "github.com/GenerateNU/sac/backend/middleware/utility" "github.com/GenerateNU/sac/backend/types" "github.com/GenerateNU/sac/backend/utilities" @@ -50,22 +52,24 @@ func Init(db *gorm.DB, integrations integrations.Integrations, settings config.S } jwt := authenticator.NewJWTClient(settings.Auth, jwt.SigningMethodHS256) - authMiddleware := middleware.NewAuthMiddlewareService(db, validate, settings.Auth) + authMiddleware := authMiddleware.New(db, validate, settings.Auth) + utilityMiddleware := utilityMiddleware.New(fiberpaginate.New()) apiv1 := app.Group("/api/v1") - routeParams := types.RouteParams{ - Router: apiv1, - AuthMiddleware: authMiddleware, - ServiceParams: types.ServiceParams{ - DB: db, - Validate: validate, - Auth: &settings.Auth, - JWT: jwt, - Integrations: integrations, - Calendar: &settings.Calendar, - }, - } + routeParams := types.NewRouteParams( + apiv1, + authMiddleware, + utilityMiddleware, + types.NewServiceParams( + db, + validate, + &settings.Auth, + jwt, + &settings.Calendar, + integrations, + ), + ) allRoutes(app, routeParams) diff --git a/backend/types/params.go b/backend/types/params.go index 6f4551524..47a65a348 100644 --- a/backend/types/params.go +++ b/backend/types/params.go @@ -4,16 +4,27 @@ import ( "github.com/GenerateNU/sac/backend/auth" "github.com/GenerateNU/sac/backend/config" "github.com/GenerateNU/sac/backend/integrations" - "github.com/GenerateNU/sac/backend/middleware" + authMiddleware "github.com/GenerateNU/sac/backend/middleware/auth" + utilityMiddleware "github.com/GenerateNU/sac/backend/middleware/utility" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" "gorm.io/gorm" ) type RouteParams struct { - Router fiber.Router - AuthMiddleware *middleware.AuthMiddlewareService - ServiceParams ServiceParams + Router fiber.Router + AuthMiddleware *authMiddleware.AuthMiddlewareService + UtilityMiddleware *utilityMiddleware.UtilityMiddlewareService + ServiceParams ServiceParams +} + +func NewRouteParams(router fiber.Router, authMiddleware *authMiddleware.AuthMiddlewareService, utilityMiddleware *utilityMiddleware.UtilityMiddlewareService, serviceParams ServiceParams) RouteParams { + return RouteParams{ + Router: router, + AuthMiddleware: authMiddleware, + UtilityMiddleware: utilityMiddleware, + ServiceParams: serviceParams, + } } type ServiceParams struct { @@ -24,3 +35,14 @@ type ServiceParams struct { Calendar *config.CalendarSettings Integrations integrations.Integrations } + +func NewServiceParams(db *gorm.DB, validate *validator.Validate, auth *config.AuthSettings, jwt auth.JWTClientInterface, calendar *config.CalendarSettings, integrations integrations.Integrations) ServiceParams { + return ServiceParams{ + DB: db, + Validate: validate, + Auth: auth, + JWT: jwt, + Calendar: calendar, + Integrations: integrations, + } +}