You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
package http
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// ServerInfo is HTTP server infomation.
|
|
type ServerInfo struct {
|
|
Request *http.Request
|
|
Response http.ResponseWriter
|
|
}
|
|
|
|
type serverKey struct{}
|
|
|
|
// NewServerContext returns a new Context that carries value.
|
|
func NewServerContext(ctx context.Context, info ServerInfo) context.Context {
|
|
return context.WithValue(ctx, serverKey{}, info)
|
|
}
|
|
|
|
// FromServerContext returns the Transport value stored in ctx, if any.
|
|
func FromServerContext(ctx context.Context) (info ServerInfo, ok bool) {
|
|
info, ok = ctx.Value(serverKey{}).(ServerInfo)
|
|
return
|
|
}
|
|
|
|
// ClientInfo is HTTP client infomation.
|
|
type ClientInfo struct {
|
|
Request *http.Request
|
|
}
|
|
|
|
type clientKey struct{}
|
|
|
|
// NewClientContext returns a new Context that carries value.
|
|
func NewClientContext(ctx context.Context, info ClientInfo) context.Context {
|
|
return context.WithValue(ctx, clientKey{}, info)
|
|
}
|
|
|
|
// FromClientContext returns the Transport value stored in ctx, if any.
|
|
func FromClientContext(ctx context.Context) (info ClientInfo, ok bool) {
|
|
info, ok = ctx.Value(clientKey{}).(ClientInfo)
|
|
return
|
|
}
|
|
|
|
// Vars returns the route variables for the current request, if any.
|
|
func Vars(req *http.Request) map[string]string {
|
|
return mux.Vars(req)
|
|
}
|
|
|