all repos

onasty @ 62e4dde75f5cca100c807673176826e5f120c86d

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat(api): get note metadata (#148)..., 11 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
)
9
10
type APIV1 struct {
11
	usersrv usersrv.UserServicer
12
	notesrv notesrv.NoteServicer
13
	env     config.Environment
14
	domain  string
15
}
16
17
func NewAPIV1(
18
	us usersrv.UserServicer,
19
	ns notesrv.NoteServicer,
20
	env config.Environment,
21
	domain string,
22
) *APIV1 {
23
	return &APIV1{
24
		usersrv: us,
25
		notesrv: ns,
26
		env:     env,
27
		domain:  domain,
28
	}
29
}
30
31
func (a *APIV1) Routes(r *gin.RouterGroup) {
32
	r.Use(a.metricsMiddleware)
33
	auth := r.Group("/auth")
34
	{
35
		auth.POST("/signup", a.signUpHandler)
36
		auth.POST("/signin", a.signInHandler)
37
		auth.POST("/refresh-tokens", a.refreshTokensHandler)
38
		auth.GET("/verify/:token", a.verifyHandler)
39
		auth.POST("/resend-verification-email", a.resendVerificationEmailHandler)
40
		auth.POST("/reset-password", a.requestResetPasswordHandler)
41
		auth.POST("/reset-password/:token", a.resetPasswordHandler)
42
43
		oauth := r.Group("/oauth")
44
		{
45
			oauth.GET("/:provider", a.oauthLoginHandler)
46
			oauth.GET("/:provider/callback", a.oauthCallbackHandler)
47
		}
48
49
		authorized := auth.Group("/", a.authorizedMiddleware)
50
		{
51
			authorized.POST("/logout", a.logOutHandler)
52
			authorized.POST("/logout/all", a.logOutAllHandler)
53
			authorized.POST("/change-password", a.changePasswordHandler)
54
		}
55
	}
56
57
	r.GET("/me", a.authorizedMiddleware, a.getMeHandler)
58
59
	note := r.Group("/note")
60
	{
61
		note.GET("/:slug", a.getNoteBySlugHandler)
62
		note.GET("/:slug/meta", a.getNoteMetadataByIDHandler)
63
64
		possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware)
65
		{
66
			possiblyAuthorized.POST("", a.createNoteHandler)
67
		}
68
69
		authorized := note.Group("", a.authorizedMiddleware)
70
		{
71
			authorized.GET("", a.getNotesHandler)
72
			authorized.PATCH(":slug/expires", a.updateNoteHandler)
73
			authorized.PATCH(":slug/password", a.setNotePasswordHandler)
74
			authorized.DELETE(":slug", a.deleteNoteHandler)
75
		}
76
	}
77
}