Documentation
¶
Index ¶
- Constants
- func BodyContentType(v any) string
- func BuildPath(pathTemplate string, msg any, opts ...BuildPathOption) string
- func CodecForRequest(r *http.Request, name string) (encoding.Codec, bool)
- func CodecForResponse(r *http.Response) encoding.Codec
- func DefaultErrorDecoder(_ context.Context, res *http.Response) error
- func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error)
- func DefaultRequestDecoder(r *http.Request, v any) error
- func DefaultRequestEncoder(_ context.Context, contentType string, in any) ([]byte, error)
- func DefaultRequestQuery(r *http.Request, v any) error
- func DefaultRequestVars(r *http.Request, v any) error
- func DefaultResponseDecoder(_ context.Context, res *http.Response, v any) error
- func DefaultResponseEncoder(w http.ResponseWriter, r *http.Request, v any) error
- func RequestFromServerContext(ctx context.Context) (*http.Request, bool)
- func ResponseWriterFromServerContext(ctx context.Context) (http.ResponseWriter, bool)
- func SetCookie(ctx context.Context, cookie *http.Cookie)
- func SetOperation(ctx context.Context, op string)
- type AcceptCallOption
- type BuildPathOption
- type CORSConfig
- type CallOption
- type Client
- func (client *Client) Close() error
- func (client *Client) Do(req *http.Request, opts ...CallOption) (*http.Response, error)
- func (client *Client) Invoke(ctx context.Context, method, path string, args any, reply any, ...) error
- func (client *Client) ServerSentEvent(ctx context.Context, method, path string, args any, opts ...CallOption) (ClientStream, error)
- func (client *Client) WebSocket(ctx context.Context, path string, opts ...CallOption) (ClientStream, error)
- type ClientOption
- func WithBlock() ClientOption
- func WithDiscovery(d registry.Discovery) ClientOption
- func WithEndpoint(endpoint string) ClientOption
- func WithErrorDecoder(errorDecoder DecodeErrorFunc) ClientOption
- func WithMiddleware(m ...middleware.Middleware) ClientOption
- func WithNodeFilter(filters ...selector.NodeFilter) ClientOption
- func WithRequestEncoder(encoder EncodeRequestFunc) ClientOption
- func WithResponseDecoder(decoder DecodeResponseFunc) ClientOption
- func WithSubset(size int) ClientOption
- func WithTLSConfig(c *tls.Config) ClientOption
- func WithTimeout(d time.Duration) ClientOption
- func WithTransport(trans http.RoundTripper) ClientOption
- func WithUserAgent(ua string) ClientOption
- type ClientStream
- type ContentTypeCallOption
- type Context
- type DecodeErrorFunc
- type DecodeRequestFunc
- type DecodeResponseFunc
- type EmptyCallOption
- type EncodeErrorFunc
- type EncodeRequestFunc
- type EncodeResponseFunc
- type ErrorResponse
- type FilterFunc
- type Flusher
- type HandlerFunc
- type HeaderCallOption
- type OperationCallOption
- type PathTemplateCallOption
- type Redirector
- type Request
- type ResponseController
- type ResponseTransporter
- type ResponseWriter
- type RouteInfo
- type Router
- func (r *Router) CONNECT(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) DELETE(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) GET(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) Group(prefix string, filters ...FilterFunc) *Router
- func (r *Router) HEAD(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) Handle(method, relativePath string, h HandlerFunc, filters ...FilterFunc)
- func (r *Router) OPTIONS(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) PATCH(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) POST(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) PUT(path string, h HandlerFunc, m ...FilterFunc)
- func (r *Router) TRACE(path string, h HandlerFunc, m ...FilterFunc)
- type Server
- func (s *Server) Endpoint() (*url.URL, error)
- func (s *Server) Handle(path string, h http.Handler)
- func (s *Server) HandleFunc(path string, h http.HandlerFunc)
- func (s *Server) HandleHeader(key, val string, h http.HandlerFunc)
- func (s *Server) HandlePrefix(prefix string, h http.Handler)
- func (s *Server) Route(prefix string, filters ...FilterFunc) *Router
- func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) Stop(ctx context.Context) error
- func (s *Server) Use(selector string, m ...middleware.Middleware)
- func (s *Server) WalkHandle(handle func(method, path string, handler http.HandlerFunc)) error
- func (s *Server) WalkRoute(fn WalkRouteFunc) error
- type ServerOption
- func AccessLog(cfg logx.AccessLogConfig) ServerOption
- func Address(addr string) ServerOption
- func CORS(cfg CORSConfig) ServerOption
- func Endpoint(endpoint *url.URL) ServerOption
- func ErrorEncoder(en EncodeErrorFunc) ServerOption
- func Filter(filters ...FilterFunc) ServerOption
- func Listener(lis net.Listener) ServerOption
- func Logger(logger logx.Logger) ServerOption
- func MethodNotAllowedHandler(handler http.Handler) ServerOption
- func Metrics(m metricsx.Manager) ServerOption
- func Middleware(m ...middleware.Middleware) ServerOption
- func Network(network string) ServerOption
- func NotFoundHandler(handler http.Handler) ServerOption
- func PathPrefix(prefix string) ServerOption
- func RequestDecoder(dec DecodeRequestFunc) ServerOption
- func RequestQueryDecoder(dec DecodeRequestFunc) ServerOption
- func RequestVarsDecoder(dec DecodeRequestFunc) ServerOption
- func ResponseEncoder(en EncodeResponseFunc) ServerOption
- func StrictSlash(strictSlash bool) ServerOption
- func TLSConfig(c *tls.Config) ServerOption
- func Timeout(timeout time.Duration) ServerOption
- type ServerStream
- type ServerStreamOption
- type Target
- type Transport
- func (tr *Transport) Endpoint() string
- func (tr *Transport) Kind() transport.Kind
- func (tr *Transport) Operation() string
- func (tr *Transport) PathTemplate() string
- func (tr *Transport) ReplyHeader() transport.Header
- func (tr *Transport) Request() *http.Request
- func (tr *Transport) RequestHeader() transport.Header
- func (tr *Transport) Response() http.ResponseWriter
- type Transporter
- type WalkRouteFunc
Constants ¶
const SupportPackageIsVersion3 = true
SupportPackageIsVersion3 These constants should not be referenced from any other code.
Variables ¶
This section is empty.
Functions ¶
func BodyContentType ¶
BodyContentType returns the content type carried by v or a binary default.
func BuildPath ¶
func BuildPath(pathTemplate string, msg any, opts ...BuildPathOption) string
BuildPath builds an HTTP request path from a path template and request message.
func CodecForRequest ¶
CodecForRequest get encoding.Codec via http.Request
func CodecForResponse ¶
CodecForResponse get encoding.Codec via http.Response
func DefaultErrorDecoder ¶
DefaultErrorDecoder is an HTTP error decoder.
func DefaultErrorEncoder ¶
func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error)
DefaultErrorEncoder encodes the error to the HTTP response.
func DefaultRequestDecoder ¶
DefaultRequestDecoder decodes the request body to object.
func DefaultRequestEncoder ¶
DefaultRequestEncoder is an HTTP request encoder.
func DefaultRequestQuery ¶
DefaultRequestQuery decodes the request vars to object.
func DefaultRequestVars ¶
DefaultRequestVars decodes the request vars to object.
func DefaultResponseDecoder ¶
DefaultResponseDecoder is an HTTP response decoder.
func DefaultResponseEncoder ¶
DefaultResponseEncoder encodes the object to the HTTP response.
func RequestFromServerContext ¶
RequestFromServerContext returns request from context.
func ResponseWriterFromServerContext ¶
func ResponseWriterFromServerContext(ctx context.Context) (http.ResponseWriter, bool)
ResponseWriterFromServerContext returns the http.ResponseWriter from context if available. This function provides backward compatibility and safe access to the ResponseWriter. Returns nil if the transport doesn't implement ResponseTransporter.
func SetCookie ¶
SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
func SetOperation ¶
SetOperation sets the transport operation.
Types ¶
type AcceptCallOption ¶
type AcceptCallOption struct {
EmptyCallOption
ContentType string
}
AcceptCallOption sets the accepted response content type.
type BuildPathOption ¶
type BuildPathOption func(*buildPathOptions)
BuildPathOption configures path construction.
func WithOmitFields ¶
func WithOmitFields(fields ...string) BuildPathOption
WithOmitFields excludes fields from generated query parameters.
func WithQueryParams ¶
func WithQueryParams() BuildPathOption
WithQueryParams appends request fields that are not bound in the path as query parameters.
type CORSConfig ¶ added in v0.0.7
type CORSConfig struct {
Enabled bool
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
ExposedHeaders []string
AllowCredentials bool
MaxAge time.Duration
}
CORSConfig configures the built-in HTTP CORS filter.
type CallOption ¶
type CallOption interface {
// contains filtered or unexported methods
}
CallOption configures a Call before it starts or extracts information from a Call after it completes.
func ContentType ¶
func ContentType(contentType string) CallOption
ContentType with request content type.
func Header ¶
func Header(header *http.Header) CallOption
Header returns a CallOptions that retrieves the http response header from server reply.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client.
func NewClient ¶
func NewClient(ctx context.Context, opts ...ClientOption) (*Client, error)
NewClient returns an HTTP client.
func (*Client) Do ¶
Do send an HTTP request and decodes the body of response into target. returns an error (of type *Error) if the response status code is not 2xx.
func (*Client) Invoke ¶
func (client *Client) Invoke(ctx context.Context, method, path string, args any, reply any, opts ...CallOption) error
Invoke makes a rpc call procedure for remote service.
func (*Client) ServerSentEvent ¶
func (client *Client) ServerSentEvent(ctx context.Context, method, path string, args any, opts ...CallOption) (ClientStream, error)
ServerSentEvent opens an HTTP server-streaming call and receives replies as SSE events.
func (*Client) WebSocket ¶
func (client *Client) WebSocket(ctx context.Context, path string, opts ...CallOption) (ClientStream, error)
WebSocket opens an HTTP bidirectional streaming call over WebSocket.
type ClientOption ¶
type ClientOption func(*clientOptions)
ClientOption is HTTP client option.
func WithDiscovery ¶
func WithDiscovery(d registry.Discovery) ClientOption
WithDiscovery with client discovery.
func WithErrorDecoder ¶
func WithErrorDecoder(errorDecoder DecodeErrorFunc) ClientOption
WithErrorDecoder with client error decoder.
func WithMiddleware ¶
func WithMiddleware(m ...middleware.Middleware) ClientOption
WithMiddleware with client middleware.
func WithNodeFilter ¶
func WithNodeFilter(filters ...selector.NodeFilter) ClientOption
WithNodeFilter with select filters
func WithRequestEncoder ¶
func WithRequestEncoder(encoder EncodeRequestFunc) ClientOption
WithRequestEncoder with client request encoder.
func WithResponseDecoder ¶
func WithResponseDecoder(decoder DecodeResponseFunc) ClientOption
WithResponseDecoder with client response decoder.
func WithSubset ¶
func WithSubset(size int) ClientOption
WithSubset with client discovery subset size. zero value means subset filter disabled
func WithTimeout ¶
func WithTimeout(d time.Duration) ClientOption
WithTimeout with client request timeout.
func WithTransport ¶
func WithTransport(trans http.RoundTripper) ClientOption
WithTransport with client transport.
func WithUserAgent ¶
func WithUserAgent(ua string) ClientOption
WithUserAgent with client user agent.
type ClientStream ¶
type ClientStream interface {
grpc.ClientStream
Send(any) error
Recv(any) error
CloseAndRecv(any) error
}
ClientStream adapts HTTP streaming clients to grpc generated stream interfaces.
type ContentTypeCallOption ¶
type ContentTypeCallOption struct {
EmptyCallOption
ContentType string
}
ContentTypeCallOption is BodyCallOption
type Context ¶
type Context interface {
context.Context
Vars() url.Values
Query() url.Values
Form() url.Values
Header() http.Header
Request() *http.Request
Response() http.ResponseWriter
Middleware(middleware.Handler) middleware.Handler
Bind(any) error
BindVars(any) error
BindQuery(any) error
BindForm(any) error
Returns(any, error) error
Result(int, any) error
JSON(int, any) error
XML(int, any) error
String(int, string) error
Blob(int, string, []byte) error
Stream(int, string, io.Reader) error
Reset(http.ResponseWriter, *http.Request)
}
Context is an HTTP Context.
type DecodeErrorFunc ¶
DecodeErrorFunc is decode error func.
type DecodeRequestFunc ¶
DecodeRequestFunc is decode request func.
type DecodeResponseFunc ¶
DecodeResponseFunc is response decode func.
type EmptyCallOption ¶
type EmptyCallOption struct{}
EmptyCallOption does not alter the Call configuration. It can be embedded in another structure to carry satellite data for use by interceptors.
type EncodeErrorFunc ¶
type EncodeErrorFunc func(http.ResponseWriter, *http.Request, error)
EncodeErrorFunc is encode error func.
type EncodeRequestFunc ¶
type EncodeRequestFunc func(ctx context.Context, contentType string, in any) (body []byte, err error)
EncodeRequestFunc is request encode func.
type EncodeResponseFunc ¶
EncodeResponseFunc is encode response func.
type ErrorResponse ¶
type ErrorResponse struct {
Code string `json:"code" yaml:"code"`
Message string `json:"message" yaml:"message"`
RequestID string `json:"request_id,omitempty" yaml:"request_id,omitempty"`
TraceID string `json:"trace_id,omitempty" yaml:"trace_id,omitempty"`
Metadata map[string]any `json:"metadata,omitempty" yaml:"metadata,omitempty"`
}
ErrorResponse is the transport-safe HTTP representation of an errorx error. HTTP status stays in the response status code; Code is the stable business code.
func ErrorResponseFromError ¶
func ErrorResponseFromError(err error) ErrorResponse
type FilterFunc ¶
FilterFunc is a function which receives a http.Handler and returns another http.Handler.
func CORSFilter ¶ added in v0.0.7
func CORSFilter(cfg CORSConfig) FilterFunc
CORSFilter returns a standard net/http middleware implementing browser CORS preflight and response headers. It is also exported so apps can use it with the generic Filter option when needed.
func FilterChain ¶
func FilterChain(filters ...FilterFunc) FilterFunc
FilterChain returns a FilterFunc that specifies the chained handler for HTTP Router.
type HandlerFunc ¶
HandlerFunc defines a function to serve HTTP requests.
type HeaderCallOption ¶
type HeaderCallOption struct {
EmptyCallOption
// contains filtered or unexported fields
}
HeaderCallOption is retrieve response header for client call
type OperationCallOption ¶
type OperationCallOption struct {
EmptyCallOption
Operation string
}
OperationCallOption is set ServiceMethod for client call
type PathTemplateCallOption ¶
type PathTemplateCallOption struct {
EmptyCallOption
Pattern string
}
PathTemplateCallOption is set path template for client call
type Redirector ¶
Redirector replies to the request with a redirect to url which may be a path relative to the request path.
func NewRedirect ¶
func NewRedirect(url string, code int) Redirector
NewRedirect new a redirect with url, which may be a path relative to the request path. The provided code should be in the 3xx range and is usually StatusMovedPermanently, StatusFound or StatusSeeOther. If the Content-Type header has not been set, Redirect sets it to "text/html; charset=utf-8" and writes a small HTML body. Setting the Content-Type header to any value, including nil, disables that behavior.
type ResponseController ¶
type ResponseController = http.ResponseController
type ResponseTransporter ¶
type ResponseTransporter interface {
Transporter
Response() http.ResponseWriter
}
ResponseTransporter extends Transporter with HTTP response access This interface provides access to the http.ResponseWriter for use cases like file downloads, streaming responses, or direct response manipulation.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is an HTTP router.
func (*Router) CONNECT ¶
func (r *Router) CONNECT(path string, h HandlerFunc, m ...FilterFunc)
CONNECT registers a new CONNECT route for a path with matching handler in the router.
func (*Router) DELETE ¶
func (r *Router) DELETE(path string, h HandlerFunc, m ...FilterFunc)
DELETE registers a new DELETE route for a path with matching handler in the router.
func (*Router) GET ¶
func (r *Router) GET(path string, h HandlerFunc, m ...FilterFunc)
GET registers a new GET route for a path with matching handler in the router.
func (*Router) Group ¶
func (r *Router) Group(prefix string, filters ...FilterFunc) *Router
Group returns a new router group.
func (*Router) HEAD ¶
func (r *Router) HEAD(path string, h HandlerFunc, m ...FilterFunc)
HEAD registers a new HEAD route for a path with matching handler in the router.
func (*Router) Handle ¶
func (r *Router) Handle(method, relativePath string, h HandlerFunc, filters ...FilterFunc)
Handle registers a new route with a matcher for the URL path and method.
func (*Router) OPTIONS ¶
func (r *Router) OPTIONS(path string, h HandlerFunc, m ...FilterFunc)
OPTIONS registers a new OPTIONS route for a path with matching handler in the router.
func (*Router) PATCH ¶
func (r *Router) PATCH(path string, h HandlerFunc, m ...FilterFunc)
PATCH registers a new PATCH route for a path with matching handler in the router.
func (*Router) POST ¶
func (r *Router) POST(path string, h HandlerFunc, m ...FilterFunc)
POST registers a new POST route for a path with matching handler in the router.
func (*Router) PUT ¶
func (r *Router) PUT(path string, h HandlerFunc, m ...FilterFunc)
PUT registers a new PUT route for a path with matching handler in the router.
func (*Router) TRACE ¶
func (r *Router) TRACE(path string, h HandlerFunc, m ...FilterFunc)
TRACE registers a new TRACE route for a path with matching handler in the router.
type Server ¶
Server is an HTTP server wrapper.
func NewServer ¶
func NewServer(opts ...ServerOption) *Server
NewServer creates an HTTP server by options.
func (*Server) Endpoint ¶
Endpoint return a real address to registry endpoint. examples:
https://127.0.0.1:8000 Legacy: http://127.0.0.1:8000?isSecure=false
func (*Server) HandleFunc ¶
func (s *Server) HandleFunc(path string, h http.HandlerFunc)
HandleFunc registers a new route with a matcher for the URL path.
func (*Server) HandleHeader ¶
func (s *Server) HandleHeader(key, val string, h http.HandlerFunc)
HandleHeader registers a new route with a matcher for the header.
func (*Server) HandlePrefix ¶
HandlePrefix registers a new route with a matcher for the URL path prefix.
func (*Server) Route ¶
func (s *Server) Route(prefix string, filters ...FilterFunc) *Router
Route registers an HTTP router.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)
ServeHTTP should write reply headers and data to the ResponseWriter and then return.
func (*Server) Use ¶
func (s *Server) Use(selector string, m ...middleware.Middleware)
Use uses a service middleware with selector. selector:
- '/*'
- '/helloworld.v1.Greeter/*'
- '/helloworld.v1.Greeter/SayHello'
func (*Server) WalkHandle ¶
func (s *Server) WalkHandle(handle func(method, path string, handler http.HandlerFunc)) error
WalkHandle walks the router and all its sub-routers, calling walkFn for each route in the tree.
func (*Server) WalkRoute ¶
func (s *Server) WalkRoute(fn WalkRouteFunc) error
WalkRoute walks the router and all its sub-routers, calling walkFn for each route in the tree.
type ServerOption ¶
type ServerOption func(*Server)
ServerOption is an HTTP server option.
func AccessLog ¶ added in v0.0.5
func AccessLog(cfg logx.AccessLogConfig) ServerOption
AccessLog configures the built-in HTTP access log middleware. Pass logx.AccessLogConfig{Enabled:false} to disable it explicitly.
func CORS ¶ added in v0.0.7
func CORS(cfg CORSConfig) ServerOption
CORS installs a CORS filter. Unlike Filter(...), it appends to the existing filter chain so callers can combine it safely with authn/custom filters.
func ErrorEncoder ¶
func ErrorEncoder(en EncodeErrorFunc) ServerOption
ErrorEncoder with error encoder.
func Logger ¶ added in v0.0.5
func Logger(logger logx.Logger) ServerOption
Logger sets the logx logger used by built-in HTTP access/error logs.
func MethodNotAllowedHandler ¶
func MethodNotAllowedHandler(handler http.Handler) ServerOption
func Metrics ¶ added in v0.0.5
func Metrics(m metricsx.Manager) ServerOption
Metrics enables built-in low-cardinality HTTP server metrics.
func Middleware ¶
func Middleware(m ...middleware.Middleware) ServerOption
Middleware with service middleware option.
func NotFoundHandler ¶
func NotFoundHandler(handler http.Handler) ServerOption
func PathPrefix ¶
func PathPrefix(prefix string) ServerOption
PathPrefix with mux's PathPrefix, router will be replaced by a subrouter that start with prefix.
func RequestDecoder ¶
func RequestDecoder(dec DecodeRequestFunc) ServerOption
RequestDecoder with request decoder.
func RequestQueryDecoder ¶
func RequestQueryDecoder(dec DecodeRequestFunc) ServerOption
RequestQueryDecoder with request decoder.
func RequestVarsDecoder ¶
func RequestVarsDecoder(dec DecodeRequestFunc) ServerOption
RequestVarsDecoder with request decoder.
func ResponseEncoder ¶
func ResponseEncoder(en EncodeResponseFunc) ServerOption
ResponseEncoder with response encoder.
func StrictSlash ¶
func StrictSlash(strictSlash bool) ServerOption
StrictSlash is with mux's StrictSlash If true, when the path pattern is "/path/", accessing "/path" will redirect to the former and vice versa.
type ServerStream ¶
type ServerStream interface {
grpc.ServerStream
Send(any) error
Recv(any) error
SendAndClose(any) error
Close(error) error
SetContext(context.Context)
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
}
ServerStream adapts HTTP streaming transports to grpc generated stream interfaces.
func NewServerSentEventServerStream ¶
func NewServerSentEventServerStream(ctx Context) ServerStream
NewServerSentEventServerStream returns a stream that writes server messages as SSE events.
func NewWebSocketServerStream ¶
func NewWebSocketServerStream(ctx Context, opts ...ServerStreamOption) (ServerStream, error)
NewWebSocketServerStream upgrades the current request and returns a WebSocket stream.
type ServerStreamOption ¶
type ServerStreamOption func(*serverStream)
ServerStreamOption customizes a server stream created by the HTTP transport.
func WithStreamBodyField ¶
func WithStreamBodyField(name string) ServerStreamOption
WithStreamBodyField declares the request message field that carries each streamed frame's payload. It is used for client-streaming RPCs whose HTTP rule maps a named body field (e.g. body: "data"): every received frame is decoded into that field while the remaining fields are bound from the request query and path vars.
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport is an HTTP transport.
func (*Transport) PathTemplate ¶
PathTemplate returns the http path template.
func (*Transport) ReplyHeader ¶
ReplyHeader returns the reply header.
func (*Transport) RequestHeader ¶
RequestHeader returns the request header.
func (*Transport) Response ¶
func (tr *Transport) Response() http.ResponseWriter
Response returns the HTTP response.
type Transporter ¶
type Transporter interface {
transport.Transporter
Request() *http.Request
PathTemplate() string
}
Transporter is http Transporter
type WalkRouteFunc ¶
WalkRouteFunc is the type of the function called for each route visited by Walk.