all repos

onasty @ 11e5f7b

a one-time notes service
5 files changed, 33 insertions(+), 33 deletions(-)
refactor: endpoints should not be pointer receiver (#217)

Author: Oleksandr Smirnov olexsmir@gmail.com
Committed by: GitHub noreply@github.com
Committed at: 2025-09-30 19:08:52 +0300
Parent: ca622f3
M internal/transport/http/apiv1/apiv1.go

@@ -41,7 +41,7 @@ frontendURL: frontendURL,

} } -func (a *APIV1) Routes(r *gin.RouterGroup) { +func (a APIV1) Routes(r *gin.RouterGroup) { r.Use(a.metricsMiddleware) r.GET("/me", a.authorizedMiddleware, a.getMeHandler)

@@ -95,6 +95,6 @@ }

} } -func (a *APIV1) slowRateLimit() gin.HandlerFunc { +func (a APIV1) slowRateLimit() gin.HandlerFunc { return ratelimit.MiddlewareWithConfig(a.slowRatelimitCfg) }
M internal/transport/http/apiv1/auth.go

@@ -14,7 +14,7 @@ Email string `json:"email"`

Password string `json:"password"` } -func (a *APIV1) signUpHandler(c *gin.Context) { +func (a APIV1) signUpHandler(c *gin.Context) { var req signUpRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -44,7 +44,7 @@ AccessToken string `json:"access_token"`

RefreshToken string `json:"refresh_token"` } -func (a *APIV1) signInHandler(c *gin.Context) { +func (a APIV1) signInHandler(c *gin.Context) { var req signInRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -70,7 +70,7 @@ type refreshTokenRequest struct {

RefreshToken string `json:"refresh_token"` } -func (a *APIV1) refreshTokensHandler(c *gin.Context) { +func (a APIV1) refreshTokensHandler(c *gin.Context) { var req refreshTokenRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -93,7 +93,7 @@ type logoutRequest struct {

RefreshToken string `json:"refresh_token"` } -func (a *APIV1) logOutHandler(c *gin.Context) { +func (a APIV1) logOutHandler(c *gin.Context) { var req logoutRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -112,7 +112,7 @@

c.Status(http.StatusNoContent) } -func (a *APIV1) logOutAllHandler(c *gin.Context) { +func (a APIV1) logOutAllHandler(c *gin.Context) { if err := a.authsrv.LogoutAll(c.Request.Context(), a.getUserID(c)); err != nil { errorResponse(c, err) return

@@ -123,7 +123,7 @@ }

const oatuhStateCookie = "oauth_state" -func (a *APIV1) oauthLoginHandler(c *gin.Context) { +func (a APIV1) oauthLoginHandler(c *gin.Context) { redirectInfo, err := a.authsrv.GetOAuthURL(c.Param("provider")) if err != nil { errorResponse(c, err)

@@ -143,7 +143,7 @@

c.Redirect(http.StatusSeeOther, redirectInfo.URL) } -func (a *APIV1) oauthCallbackHandler(c *gin.Context) { +func (a APIV1) oauthCallbackHandler(c *gin.Context) { redURL, err := url.Parse(a.frontendURL + "/oauth/callback") if err != nil { errorResponse(c, err)

@@ -174,7 +174,7 @@

c.Redirect(http.StatusFound, redURL.String()) } -func (a *APIV1) oauthCallbackErrorResponse(c *gin.Context, u *url.URL) { +func (a APIV1) oauthCallbackErrorResponse(c *gin.Context, u *url.URL) { u.RawQuery = url.Values{"error": {"internal server error"}}.Encode() c.Redirect(http.StatusFound, u.String()) }
M internal/transport/http/apiv1/middleware.go

@@ -17,7 +17,7 @@ // authorizedMiddleware is a middleware that checks if user is authorized

// and if so sets user metadata to context // // being authorized is required for making the request for specific endpoint -func (a *APIV1) authorizedMiddleware(c *gin.Context) { +func (a APIV1) authorizedMiddleware(c *gin.Context) { token, ok := getTokenFromAuthHeaders(c) if !ok { errorResponse(c, ErrUnauthorized)

@@ -39,7 +39,7 @@ // couldBeAuthorizedMiddleware is a middleware that checks if user is authorized and

// if so sets user metadata to context // // it is NOT required to be authorized for making the request for specific endpoint -func (a *APIV1) couldBeAuthorizedMiddleware(c *gin.Context) { +func (a APIV1) couldBeAuthorizedMiddleware(c *gin.Context) { token, ok := getTokenFromAuthHeaders(c) if ok { uid, err := a.validateAuthorizedUser(c.Request.Context(), token)

@@ -54,7 +54,7 @@

c.Next() } -func (a *APIV1) metricsMiddleware(c *gin.Context) { +func (a APIV1) metricsMiddleware(c *gin.Context) { start := time.Now() c.Next() latency := time.Since(start)

@@ -92,7 +92,7 @@ // getUserId returns userId from the context

// getting user id is only possible if user is authorized // // if userID is not set, [uuid.Nil] will be returned. -func (a *APIV1) getUserID(c *gin.Context) uuid.UUID { +func (a APIV1) getUserID(c *gin.Context) uuid.UUID { userID, exists := c.Get(userIDCtxKey) if !exists { return uuid.Nil

@@ -106,7 +106,7 @@

return uid } -func (a *APIV1) validateAuthorizedUser(ctx context.Context, accessToken string) (uuid.UUID, error) { +func (a APIV1) validateAuthorizedUser(ctx context.Context, accessToken string) (uuid.UUID, error) { tokenPayload, err := a.authsrv.ParseJWTToken(accessToken) if err != nil { return uuid.Nil, err
M internal/transport/http/apiv1/note.go

@@ -21,7 +21,7 @@ type createNoteResponse struct {

Slug string `json:"slug"` } -func (a *APIV1) createNoteHandler(c *gin.Context) { +func (a APIV1) createNoteHandler(c *gin.Context) { var req createNoteRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -53,7 +53,7 @@ CreatedAt time.Time `json:"created_at"`

ExpiresAt time.Time `json:"expires_at,omitzero"` } -func (a *APIV1) getNoteBySlugHandler(c *gin.Context) { +func (a APIV1) getNoteBySlugHandler(c *gin.Context) { note, err := a.notesrv.GetBySlugAndRemoveIfNeeded( c.Request.Context(), notesrv.GetNoteBySlugInput{

@@ -84,7 +84,7 @@ type getNoteBuySlugAndPasswordRequest struct {

Password string `json:"password"` } -func (a *APIV1) getNoteBySlugAndPasswordHandler(c *gin.Context) { +func (a APIV1) getNoteBySlugAndPasswordHandler(c *gin.Context) { var req getNoteBuySlugAndPasswordRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -122,7 +122,7 @@ CreatedAt time.Time `json:"created_at"`

HasPassword bool `json:"has_password"` } -func (a *APIV1) getNoteMetadataByIDHandler(c *gin.Context) { +func (a APIV1) getNoteMetadataByIDHandler(c *gin.Context) { meta, err := a.notesrv.GetNoteMetadataBySlug(c.Request.Context(), c.Param("slug")) if err != nil { errorResponse(c, err)

@@ -145,7 +145,7 @@ ExpiresAt time.Time `json:"expires_at,omitzero"`

ReadAt time.Time `json:"read_at,omitzero"` } -func (a *APIV1) getNotesHandler(c *gin.Context) { +func (a APIV1) getNotesHandler(c *gin.Context) { notes, err := a.notesrv.GetAllByAuthorID(c.Request.Context(), a.getUserID(c)) if err != nil { errorResponse(c, err)

@@ -155,7 +155,7 @@

c.JSON(http.StatusOK, mapNotesDTOToResponse(notes)) } -func (a *APIV1) getReadNotesHandler(c *gin.Context) { +func (a APIV1) getReadNotesHandler(c *gin.Context) { notes, err := a.notesrv.GetAllReadByAuthorID(c.Request.Context(), a.getUserID(c)) if err != nil { errorResponse(c, err)

@@ -165,7 +165,7 @@

c.JSON(http.StatusOK, mapNotesDTOToResponse(notes)) } -func (a *APIV1) getUnReadNotesHandler(c *gin.Context) { +func (a APIV1) getUnReadNotesHandler(c *gin.Context) { notes, err := a.notesrv.GetAllUnreadByAuthorID(c.Request.Context(), a.getUserID(c)) if err != nil { errorResponse(c, err)

@@ -180,7 +180,7 @@ ExpiresAt *time.Time `json:"expires_at,omitempty"`

KeepBeforeExpiration *bool `json:"keep_before_expiration,omitempty"` } -func (a *APIV1) updateNoteHandler(c *gin.Context) { +func (a APIV1) updateNoteHandler(c *gin.Context) { var req updateNoteRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -203,7 +203,7 @@

c.Status(http.StatusOK) } -func (a *APIV1) deleteNoteHandler(c *gin.Context) { +func (a APIV1) deleteNoteHandler(c *gin.Context) { if err := a.notesrv.DeleteBySlug( c.Request.Context(), c.Param("slug"),

@@ -220,7 +220,7 @@ type setNotePasswordRequest struct {

Password string `json:"password"` } -func (a *APIV1) setNotePasswordHandler(c *gin.Context) { +func (a APIV1) setNotePasswordHandler(c *gin.Context) { var req setNotePasswordRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)
M internal/transport/http/apiv1/user.go

@@ -15,7 +15,7 @@ LastLoginAt time.Time `json:"last_login_at"`

NotesCreated int `json:"notes_created"` } -func (a *APIV1) getMeHandler(c *gin.Context) { +func (a APIV1) getMeHandler(c *gin.Context) { uinfo, err := a.usersrv.GetUserInfo(c.Request.Context(), a.getUserID(c)) if err != nil { errorResponse(c, err)

@@ -35,7 +35,7 @@ CurrentPassword string `json:"current_password"`

NewPassword string `json:"new_password"` } -func (a *APIV1) changePasswordHandler(c *gin.Context) { +func (a APIV1) changePasswordHandler(c *gin.Context) { var req changePasswordRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -60,7 +60,7 @@ type requestResetPasswordRequest struct {

Email string `json:"email"` } -func (a *APIV1) requestResetPasswordHandler(c *gin.Context) { +func (a APIV1) requestResetPasswordHandler(c *gin.Context) { var req requestResetPasswordRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -84,7 +84,7 @@ type resetPasswordRequest struct {

Password string `json:"password"` } -func (a *APIV1) resetPasswordHandler(c *gin.Context) { +func (a APIV1) resetPasswordHandler(c *gin.Context) { var req resetPasswordRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -109,7 +109,7 @@ type changeEmailRequest struct {

NewEmail string `json:"new_email"` } -func (a *APIV1) requestEmailChangeHandler(c *gin.Context) { +func (a APIV1) requestEmailChangeHandler(c *gin.Context) { var req changeEmailRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)

@@ -129,7 +129,7 @@

c.Status(http.StatusOK) } -func (a *APIV1) changeEmailHandler(c *gin.Context) { +func (a APIV1) changeEmailHandler(c *gin.Context) { if err := a.usersrv.ChangeEmail( c.Request.Context(), c.Param("token"),

@@ -141,7 +141,7 @@

c.String(http.StatusOK, "email changed") } -func (a *APIV1) verifyHandler(c *gin.Context) { +func (a APIV1) verifyHandler(c *gin.Context) { if err := a.usersrv.Verify( c.Request.Context(), c.Param("token"),

@@ -157,7 +157,7 @@ type resendVerificationEmailRequest struct {

Email string `json:"email"` } -func (a *APIV1) resendVerificationEmailHandler(c *gin.Context) { +func (a APIV1) resendVerificationEmailHandler(c *gin.Context) { var req resendVerificationEmailRequest if err := c.ShouldBindJSON(&req); err != nil { invalidRequest(c)