Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use context.Context #855

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions backend/constants/timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package constants

import "time"

const (
HANDLER_TIMEOUT = 1 * time.Second
QUERY_TIMEOUT = 250 * time.Millisecond
THIRD_PARTY_TIMEOUT = 500 * time.Millisecond
)
46 changes: 36 additions & 10 deletions backend/entities/auth/base/controller.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package base

import (
"context"
"net/http"

"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/constants"
authEntities "github.com/GenerateNU/sac/backend/entities/auth"
users "github.com/GenerateNU/sac/backend/entities/users/base"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
)

type AuthController struct {
authService AuthServiceInterface
authService AuthServicer
}

func NewAuthController(authService AuthServiceInterface) *AuthController {
func NewAuthController(authService AuthServicer) *AuthController {
return &AuthController{authService: authService}
}

Expand All @@ -39,7 +41,10 @@ func (a *AuthController) Login(c *fiber.Ctx) error {
return utilities.InvalidJSON()
}

user, tokens, err := a.authService.Login(userBody)
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

user, tokens, err := a.authService.Login(ctx, userBody)
if err != nil {
return err
}
Expand Down Expand Up @@ -73,7 +78,10 @@ func (a *AuthController) Register(c *fiber.Ctx) error {
return utilities.InvalidJSON()
}

user, tokens, err := a.authService.Register(userBody)
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

user, tokens, err := a.authService.Register(ctx, userBody)
if err != nil {
return err
}
Expand Down Expand Up @@ -106,7 +114,10 @@ func (a *AuthController) Refresh(c *fiber.Ctx) error {
return utilities.InvalidCookies()
}

tokens, err := a.authService.Refresh(refreshBody.RefreshToken)
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

tokens, err := a.authService.Refresh(ctx, refreshBody.RefreshToken)
if err != nil {
return err
}
Expand All @@ -130,7 +141,10 @@ func (a *AuthController) Refresh(c *fiber.Ctx) error {
// @Success 200 {object} utilities.SuccessResponse
// @Router /auth/logout [post]
func (a *AuthController) Logout(c *fiber.Ctx) error {
err := a.authService.Logout(c)
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

err := a.authService.Logout(ctx, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +173,10 @@ func (a *AuthController) ForgotPassword(c *fiber.Ctx) error {
return utilities.InvalidJSON()
}

if err := a.authService.ForgotPassword(emailBody.Email); err != nil {
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

if err := a.authService.ForgotPassword(ctx, emailBody.Email); err != nil {
return err
}

Expand Down Expand Up @@ -188,7 +205,10 @@ func (a *AuthController) VerifyPasswordResetToken(c *fiber.Ctx) error {
return utilities.InvalidJSON()
}

if err := a.authService.VerifyPasswordResetToken(tokenBody); err != nil {
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

if err := a.authService.VerifyPasswordResetToken(ctx, tokenBody); err != nil {
return err
}

Expand Down Expand Up @@ -216,7 +236,10 @@ func (a *AuthController) SendCode(c *fiber.Ctx) error {
return utilities.InvalidJSON()
}

if err := a.authService.SendCode(emailBody.Email); err != nil {
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

if err := a.authService.SendCode(ctx, emailBody.Email); err != nil {
return err
}

Expand Down Expand Up @@ -244,7 +267,10 @@ func (a *AuthController) VerifyEmail(c *fiber.Ctx) error {
return utilities.InvalidJSON()
}

if err := a.authService.VerifyEmail(tokenBody); err != nil {
ctx, cancel := context.WithTimeout(c.Context(), constants.HANDLER_TIMEOUT)
defer cancel()

if err := a.authService.VerifyEmail(ctx, tokenBody); err != nil {
return err
}

Expand Down
Loading
Loading