1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-02-09 13:36:57 +02:00
kratos/transport/http/context.go
Tony Chen 496edc6fb1
Http/refactor register service (#734)
* refactor http handle
2021-03-03 22:22:59 +08:00

44 lines
1.1 KiB
Go

package http
import (
"context"
"net/http"
)
// 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
}