all repos

onasty @ 6e284908136d4766eeaf8cd85a6a2b394764af18

a one-time notes service

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

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