From ed404106f2cd9e9e5d220e5304d96a2727217ec3 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 20 Jun 2021 00:46:42 +0200 Subject: [PATCH] refactor: rename api.Ctx to api.Context, extends context.Context with helper methods --- api/context.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/api/context.go b/api/context.go index f265f2a..c5f9a9f 100644 --- a/api/context.go +++ b/api/context.go @@ -3,14 +3,8 @@ package api import ( "context" "net/http" -) -// custom context key type -type ctxKey int - -const ( - ctxRequest ctxKey = iota - ctxAuth + "git.xdrm.io/go/aicra/internal/ctx" ) // Context is a simple wrapper around context.Context that adds helper methods @@ -20,7 +14,7 @@ type Context struct{ context.Context } // Request current request func (c Context) Request() *http.Request { var ( - raw = c.Value(ctxRequest) + raw = c.Value(ctx.Request) cast, ok = raw.(*http.Request) ) if !ok { @@ -29,10 +23,22 @@ func (c Context) Request() *http.Request { return cast } +// ResponseWriter for this request +func (c Context) ResponseWriter() http.ResponseWriter { + var ( + raw = c.Value(ctx.Response) + cast, ok = raw.(http.ResponseWriter) + ) + if !ok { + return nil + } + return cast +} + // Auth associated with this request func (c Context) Auth() *Auth { var ( - raw = c.Value(ctxAuth) + raw = c.Value(ctx.Auth) cast, ok = raw.(*Auth) ) if !ok {