mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-07 23:02:12 +02:00
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
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)
|
|
}
|