onasty/internal/transport/http/apiv1/apiv1.go (view raw)
Olexandr Smirnov
Olexandr Smirnov
ss2316544@gmail.com fix: use separate ratelimiter for some endpoints (#166)..., 10 months ago
ss2316544@gmail.com fix: use separate ratelimiter for some endpoints (#166)..., 10 months ago
| 1 | package apiv1 |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gin-gonic/gin" |
| 5 | "github.com/olexsmir/onasty/internal/config" |
| 6 | "github.com/olexsmir/onasty/internal/service/notesrv" |
| 7 | "github.com/olexsmir/onasty/internal/service/usersrv" |
| 8 | "github.com/olexsmir/onasty/internal/transport/http/ratelimit" |
| 9 | ) |
| 10 | |
| 11 | type APIV1 struct { |
| 12 | usersrv usersrv.UserServicer |
| 13 | notesrv notesrv.NoteServicer |
| 14 | slowRatelimitCfg ratelimit.Config |
| 15 | env config.Environment |
| 16 | domain string |
| 17 | } |
| 18 | |
| 19 | func NewAPIV1( |
| 20 | us usersrv.UserServicer, |
| 21 | ns notesrv.NoteServicer, |
| 22 | slowRatelimitCfg ratelimit.Config, |
| 23 | env config.Environment, |
| 24 | domain string, |
| 25 | ) *APIV1 { |
| 26 | return &APIV1{ |
| 27 | usersrv: us, |
| 28 | notesrv: ns, |
| 29 | slowRatelimitCfg: slowRatelimitCfg, |
| 30 | env: env, |
| 31 | domain: domain, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func (a *APIV1) Routes(r *gin.RouterGroup) { |
| 36 | r.Use(a.metricsMiddleware) |
| 37 | auth := r.Group("/auth") |
| 38 | { |
| 39 | auth.POST("/signup", a.signUpHandler) |
| 40 | auth.POST("/signin", a.signInHandler) |
| 41 | auth.POST("/refresh-tokens", a.refreshTokensHandler) |
| 42 | auth.GET("/verify/:token", a.verifyHandler) |
| 43 | auth.POST("/resend-verification-email", a.slowRateLimit(), a.resendVerificationEmailHandler) |
| 44 | auth.POST("/reset-password", a.slowRateLimit(), a.requestResetPasswordHandler) |
| 45 | auth.POST("/reset-password/:token", a.resetPasswordHandler) |
| 46 | |
| 47 | oauth := r.Group("/oauth") |
| 48 | { |
| 49 | oauth.GET("/:provider", a.oauthLoginHandler) |
| 50 | oauth.GET("/:provider/callback", a.oauthCallbackHandler) |
| 51 | } |
| 52 | |
| 53 | authorized := auth.Group("/", a.authorizedMiddleware) |
| 54 | { |
| 55 | authorized.POST("/logout", a.logOutHandler) |
| 56 | authorized.POST("/logout/all", a.logOutAllHandler) |
| 57 | authorized.POST("/change-password", a.changePasswordHandler) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | r.GET("/me", a.authorizedMiddleware, a.getMeHandler) |
| 62 | |
| 63 | note := r.Group("/note") |
| 64 | { |
| 65 | note.GET("/:slug", a.getNoteBySlugHandler) |
| 66 | note.GET("/:slug/meta", a.getNoteMetadataByIDHandler) |
| 67 | |
| 68 | possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware) |
| 69 | { |
| 70 | possiblyAuthorized.POST("", a.createNoteHandler) |
| 71 | } |
| 72 | |
| 73 | authorized := note.Group("", a.authorizedMiddleware) |
| 74 | { |
| 75 | authorized.GET("", a.getNotesHandler) |
| 76 | authorized.PATCH(":slug/expires", a.updateNoteHandler) |
| 77 | authorized.PATCH(":slug/password", a.setNotePasswordHandler) |
| 78 | authorized.DELETE(":slug", a.deleteNoteHandler) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func (a *APIV1) slowRateLimit() gin.HandlerFunc { |
| 84 | return ratelimit.MiddlewareWithConfig(a.slowRatelimitCfg) |
| 85 | } |