all repos

onasty @ 51d3b53b0c8469cfd0ca12bb2a505d7fe77d228e

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
fix: oauth state (#128)..., 12 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("/change-password", a.changePasswordHandler)
53
		}
54
	}
55
56
	note := r.Group("/note")
57
	{
58
		note.GET("/:slug", a.getNoteBySlugHandler)
59
60
		possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware)
61
		{
62
			possiblyAuthorized.POST("", a.createNoteHandler)
63
		}
64
65
		authorized := note.Group("", a.authorizedMiddleware)
66
		{
67
			authorized.GET("", a.getNotesHandler)
68
			authorized.PATCH(":slug/expires", a.updateNoteHandler)
69
			authorized.PATCH(":slug/password", a.setNotePasswordHandler)
70
			authorized.DELETE(":slug", a.deleteNoteHandler)
71
		}
72
	}
73
}