From e8db174d3e464a5764a9f4bfd82172261bd50519 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 21 Feb 2026 10:29:21 +0900 Subject: refactor(api): separate business logic into game, tournament, session packages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- backend/api/auth_middleware.go | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) (limited to 'backend/api/auth_middleware.go') 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 -} -- cgit v1.3.1