Documentation ¶
Index ¶
- Variables
- func CheckFunction(target interface{})
- func Handle(method string, path string, hc HandlerContainer)
- func HandleFunc(method string, path string, h interface{})
- func IsEmpty(fV reflect.Value) bool
- func ListenAndServe(addr string)
- func MakeHandlerTestBed(t *testing.T, method string, path string, body io.Reader) *http.Response
- func MakeMiddlewareTestBed(t *testing.T, middleware MiddlewareFunc, handler interface{}, ...) (*Bubble, *ServeMux)
- func Middleware(f MiddlewareFunc)
- func Orthodox()
- func Plugin(plugin interface{})
- func SetValueFromString(f reflect.Value, value string) error
- func SetValueFromStrings(f reflect.Value, values []string) error
- type Bubble
- type BubbleTestOption
- type CSRFOption
- type Context
- type HTTPErrorResponse
- type HTTPResponseModifier
- type HandlerContainer
- type HandlersScannerPlugin
- type MiddlewareFunc
- type PathTemplate
- type RouteDefinition
- type Router
- type ServeMux
- func (m *ServeMux) Handle(method string, path string, hc HandlerContainer)
- func (m *ServeMux) HandleFunc(method string, path string, h interface{})
- func (m *ServeMux) ListenAndServe(addr string) error
- func (m *ServeMux) Middleware(f MiddlewareFunc)
- func (m *ServeMux) Plugin(plugin interface{})
- func (m *ServeMux) Prepare()
- func (m *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type StringParser
- type TagJSON
- type Validator
Constants ¶
This section is empty.
Variables ¶
var DefaultMux = NewServeMux()
DefaultMux is the default ServeMux in ucon.
var ErrCSRFBadToken = newBadRequestf("invalid CSRF token")
ErrCSRFBadToken is the error returns CSRF token verify failure.
var ErrInvalidArgumentLength = errors.New("invalid arguments")
ErrInvalidArgumentLength is the error that length of Bubble.Arguments does not match to RequestHandler arguments.
var ErrInvalidArgumentValue = errors.New("invalid argument value")
ErrInvalidArgumentValue is the error that value in Bubble.Arguments is invalid.
var ErrInvalidPathParameterType = errors.New("path parameter type should be map[string]string")
ErrInvalidPathParameterType is the error that context with PathParameterKey key returns not map[string]string type.
var ErrInvalidRequestHandler = errors.New("invalid request handler. not function")
ErrInvalidRequestHandler is the error that Bubble.RequestHandler is not a function.
var ErrPathParameterFieldMissing = errors.New("can't find path parameter in struct")
ErrPathParameterFieldMissing is the path parameter mapping error.
var PathParameterKey = &struct{ temp string }{}
PathParameterKey is context key of path parameter. context returns map[string]string.
Functions ¶
func CheckFunction ¶
func CheckFunction(target interface{})
CheckFunction checks whether the target is a function.
func Handle ¶
func Handle(method string, path string, hc HandlerContainer)
Handle register the HandlerContainer for the given method & path to the ServeMux.
func HandleFunc ¶
HandleFunc register the handler function for the given method & path to the ServeMux.
func ListenAndServe ¶
func ListenAndServe(addr string)
ListenAndServe start accepts the client request.
func MakeHandlerTestBed ¶
MakeHandlerTestBed returns a response by the request made from arguments. To test some handlers, those must be registered by Handle or HandleFunc before calling this.
func MakeMiddlewareTestBed ¶
func MakeMiddlewareTestBed(t *testing.T, middleware MiddlewareFunc, handler interface{}, opts *BubbleTestOption) (*Bubble, *ServeMux)
MakeMiddlewareTestBed returns a Bubble and ServeMux for handling the request made from the option.
func SetValueFromString ¶
SetValueFromString parses string and sets value.
Types ¶
type Bubble ¶
type Bubble struct { R *http.Request W http.ResponseWriter Context context.Context RequestHandler HandlerContainer Debug bool Handled bool ArgumentTypes []reflect.Type Arguments []reflect.Value Returns []reflect.Value // contains filtered or unexported fields }
Bubble is a context of data processing that will be passed to a request handler at last. The name `Bubble` means that the processing flow is a event-bubbling. Processors, called `middleware`, are executed in order with same context, and at last the RequestHandler will be called.
type BubbleTestOption ¶
BubbleTestOption is an option for setting a mock request.
type CSRFOption ¶
type CSRFOption struct { Salt []byte SafeMethods []string CookieName string RequestHeaderName string GenerateCookie func(r *http.Request) (*http.Cookie, error) }
CSRFOption is options for CSRFProtect.
type Context ¶
type Context interface {
Value(key interface{}) interface{}
}
Context is a key-value store.
type HTTPErrorResponse ¶
type HTTPErrorResponse interface { // StatusCode returns http response status code. StatusCode() int // ErrorMessage returns an error object. // Returned object will be converted by json.Marshal and written as http response body. ErrorMessage() interface{} }
HTTPErrorResponse is a response to represent http errors.
type HTTPResponseModifier ¶
HTTPResponseModifier is an interface to hook on each responses and modify those. The hook will hijack ResponseMapper, so it makes possible to do something in place of ResponseMapper. e.g. You can convert a response object to xml and write it as response body.
type HandlerContainer ¶
type HandlerContainer interface { Handler() interface{} Context }
HandlerContainer is handler function container. and It has a ucon Context that make it possible communicate to Plugins.
type HandlersScannerPlugin ¶
type HandlersScannerPlugin interface {
HandlersScannerProcess(m *ServeMux, rds []*RouteDefinition) error
}
HandlersScannerPlugin is an interface to make a plugin for scanning request handlers.
type MiddlewareFunc ¶
MiddlewareFunc is an adapter to hook middleware processing. Middleware works with 1 request.
func CSRFProtect ¶
func CSRFProtect(opts *CSRFOption) (MiddlewareFunc, error)
CSRFProtect is a CSRF (Cross Site Request Forgery) prevention middleware.
func ContextDI ¶
func ContextDI() MiddlewareFunc
ContextDI injects Bubble.Context into the bubble.Arguments.
func HTTPRWDI ¶
func HTTPRWDI() MiddlewareFunc
HTTPRWDI injects Bubble.R and Bubble.W into the bubble.Arguments.
func NetContextDI ¶
func NetContextDI() MiddlewareFunc
NetContextDI injects Bubble.Context into the bubble.Arguments. deprecated. use ContextDI instead of NetContextDI.
func RequestObjectMapper ¶
func RequestObjectMapper() MiddlewareFunc
RequestObjectMapper converts a request to object and injects it into the bubble.Arguments.
func RequestValidator ¶
func RequestValidator(validator Validator) MiddlewareFunc
RequestValidator checks request object validity.
func ResponseMapper ¶
func ResponseMapper() MiddlewareFunc
ResponseMapper converts a response object to JSON and writes it as response body.
type PathTemplate ¶
type PathTemplate struct { PathTemplate string PathParameters []string // contains filtered or unexported fields }
PathTemplate is a path with parameters template.
func ParsePathTemplate ¶
func ParsePathTemplate(pathTmpl string) *PathTemplate
ParsePathTemplate parses path string to PathTemplate.
type RouteDefinition ¶
type RouteDefinition struct { Method string PathTemplate *PathTemplate HandlerContainer HandlerContainer }
RouteDefinition is a definition of route handling. If a request matches on both the method and the path, the handler runs.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is a handler to pass requests to the best-matched route definition. For the router decides the best route definition, there are 3 rules.
- Methods must match. a. If the method of request is `HEAD`, exceptionally `GET` definition is also allowed. b. If the method of definition is `*`, the definition matches on all method.
- Paths must match as longer as possible. a. The path of definition must match to the request path completely. b. Select the longest match. * Against Request[/api/foo/hi/comments/1], Definition[/api/foo/{bar}/] is stronger than Definition[/api/foo/].
- If there are multiple options after 1 and 2 rules, select the earliest one which have been added to router.
type ServeMux ¶
type ServeMux struct { Debug bool // contains filtered or unexported fields }
ServeMux is an HTTP request multiplexer.
func (*ServeMux) Handle ¶
func (m *ServeMux) Handle(method string, path string, hc HandlerContainer)
Handle register the HandlerContainer for the given method & path to the ServeMux.
func (*ServeMux) HandleFunc ¶
HandleFunc register the handler function for the given method & path to the ServeMux.
func (*ServeMux) ListenAndServe ¶
ListenAndServe start accepts the client request.
func (*ServeMux) Middleware ¶
func (m *ServeMux) Middleware(f MiddlewareFunc)
Middleware can append Middleware to ServeMux.
func (*ServeMux) Plugin ¶
func (m *ServeMux) Plugin(plugin interface{})
Plugin can append Plugin to ServeMux.
type StringParser ¶
StringParser is a parser for string-to-object custom conversion.