Documentation ¶
Index ¶
- Constants
- Variables
- type BindingError
- type CORS
- func (cors *CORS) AddAllowedHeader(header string)
- func (cors *CORS) AddAllowedMethod(method string)
- func (cors *CORS) AddAllowedOrigin(origin string)
- func (cors *CORS) Handle(c *Context)
- func (cors *CORS) SetAllowedHeaders(headers []string)
- func (cors *CORS) SetAllowedMethods(methods []string)
- func (cors *CORS) SetAllowedOrigins(origins []string)
- type CORSConfig
- type Context
- func (c *Context) Bind(targetStruct interface{}) *BindingError
- func (c *Context) BindJSON(targetStruct interface{}) *BindingError
- func (c *Context) BindMultipartForm(targetStruct interface{}) *BindingError
- func (c *Context) BindSimpleForm(targetStruct interface{}) *BindingError
- func (c *Context) Data(statusCode int, binary []byte)
- func (c *Context) ExpectJSON() bool
- func (c *Context) File(statusCode int, filepath string)
- func (c *Context) GetRequestHeader(key string) string
- func (c *Context) HTML(statusCode int, html string)
- func (c *Context) IsJSON() bool
- func (c *Context) JSON(statusCode int, object interface{})
- func (c *Context) Next()
- func (c *Context) Param(key string) string
- func (c *Context) PostForm(key string) string
- func (c *Context) PostFormDefault(key string, defaultValue string) string
- func (c *Context) Query(key string) string
- func (c *Context) QueryDefault(key string, defaultValue string) string
- func (c *Context) SetContentType(contentType string)
- func (c *Context) SetHeader(key, value string)
- func (c *Context) Status(statusCode int)
- func (c *Context) String(statusCode int, template string, value ...interface{})
- type Engine
- type H
- type HandlerFunc
- type RouterGroup
- func (rg *RouterGroup) DELETE(urlPattern string, handler HandlerFunc)
- func (rg *RouterGroup) Default(handler HandlerFunc) error
- func (rg *RouterGroup) GET(urlPattern string, handler ...HandlerFunc)
- func (rg *RouterGroup) Group(prefix string) *RouterGroup
- func (rg *RouterGroup) HEAD(urlPattern string, handler ...HandlerFunc)
- func (rg *RouterGroup) OPTIONS(urlPattern string, handler HandlerFunc)
- func (rg *RouterGroup) PATCH(urlPattern string, handler HandlerFunc)
- func (rg *RouterGroup) POST(urlPattern string, handler HandlerFunc)
- func (rg *RouterGroup) PUT(urlPattern string, handler HandlerFunc)
- func (rg *RouterGroup) Static(baseURL string, rootDir http.FileSystem)
- func (rg *RouterGroup) Use(middlewares ...HandlerFunc)
Constants ¶
const ( // HeaderAcceptEncoding is accept encoding. HeaderAcceptEncoding = "Accept-Encoding" // HeaderContentEncoding is content encoding. HeaderContentEncoding = "Content-Encoding" // HeaderContentLength is content length. HeaderContentLength = "Content-Length" // HeaderContentType is content type. HeaderContentType = "Content-Type" // HeaderAccept is accept content type. HeaderAccept = "Accept" // HeaderOrigin is request origin. HeaderOrigin = "Origin" // HeaderVary is request vary. HeaderVary = "Vary" // HeaderAccessControlRequestMethod is cors request method. HeaderAccessControlRequestMethod = "Access-Control-Request-Method" // HeaderAccessControlRequestHeader is cors request header. HeaderAccessControlRequestHeader = "Access-Control-Request-Header" // HeaderAccessControlAllowOrigin is cors allowed origins. HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" // HeaderAccessControlAllowMethods is cors allowed origins. HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" // HeaderAccessControlAllowHeader is cors allowed headers. HeaderAccessControlAllowHeader = "Access-Control-Allow-Header" // MimeJSON is standard json mime. MimeJSON = "application/json" // MimeXML is standard json mime. MimeXML = "application/xml" // MimeHTML is standard html mime. MimeHTML = "text/html" // MimePlainText is standard plain text mime. MimePlainText = "text/plain" // MimeMultipartForm is standard multipart form mime. MimeMultipartForm = "multipart/form-data" // MimeFormURLEncoded is standard urlencoded form mime. MimeFormURLEncoded = "application/x-www-form-urlencoded" )
Variables ¶
var ( // ErrBindNonPointer must be returned when non-pointer struct passed as targetStruct parameter. ErrBindNonPointer = &BindingError{ Message: "expected pointer to target struct, got non-pointer", HTTPStatusCode: http.StatusInternalServerError, } // ErrBindContentType returned when client content type besides json, urlencoded, & multipart form. ErrBindContentType = &BindingError{ HTTPStatusCode: http.StatusBadRequest, Message: "unknown content type of request body", } )
var ( // ErrDefaultHandler should be returned when user try to set default handler for seconds time. ErrDefaultHandler = errors.New("default handler already registered") )
Functions ¶
This section is empty.
Types ¶
type BindingError ¶
BindingError is an error wrapper. HTTPStatusCode will set to 422 when there is error on validation, 400 when client sent unsupported/without Content-Type header, and 500 when targetStruct is not pointer or type conversion is fail.
type CORS ¶
type CORS struct {
// contains filtered or unexported fields
}
CORS struct.
func (*CORS) AddAllowedHeader ¶
AddAllowedHeader is functions to append method to allowed list.
func (*CORS) AddAllowedMethod ¶
AddAllowedMethod is functions to append method to allowed list.
func (*CORS) AddAllowedOrigin ¶
AddAllowedOrigin is functions to append method to allowed list.
func (*CORS) Handle ¶
Handle corss-origin request The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser. Additionally, for HTTP request methods that can cause side-effects on server's data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.
func (*CORS) SetAllowedHeaders ¶
SetAllowedHeaders is functions to fill/replace all allowed headers.
func (*CORS) SetAllowedMethods ¶
SetAllowedMethods is functions to fill/replace all allowed methods.
func (*CORS) SetAllowedOrigins ¶
SetAllowedOrigins is functions to fill/replace all allowed origins.
type CORSConfig ¶
CORSConfig define nano cors middleware configuration.
type Context ¶
type Context struct { Request *http.Request Writer http.ResponseWriter Method string Path string Origin string Params map[string]string // contains filtered or unexported fields }
Context defines nano request - response context.
func (*Context) Bind ¶
func (c *Context) Bind(targetStruct interface{}) *BindingError
Bind request body into defined user struct. This function help you to automatic binding based on request Content-Type & request method. If you want to chooose binding method manually, you could use : BindSimpleForm to bind urlencoded form & url query, BindMultipartForm to bind multipart/form data, and BindJSON to bind application/json request body.
func (*Context) BindJSON ¶
func (c *Context) BindJSON(targetStruct interface{}) *BindingError
BindJSON is functions to bind request body (with contet type application/json) to targetStruct. targetStruct must be pointer to user defined struct.
func (*Context) BindMultipartForm ¶
func (c *Context) BindMultipartForm(targetStruct interface{}) *BindingError
BindMultipartForm is functions to bind request body (with contet type multipart/form-data) to targetStruct. targetStruct must be pointer to user defined struct.
func (*Context) BindSimpleForm ¶
func (c *Context) BindSimpleForm(targetStruct interface{}) *BindingError
BindSimpleForm is functions to bind request body (with content type form-urlencoded or url query) to targetStruct. targetStruct must be pointer to user defined struct.
func (*Context) ExpectJSON ¶
ExpectJSON returns true when client request json response, since this function use string.Contains, value ordering in Accept values doesn't matter.
func (*Context) GetRequestHeader ¶
GetRequestHeader returns header value by given key.
func (*Context) Next ¶
func (c *Context) Next()
Next is functions to move cursor to the next handler stack.
func (*Context) PostFormDefault ¶
PostFormDefault return default value when form body field is empty.
func (*Context) QueryDefault ¶
QueryDefault return default value when url query is empty
func (*Context) SetContentType ¶
SetContentType is functions to set http content type response header.
type Engine ¶
type Engine struct { *RouterGroup // contains filtered or unexported fields }
Engine defines nano web engine.
type HandlerFunc ¶
type HandlerFunc func(c *Context)
HandlerFunc defines nano request handler function signature.
func CORSWithConfig ¶
func CORSWithConfig(config CORSConfig) HandlerFunc
CORSWithConfig returns cors middleware.
func Gzip ¶
func Gzip(compressionLevel int) HandlerFunc
Gzip compression for http response. this compression works when client accept gzip in their request.
func Recovery ¶
func Recovery() HandlerFunc
Recovery middleware is functions to recover when panic was fired.
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
RouterGroup defines collection of route that has same prefix
func (*RouterGroup) DELETE ¶
func (rg *RouterGroup) DELETE(urlPattern string, handler HandlerFunc)
DELETE is functions to register route with DELETE request method.
func (*RouterGroup) Default ¶
func (rg *RouterGroup) Default(handler HandlerFunc) error
Default is functions to register default handler when no matching routes. Only one Default handler allowed to register.
func (*RouterGroup) GET ¶
func (rg *RouterGroup) GET(urlPattern string, handler ...HandlerFunc)
GET is functions to register route with GET request method.
func (*RouterGroup) Group ¶
func (rg *RouterGroup) Group(prefix string) *RouterGroup
Group is functions to create new router group.
func (*RouterGroup) HEAD ¶
func (rg *RouterGroup) HEAD(urlPattern string, handler ...HandlerFunc)
HEAD is functions to register route with HEAD request method.
func (*RouterGroup) OPTIONS ¶
func (rg *RouterGroup) OPTIONS(urlPattern string, handler HandlerFunc)
OPTIONS is functions to register route with OPTIONS request method.
func (*RouterGroup) PATCH ¶
func (rg *RouterGroup) PATCH(urlPattern string, handler HandlerFunc)
PATCH is functions to register route with PATCH request method.
func (*RouterGroup) POST ¶
func (rg *RouterGroup) POST(urlPattern string, handler HandlerFunc)
POST is functions to register route with POST request method.
func (*RouterGroup) PUT ¶
func (rg *RouterGroup) PUT(urlPattern string, handler HandlerFunc)
PUT is functions to register route with PUT request method.
func (*RouterGroup) Static ¶
func (rg *RouterGroup) Static(baseURL string, rootDir http.FileSystem)
Static will create static file server.
func (*RouterGroup) Use ¶
func (rg *RouterGroup) Use(middlewares ...HandlerFunc)
Use is functions to apply middleware function(s).