diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-21 10:29:21 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-21 10:29:21 +0900 |
| commit | e8db174d3e464a5764a9f4bfd82172261bd50519 (patch) | |
| tree | 68cb8f0713fcc1f960a650d879232cb4c20ca6cd /backend/api/auth_middleware.go | |
| parent | 1be106ac53caa019a8912af932a43570fa8c052d (diff) | |
| download | phperkaigi-2026-albatross-e8db174d3e464a5764a9f4bfd82172261bd50519.tar.gz phperkaigi-2026-albatross-e8db174d3e464a5764a9f4bfd82172261bd50519.tar.zst phperkaigi-2026-albatross-e8db174d3e464a5764a9f4bfd82172261bd50519.zip | |
refactor(api): separate business logic into game, tournament, session packages
Extract business logic from api/handler.go into dedicated service packages:
- session: context helpers (resolves admin → api import dependency)
- game: game state, code submission, ranking, watch logic
- tournament: bracket construction and seed ordering
- api/convert.go: domain → API type conversion functions
api/handler.go is now a thin adapter that delegates to services and
maps domain errors to HTTP status codes.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'backend/api/auth_middleware.go')
| -rw-r--r-- | backend/api/auth_middleware.go | 34 |
1 files changed, 4 insertions, 30 deletions
diff --git a/backend/api/auth_middleware.go b/backend/api/auth_middleware.go index f2a3987..a588185 100644 --- a/backend/api/auth_middleware.go +++ b/backend/api/auth_middleware.go @@ -1,17 +1,13 @@ package api import ( - "context" - "github.com/labstack/echo/v4" "albatross-2026-backend/auth" "albatross-2026-backend/db" + "albatross-2026-backend/session" ) -type sessionIDContextKey struct{} -type userContextKey struct{} - func SessionCookieMiddleware(q db.Querier) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { @@ -25,31 +21,14 @@ func SessionCookieMiddleware(q db.Querier) echo.MiddlewareFunc { return next(c) } ctx := c.Request().Context() - ctx = context.WithValue(ctx, sessionIDContextKey{}, hashedID) - ctx = context.WithValue(ctx, userContextKey{}, &user) + ctx = session.SetSessionIDInContext(ctx, hashedID) + ctx = session.SetUserInContext(ctx, &user) c.SetRequest(c.Request().WithContext(ctx)) return next(c) } } } -func GetSessionIDFromContext(ctx context.Context) (string, bool) { - sessionID, ok := ctx.Value(sessionIDContextKey{}).(string) - return sessionID, ok -} - -func GetUserFromContext(ctx context.Context) (*db.User, bool) { - user, ok := ctx.Value(userContextKey{}).(*db.User) - return user, ok -} - -// SetUserInContext sets a user in the context. Intended for testing. -func SetUserInContext(ctx context.Context, user *db.User) context.Context { - return context.WithValue(ctx, userContextKey{}, user) -} - -type clientIPContextKey struct{} - // ClientIPMiddleware extracts the client IP from echo.Context.RealIP() // and stores it in the request's context.Context so that handlers // receiving only context.Context (via generated code) can access it. @@ -57,14 +36,9 @@ func ClientIPMiddleware() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { ip := c.RealIP() - ctx := context.WithValue(c.Request().Context(), clientIPContextKey{}, ip) + ctx := session.SetClientIPInContext(c.Request().Context(), ip) c.SetRequest(c.Request().WithContext(ctx)) return next(c) } } } - -func GetClientIPFromContext(ctx context.Context) string { - ip, _ := ctx.Value(clientIPContextKey{}).(string) - return ip -} |
