Documentation
¶
Overview ¶
Package http provides HTTP server implementation with server lifecycle control, commonly used adapters, easy service registration and response/error handling, tls support...
Index ¶
- func ReqFromCtx(c context.Context) *http.Request
- type Adapter
- type BaseService
- func (b *BaseService) Adapt(mw ...Adapter)
- func (b *BaseService) Endpoints() Endpoints
- func (b *BaseService) Prefix() string
- func (b *BaseService) RegisterEndpoint(verb string, path string, method interface{}, a ...Adapter) error
- func (b *BaseService) RegisterHandler(verb string, path string, h HandlerFunc, a ...Adapter)
- type Endpoint
- type Endpoints
- type Error
- type HandlerFunc
- type Response
- type Server
- type ServerOption
- type Service
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Adapter ¶
type Adapter func(HandlerFunc) HandlerFunc
Adapter represents HandlerFunc middleware adapter
type BaseService ¶
type BaseService struct {
// contains filtered or unexported fields
}
BaseService represents base http service
func (*BaseService) Adapt ¶
func (b *BaseService) Adapt(mw ...Adapter)
Adapt is used to adapt the service with provided adapters
func (*BaseService) Endpoints ¶
func (b *BaseService) Endpoints() Endpoints
Endpoints returns all registered endpoints
func (*BaseService) Prefix ¶
func (b *BaseService) Prefix() string
Prefix returns service routing prefix
func (*BaseService) RegisterEndpoint ¶
func (b *BaseService) RegisterEndpoint(verb string, path string, method interface{}, a ...Adapter) error
RegisterEndpoint is a helper method that registers service json endpoint JSON endpoint method should have the following signature: func(c context.Context, w http.ResponseWriter, req *CustomType) (*http.Response, error) where *CustomType is your custom request type to which r.Body will be json unmarshalled automatically *http.Response can be omitted if endpoint has no reasonable response, error is always required however
func (*BaseService) RegisterHandler ¶
func (b *BaseService) RegisterHandler(verb string, path string, h HandlerFunc, a ...Adapter)
RegisterHandler is a helper method that registers service HandlerFunc Service HandlerFunc is an extension of http.HandlerFunc which only adds context.Context as first parameter, the rest stays the same
type Endpoint ¶
type Endpoint struct {
Methods []string
Handler HandlerFunc
}
Endpoint represents http api endpoint interface
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error represents http error
type HandlerFunc ¶
HandlerFunc represents kit http handler func
func AdaptHandlerFunc ¶
func AdaptHandlerFunc(hf HandlerFunc, adapters ...Adapter) HandlerFunc
AdaptHandlerFunc decorates given HandlerFunc with provided adapters
func (HandlerFunc) ServeHTTP ¶
func (hf HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler for HandlerFunc
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response represents http response
func NewResponse ¶
NewResponse wraps provided code and resp into Response so it can be used with respond
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents kit http server
func NewServer ¶
func NewServer(opts ...ServerOption) *Server
NewServer creates new http server instance
func (*Server) RegisterService ¶
RegisterService registers a given http Service with the server and sets up routes
func (*Server) RegisterServices ¶
RegisterServices registers given http Services with the server and sets up routes
type ServerOption ¶
type ServerOption func(*Server)
ServerOption is used for setting up server configuration
func WithAdapters ¶
func WithAdapters(a ...Adapter) ServerOption
WithAdapters represents server option for setting up server-wide request adapters
func WithDefaultHandler ¶
func WithDefaultHandler(h http.Handler) ServerOption
WithDefaultHandler represents server option for setting default http server handler which by default is gorilla mux router
func WithLogger ¶
func WithLogger(l *log.Logger) ServerOption
WithLogger represents server option for setting up logger
func WithNotFoundHandler ¶
func WithNotFoundHandler(h http.Handler) ServerOption
WithNotFoundHandler represents server option for setting default not found handler
func WithTLSConfig ¶
func WithTLSConfig(cert, key string) ServerOption
WithTLSConfig represents server option for setting tls cer and key files