2021-04-18 16:14:30 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-06-19 22:46:04 +00:00
|
|
|
"context"
|
2021-04-18 16:14:30 +00:00
|
|
|
"net/http"
|
2021-06-19 22:46:04 +00:00
|
|
|
|
2021-06-19 22:46:42 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/ctx"
|
2021-06-19 22:46:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Context is a simple wrapper around context.Context that adds helper methods
|
|
|
|
// to access additional information
|
|
|
|
type Context struct{ context.Context }
|
|
|
|
|
|
|
|
// Request current request
|
|
|
|
func (c Context) Request() *http.Request {
|
|
|
|
var (
|
2021-06-19 22:46:42 +00:00
|
|
|
raw = c.Value(ctx.Request)
|
2021-06-19 22:46:04 +00:00
|
|
|
cast, ok = raw.(*http.Request)
|
|
|
|
)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cast
|
|
|
|
}
|
|
|
|
|
2021-06-19 22:46:42 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-06-19 22:46:04 +00:00
|
|
|
// Auth associated with this request
|
|
|
|
func (c Context) Auth() *Auth {
|
|
|
|
var (
|
2021-06-19 22:46:42 +00:00
|
|
|
raw = c.Value(ctx.Auth)
|
2021-06-19 22:46:04 +00:00
|
|
|
cast, ok = raw.(*Auth)
|
|
|
|
)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cast
|
2021-04-18 16:14:30 +00:00
|
|
|
}
|