refactor/idiomatic-handlers-middlewares #25
|
@ -3,14 +3,8 @@ package api
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
|
||||||
|
|
||||||
// custom context key type
|
"git.xdrm.io/go/aicra/internal/ctx"
|
||||||
type ctxKey int
|
|
||||||
|
|
||||||
const (
|
|
||||||
ctxRequest ctxKey = iota
|
|
||||||
ctxAuth
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context is a simple wrapper around context.Context that adds helper methods
|
// Context is a simple wrapper around context.Context that adds helper methods
|
||||||
|
@ -20,7 +14,7 @@ type Context struct{ context.Context }
|
||||||
// Request current request
|
// Request current request
|
||||||
func (c Context) Request() *http.Request {
|
func (c Context) Request() *http.Request {
|
||||||
var (
|
var (
|
||||||
raw = c.Value(ctxRequest)
|
raw = c.Value(ctx.Request)
|
||||||
cast, ok = raw.(*http.Request)
|
cast, ok = raw.(*http.Request)
|
||||||
)
|
)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -29,10 +23,22 @@ func (c Context) Request() *http.Request {
|
||||||
return cast
|
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
|
// Auth associated with this request
|
||||||
func (c Context) Auth() *Auth {
|
func (c Context) Auth() *Auth {
|
||||||
var (
|
var (
|
||||||
raw = c.Value(ctxAuth)
|
raw = c.Value(ctx.Auth)
|
||||||
cast, ok = raw.(*Auth)
|
cast, ok = raw.(*Auth)
|
||||||
)
|
)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Loading…
Reference in New Issue