all repos

onasty @ 49ba283475c3e2ad4a464b4d2f923aad6b161b50

a one-time notes service

onasty/internal/transport/http/apiv1/apiv1.go (view raw)

Olexandr Smirnov Olexandr Smirnov
olexsmir@gmail.com
refactor(api): split `usersrv` responsibilities (#195)..., 9 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/authsrv"
7
	"github.com/olexsmir/onasty/internal/service/notesrv"
8
	"github.com/olexsmir/onasty/internal/service/usersrv"
9
	"github.com/olexsmir/onasty/internal/transport/http/ratelimit"
10
)
11
12
type APIV1 struct {
13
	authsrv          authsrv.AuthServicer
14
	usersrv          usersrv.UserServicer
15
	notesrv          notesrv.NoteServicer
16
	slowRatelimitCfg ratelimit.Config
17
	env              config.Environment
18
	domain           string
19
}
20
21
func NewAPIV1(
22
	as authsrv.AuthServicer,
23
	us usersrv.UserServicer,
24
	ns notesrv.NoteServicer,
25
	slowRatelimitCfg ratelimit.Config,
26
	env config.Environment,
27
	domain string,
28
) *APIV1 {
29
	return &APIV1{
30
		authsrv:          as,
31
		usersrv:          us,
32
		notesrv:          ns,
33
		slowRatelimitCfg: slowRatelimitCfg,
34
		env:              env,
35
		domain:           domain,
36
	}
37
}
38
39
func (a *APIV1) Routes(r *gin.RouterGroup) {
40
	r.Use(a.metricsMiddleware)
41
42
	r.GET("/me", a.authorizedMiddleware, a.getMeHandler)
43
44
	auth := r.Group("/auth")
45
	{
46
		auth.POST("/signup", a.signUpHandler)
47
		auth.POST("/signin", a.signInHandler)
48
		auth.POST("/refresh-tokens", a.refreshTokensHandler)
49
		auth.GET("/verify/:token", a.verifyHandler)
50
		auth.POST("/resend-verification-email", a.slowRateLimit(), a.resendVerificationEmailHandler)
51
		auth.POST("/reset-password", a.slowRateLimit(), a.requestResetPasswordHandler)
52
		auth.POST("/reset-password/:token", a.resetPasswordHandler)
53
54
		oauth := r.Group("/oauth")
55
		{
56
			oauth.GET("/:provider", a.oauthLoginHandler)
57
			oauth.GET("/:provider/callback", a.oauthCallbackHandler)
58
		}
59
60
		auth.GET("/change-email/:token", a.changeEmailHandler)
61
		authorized := auth.Group("/", a.authorizedMiddleware)
62
		{
63
			authorized.POST("/logout", a.logOutHandler)
64
			authorized.POST("/logout/all", a.logOutAllHandler)
65
			authorized.POST("/change-password", a.changePasswordHandler)
66
			authorized.POST("/change-email", a.requestEmailChangeHandler)
67
		}
68
	}
69
70
	note := r.Group("/note")
71
	{
72
		note.GET("/:slug", a.getNoteBySlugHandler)
73
		note.POST("/:slug/view", a.getNoteBySlugAndPasswordHandler)
74
		note.GET("/:slug/meta", a.getNoteMetadataByIDHandler)
75
76
		possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware)
77
		{
78
			possiblyAuthorized.POST("", a.createNoteHandler)
79
		}
80
81
		authorized := note.Group("", a.authorizedMiddleware)
82
		{
83
			authorized.GET("", a.getNotesHandler)
84
			authorized.GET("/read", a.getReadNotesHandler)
85
			authorized.GET("/unread", a.getUnReadNotesHandler)
86
			authorized.PATCH(":slug/expires", a.updateNoteHandler)
87
			authorized.PATCH(":slug/password", a.setNotePasswordHandler)
88
			authorized.DELETE(":slug", a.deleteNoteHandler)
89
		}
90
	}
91
}
92
93
func (a *APIV1) slowRateLimit() gin.HandlerFunc {
94
	return ratelimit.MiddlewareWithConfig(a.slowRatelimitCfg)
95
}