onasty/internal/transport/http/apiv1/apiv1.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com feat: add oauth2 login for google and github (#109)..., 1 year ago
ss2316544@gmail.com feat: add oauth2 login for google and github (#109)..., 1 year ago
| 1 | package apiv1 |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gin-gonic/gin" |
| 5 | "github.com/olexsmir/onasty/internal/service/notesrv" |
| 6 | "github.com/olexsmir/onasty/internal/service/usersrv" |
| 7 | ) |
| 8 | |
| 9 | type APIV1 struct { |
| 10 | usersrv usersrv.UserServicer |
| 11 | notesrv notesrv.NoteServicer |
| 12 | } |
| 13 | |
| 14 | func NewAPIV1( |
| 15 | us usersrv.UserServicer, |
| 16 | ns notesrv.NoteServicer, |
| 17 | ) *APIV1 { |
| 18 | return &APIV1{ |
| 19 | usersrv: us, |
| 20 | notesrv: ns, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (a *APIV1) Routes(r *gin.RouterGroup) { |
| 25 | r.Use(a.metricsMiddleware) |
| 26 | auth := r.Group("/auth") |
| 27 | { |
| 28 | auth.POST("/signup", a.signUpHandler) |
| 29 | auth.POST("/signin", a.signInHandler) |
| 30 | auth.POST("/refresh-tokens", a.refreshTokensHandler) |
| 31 | auth.GET("/verify/:token", a.verifyHandler) |
| 32 | auth.POST("/resend-verification-email", a.resendVerificationEmailHandler) |
| 33 | |
| 34 | authorized := auth.Group("/", a.authorizedMiddleware) |
| 35 | { |
| 36 | authorized.POST("/logout", a.logOutHandler) |
| 37 | authorized.POST("/change-password", a.changePasswordHandler) |
| 38 | } |
| 39 | |
| 40 | oauth := r.Group("/oauth") |
| 41 | { |
| 42 | oauth.GET("/:provider", a.oauthLoginHandler) |
| 43 | oauth.GET("/:provider/callback", a.oauthCallbackHandler) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | note := r.Group("/note", a.couldBeAuthorizedMiddleware) |
| 48 | { |
| 49 | note.GET("/:slug", a.getNoteBySlugHandler) |
| 50 | note.POST("", a.createNoteHandler) |
| 51 | } |
| 52 | } |