all repos

onasty @ bc92b8268dc567b48d4d567ffdfefb0442152335

a one-time notes service

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

Olexandr Smirnov Olexandr Smirnov
ss2316544@gmail.com
feat: get all read and unread notes (#188)..., 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/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
38
	r.GET("/me", a.authorizedMiddleware, a.getMeHandler)
39
40
	auth := r.Group("/auth")
41
	{
42
		auth.POST("/signup", a.signUpHandler)
43
		auth.POST("/signin", a.signInHandler)
44
		auth.POST("/refresh-tokens", a.refreshTokensHandler)
45
		auth.GET("/verify/:token", a.verifyHandler)
46
		auth.POST("/resend-verification-email", a.slowRateLimit(), a.resendVerificationEmailHandler)
47
		auth.POST("/reset-password", a.slowRateLimit(), a.requestResetPasswordHandler)
48
		auth.POST("/reset-password/:token", a.resetPasswordHandler)
49
50
		oauth := r.Group("/oauth")
51
		{
52
			oauth.GET("/:provider", a.oauthLoginHandler)
53
			oauth.GET("/:provider/callback", a.oauthCallbackHandler)
54
		}
55
56
		authorized := auth.Group("/", a.authorizedMiddleware)
57
		{
58
			authorized.POST("/logout", a.logOutHandler)
59
			authorized.POST("/logout/all", a.logOutAllHandler)
60
			authorized.POST("/change-password", a.changePasswordHandler)
61
		}
62
	}
63
64
	note := r.Group("/note")
65
	{
66
		note.GET("/:slug", a.getNoteBySlugHandler)
67
		note.POST("/:slug/view", a.getNoteBySlugAndPasswordHandler)
68
		note.GET("/:slug/meta", a.getNoteMetadataByIDHandler)
69
70
		possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware)
71
		{
72
			possiblyAuthorized.POST("", a.createNoteHandler)
73
		}
74
75
		authorized := note.Group("", a.authorizedMiddleware)
76
		{
77
			authorized.GET("", a.getNotesHandler)
78
79
			// FIXME: those links make slugs `read` and `unread` unavailable
80
			authorized.GET("/read", a.getReadNotesHandler)
81
			authorized.GET("/unread", a.getUnReadNotesHandler)
82
83
			authorized.PATCH(":slug/expires", a.updateNoteHandler)
84
			authorized.PATCH(":slug/password", a.setNotePasswordHandler)
85
			authorized.DELETE(":slug", a.deleteNoteHandler)
86
		}
87
	}
88
}
89
90
func (a *APIV1) slowRateLimit() gin.HandlerFunc {
91
	return ratelimit.MiddlewareWithConfig(a.slowRatelimitCfg)
92
}