Documentation ¶
Overview ¶
Package web 实现了HTTP请求路由,中间件,请求响应处理,配合container实现依赖注入。
例如
func ExampleHandler(ctx *web.WebContext) web.Response { return ctx.Resolve(func(messageModel *storage.MessageModel) web.Response { // your codes return ctx.NewAPIResponse("000000", "ok", map[string]interface{} { "aa": "bbb", "ccc": "ddd", }) }) }
Index ¶
- Variables
- func View(tplPath string, data interface{}) (string, error)
- type Config
- type Context
- type Controller
- type Error
- type ErrorResponse
- type ExceptionHandler
- type HTMLResponse
- type HandlerDecorator
- type HttpRequest
- func (req *HttpRequest) AJAX() bool
- func (req *HttpRequest) AllHeaders() http.Header
- func (req *HttpRequest) Body() []byte
- func (req *HttpRequest) Clear()
- func (req *HttpRequest) ContentType() string
- func (req *HttpRequest) Context() context.Context
- func (req *HttpRequest) Cookie(name string) (*http.Cookie, error)
- func (req *HttpRequest) Cookies() []*http.Cookie
- func (req *HttpRequest) Decode(v interface{}) error
- func (req *HttpRequest) File(key string) (*UploadedFile, error)
- func (req *HttpRequest) Float32Input(key string, defaultVal float32) float32
- func (req *HttpRequest) Float64Input(key string, defaultVal float64) float64
- func (req *HttpRequest) Get(key string) interface{}
- func (req *HttpRequest) HTTPRequest() *http.Request
- func (req *HttpRequest) Header(key string) string
- func (req *HttpRequest) Headers(key string) []string
- func (req *HttpRequest) Input(key string) string
- func (req *HttpRequest) InputWithDefault(key string, defaultVal string) string
- func (req *HttpRequest) Int64Input(key string, defaultVal int64) int64
- func (req *HttpRequest) IntInput(key string, defaultVal int) int
- func (req *HttpRequest) Is(method string) bool
- func (req *HttpRequest) IsDelete() bool
- func (req *HttpRequest) IsGet() bool
- func (req *HttpRequest) IsHead() bool
- func (req *HttpRequest) IsJSON() bool
- func (req *HttpRequest) IsOptions() bool
- func (req *HttpRequest) IsPatch() bool
- func (req *HttpRequest) IsPost() bool
- func (req *HttpRequest) IsPut() bool
- func (req *HttpRequest) IsXMLHTTPRequest() bool
- func (req *HttpRequest) JSONGet(keys ...string) string
- func (req *HttpRequest) Method() string
- func (req *HttpRequest) PathVar(key string) string
- func (req *HttpRequest) PathVars() map[string]string
- func (req *HttpRequest) Raw() *http.Request
- func (req *HttpRequest) Session() *sessions.Session
- func (req *HttpRequest) Set(key string, value interface{})
- func (req *HttpRequest) SetSession(session *sessions.Session)
- func (req *HttpRequest) ToFloat32(val string, defaultVal float32) float32
- func (req *HttpRequest) ToFloat64(val string, defaultVal float64) float64
- func (req *HttpRequest) ToInt(val string, defaultVal int) int
- func (req *HttpRequest) ToInt64(val string, defaultVal int64) int64
- func (req *HttpRequest) Unmarshal(v interface{}) error
- func (req *HttpRequest) UnmarshalYAML(v interface{}) error
- func (req *HttpRequest) Validate(validator Validator, jsonResponse bool)
- type HttpResponse
- func (resp *HttpResponse) Cookie(cookie *http.Cookie)
- func (resp *HttpResponse) Flush()
- func (resp *HttpResponse) Header(key, value string)
- func (resp *HttpResponse) Raw() http.ResponseWriter
- func (resp *HttpResponse) ResponseWriter() http.ResponseWriter
- func (resp *HttpResponse) SetCode(code int)
- func (resp *HttpResponse) SetContent(content []byte)
- type JSONAble
- type JSONError
- type JSONResponse
- type M
- type MiddlewareRouter
- type NilResponse
- type PlainError
- type RawResponse
- type RedirectResponse
- type Request
- type RequestMiddleware
- func (rm RequestMiddleware) AccessLog() HandlerDecorator
- func (rm RequestMiddleware) AuthHandler(cb func(typ string, credential string) error) HandlerDecorator
- func (rm RequestMiddleware) CORS(origin string) HandlerDecorator
- func (rm RequestMiddleware) Session(store sessions.Store, name string, options *sessions.Options) HandlerDecorator
- type Response
- type ResponseCreator
- type Route
- type RouteRule
- func (rr *RouteRule) Custom(custom func(rou *mux.Route)) *RouteRule
- func (rr *RouteRule) Headers(pairs ...string) *RouteRule
- func (rr *RouteRule) Host(tpl string) *RouteRule
- func (rr *RouteRule) Name(name string) *RouteRule
- func (rr *RouteRule) Queries(pairs ...string) *RouteRule
- func (rr *RouteRule) Schemes(schemes ...string) *RouteRule
- type Router
- func (router *Router) Any(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) Controllers(prefix string, controllers ...Controller)
- func (router *Router) Delete(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) Get(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) GetRoutes() []*RouteRule
- func (router *Router) Group(prefix string, f func(rou *Router), decors ...HandlerDecorator)
- func (router *Router) Head(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) Options(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) Patch(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) Perform(exceptionHandler ExceptionHandler) *mux.Router
- func (router *Router) Post(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) Put(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebAny(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebDelete(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebGet(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebHead(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebOptions(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebPatch(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebPost(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WebPut(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
- func (router *Router) WithMiddleware(decors ...HandlerDecorator) *MiddlewareRouter
- type UploadedFile
- type Validator
- type WebContext
- func (ctx *WebContext) AJAX() bool
- func (ctx *WebContext) API(businessCode string, message string, data interface{}) *JSONResponse
- func (ctx *WebContext) AllHeaders() http.Header
- func (ctx *WebContext) Body() []byte
- func (ctx *WebContext) Container() *container.Container
- func (ctx *WebContext) ContentType() string
- func (ctx *WebContext) Context() context.Context
- func (ctx *WebContext) Cookie(name string) (*http.Cookie, error)
- func (ctx *WebContext) Cookies() []*http.Cookie
- func (ctx *WebContext) Decode(v interface{}) error
- func (ctx *WebContext) Error(res string, code int) *ErrorResponse
- func (ctx *WebContext) File(key string) (*UploadedFile, error)
- func (ctx *WebContext) Float32Input(key string, defaultVal float32) float32
- func (ctx *WebContext) Float64Input(key string, defaultVal float64) float64
- func (ctx *WebContext) Get(key string) interface{}
- func (ctx *WebContext) HTML(res string) *HTMLResponse
- func (ctx *WebContext) HTMLWithCode(res string, code int) *HTMLResponse
- func (ctx *WebContext) Header(key string) string
- func (ctx *WebContext) Headers(key string) []string
- func (ctx *WebContext) Input(key string) string
- func (ctx *WebContext) InputWithDefault(key string, defaultVal string) string
- func (ctx *WebContext) Int64Input(key string, defaultVal int64) int64
- func (ctx *WebContext) IntInput(key string, defaultVal int) int
- func (ctx *WebContext) Is(method string) bool
- func (ctx *WebContext) IsDelete() bool
- func (ctx *WebContext) IsGet() bool
- func (ctx *WebContext) IsHead() bool
- func (ctx *WebContext) IsJSON() bool
- func (ctx *WebContext) IsOptions() bool
- func (ctx *WebContext) IsPatch() bool
- func (ctx *WebContext) IsPost() bool
- func (ctx *WebContext) IsPut() bool
- func (ctx *WebContext) IsXMLHTTPRequest() bool
- func (ctx *WebContext) JSON(res interface{}) *JSONResponse
- func (ctx *WebContext) JSONError(res string, code int) *JSONResponse
- func (ctx *WebContext) JSONGet(keys ...string) string
- func (ctx *WebContext) JSONWithCode(res interface{}, code int) *JSONResponse
- func (ctx *WebContext) Method() string
- func (ctx *WebContext) NewAPIResponse(businessCode string, message string, data interface{}) *JSONResponse
- func (ctx *WebContext) NewErrorResponse(res string, code int) *ErrorResponse
- func (ctx *WebContext) NewHTMLResponse(res string) *HTMLResponse
- func (ctx *WebContext) NewJSONResponse(res interface{}) *JSONResponse
- func (ctx *WebContext) NewRawResponse() *RawResponse
- func (ctx *WebContext) NewYAMLResponse(res interface{}) *YAMLResponse
- func (ctx *WebContext) Nil() *NilResponse
- func (ctx *WebContext) PathVar(key string) string
- func (ctx *WebContext) PathVars() map[string]string
- func (ctx *WebContext) Redirect(location string, code int) *RedirectResponse
- func (ctx *WebContext) Request() Request
- func (ctx *WebContext) Resolve(callback interface{}) Response
- func (ctx *WebContext) Response() ResponseCreator
- func (ctx *WebContext) Session() *sessions.Session
- func (ctx *WebContext) Set(key string, value interface{})
- func (ctx *WebContext) ToFloat32(val string, defaultVal float32) float32
- func (ctx *WebContext) ToFloat64(val string, defaultVal float64) float64
- func (ctx *WebContext) ToInt(val string, defaultVal int) int
- func (ctx *WebContext) ToInt64(val string, defaultVal int64) int64
- func (ctx *WebContext) Unmarshal(v interface{}) error
- func (ctx *WebContext) UnmarshalYAML(v interface{}) error
- func (ctx *WebContext) Validate(validator Validator, jsonResponse bool)
- func (ctx *WebContext) View(tplPath string, data interface{}) *HTMLResponse
- func (ctx *WebContext) YAML(res interface{}) *YAMLResponse
- type WebHandler
- type WebSocket
- type YAMLResponse
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type Config ¶
type Context ¶
type Context interface { JSON(res interface{}) *JSONResponse NewJSONResponse(res interface{}) *JSONResponse YAML(res interface{}) *YAMLResponse NewYAMLResponse(res interface{}) *YAMLResponse JSONWithCode(res interface{}, code int) *JSONResponse Nil() *NilResponse API(businessCode string, message string, data interface{}) *JSONResponse NewAPIResponse(businessCode string, message string, data interface{}) *JSONResponse NewRawResponse() *RawResponse NewHTMLResponse(res string) *HTMLResponse HTML(res string) *HTMLResponse HTMLWithCode(res string, code int) *HTMLResponse Error(res string, code int) *ErrorResponse JSONError(res string, code int) *JSONResponse NewErrorResponse(res string, code int) *ErrorResponse Redirect(location string, code int) *RedirectResponse Resolve(callback interface{}) Response Decode(v interface{}) error Unmarshal(v interface{}) error UnmarshalYAML(v interface{}) error PathVar(key string) string PathVars() map[string]string Input(key string) string JSONGet(keys ...string) string InputWithDefault(key string, defaultVal string) string ToInt(val string, defaultVal int) int ToInt64(val string, defaultVal int64) int64 ToFloat32(val string, defaultVal float32) float32 ToFloat64(val string, defaultVal float64) float64 IntInput(key string, defaultVal int) int Int64Input(key string, defaultVal int64) int64 Float32Input(key string, defaultVal float32) float32 Float64Input(key string, defaultVal float64) float64 File(key string) (*UploadedFile, error) IsXMLHTTPRequest() bool AJAX() bool IsJSON() bool ContentType() string AllHeaders() http.Header Headers(key string) []string Header(key string) string Is(method string) bool IsGet() bool IsPost() bool IsHead() bool IsDelete() bool IsPut() bool IsPatch() bool IsOptions() bool Method() string Body() []byte Set(key string, value interface{}) Get(key string) interface{} Context() context.Context Cookie(name string) (*http.Cookie, error) Cookies() []*http.Cookie Session() *sessions.Session Request() Request Response() ResponseCreator Container() *container.Container View(tplPath string, data interface{}) *HTMLResponse Validate(validator Validator, jsonResponse bool) }
type Controller ¶
type Controller interface { // Register register routes for a controller Register(router *Router) }
Controller is a interface for controller
type ErrorResponse ¶
type ErrorResponse struct {
// contains filtered or unexported fields
}
ErrorResponse Error response
func NewErrorResponse ¶
func NewErrorResponse(response ResponseCreator, res string, code int) *ErrorResponse
NewErrorResponse Create error response
func (*ErrorResponse) CreateResponse ¶
func (resp *ErrorResponse) CreateResponse() error
CreateResponse 创建响应内容
func (*ErrorResponse) WithCode ¶
func (resp *ErrorResponse) WithCode(code int) *ErrorResponse
WithCode set response code and return itself
type ExceptionHandler ¶
ExceptionHandler is a handler using handle exceptions
type HTMLResponse ¶
type HTMLResponse struct {
// contains filtered or unexported fields
}
HTMLResponse HTML响应
func NewHTMLResponse ¶
func NewHTMLResponse(response ResponseCreator, code int, res string) *HTMLResponse
NewHTMLResponse 创建HTML响应
func (*HTMLResponse) CreateResponse ¶
func (resp *HTMLResponse) CreateResponse() error
CreateResponse 创建响应内容
func (*HTMLResponse) WithCode ¶
func (resp *HTMLResponse) WithCode(code int) *HTMLResponse
WithCode set response code and return itself
type HandlerDecorator ¶
type HandlerDecorator func(WebHandler) WebHandler
HandlerDecorator 该函数是http handler的装饰器
type HttpRequest ¶
type HttpRequest struct {
// contains filtered or unexported fields
}
request 请求对象封装
func (*HttpRequest) AJAX ¶
func (req *HttpRequest) AJAX() bool
AJAX return whether the request is a ajax request
func (*HttpRequest) AllHeaders ¶
func (req *HttpRequest) AllHeaders() http.Header
AllHeaders return all http request headers
func (*HttpRequest) ContentType ¶
func (req *HttpRequest) ContentType() string
ContentType return content type for request
func (*HttpRequest) Context ¶
func (req *HttpRequest) Context() context.Context
Context returns the request's context
func (*HttpRequest) Cookie ¶
func (req *HttpRequest) Cookie(name string) (*http.Cookie, error)
Cookie returns the named cookie provided in the request or ErrNoCookie if not found. If multiple cookies match the given name, only one cookie will be returned.
func (*HttpRequest) Cookies ¶
func (req *HttpRequest) Cookies() []*http.Cookie
Cookies parses and returns the HTTP cookies sent with the request.
func (*HttpRequest) Decode ¶
func (req *HttpRequest) Decode(v interface{}) error
Decode decodes form request to a struct
func (*HttpRequest) File ¶
func (req *HttpRequest) File(key string) (*UploadedFile, error)
File Retrieving Uploaded Files
func (*HttpRequest) Float32Input ¶
func (req *HttpRequest) Float32Input(key string, defaultVal float32) float32
Float32Input return a float32 form parameter
func (*HttpRequest) Float64Input ¶
func (req *HttpRequest) Float64Input(key string, defaultVal float64) float64
Float64Input return a float64 form parameter
func (*HttpRequest) HTTPRequest ¶
func (req *HttpRequest) HTTPRequest() *http.Request
HTTPRequest return a http.request
func (*HttpRequest) Header ¶
func (req *HttpRequest) Header(key string) string
Header gets the first value associated with the given key.
func (*HttpRequest) Headers ¶
func (req *HttpRequest) Headers(key string) []string
Headers gets all values associated with given key
func (*HttpRequest) Input ¶
func (req *HttpRequest) Input(key string) string
Input return form parameter from request
func (*HttpRequest) InputWithDefault ¶
func (req *HttpRequest) InputWithDefault(key string, defaultVal string) string
InputWithDefault return a form parameter with a default value
func (*HttpRequest) Int64Input ¶
func (req *HttpRequest) Int64Input(key string, defaultVal int64) int64
Int64Input return a integer form parameter
func (*HttpRequest) IntInput ¶
func (req *HttpRequest) IntInput(key string, defaultVal int) int
IntInput return a integer form parameter
func (*HttpRequest) IsJSON ¶
func (req *HttpRequest) IsJSON() bool
IsJSON return whether the request is a json request
func (*HttpRequest) IsXMLHTTPRequest ¶
func (req *HttpRequest) IsXMLHTTPRequest() bool
IsXMLHTTPRequest return whether the request is a ajax request
func (*HttpRequest) JSONGet ¶
func (req *HttpRequest) JSONGet(keys ...string) string
func (*HttpRequest) PathVar ¶
func (req *HttpRequest) PathVar(key string) string
PathVar return a path parameter
func (*HttpRequest) PathVars ¶
func (req *HttpRequest) PathVars() map[string]string
PathVars return all path parameters
func (*HttpRequest) Raw ¶
func (req *HttpRequest) Raw() *http.Request
Raw get the underlying http.request
func (*HttpRequest) Session ¶
func (req *HttpRequest) Session() *sessions.Session
Session return session instance
func (*HttpRequest) Set ¶
func (req *HttpRequest) Set(key string, value interface{})
Set 设置一个变量,存储到当前请求
func (*HttpRequest) SetSession ¶
func (req *HttpRequest) SetSession(session *sessions.Session)
SetSession set a session to request
func (*HttpRequest) ToFloat32 ¶
func (req *HttpRequest) ToFloat32(val string, defaultVal float32) float32
func (*HttpRequest) ToFloat64 ¶
func (req *HttpRequest) ToFloat64(val string, defaultVal float64) float64
func (*HttpRequest) Unmarshal ¶
func (req *HttpRequest) Unmarshal(v interface{}) error
Unmarshal unmarshal request body as json object result must be reference to a variable
func (*HttpRequest) UnmarshalYAML ¶
func (req *HttpRequest) UnmarshalYAML(v interface{}) error
UnmarshalYAML unmarshal request body as yaml object result must be reference to a variable
func (*HttpRequest) Validate ¶
func (req *HttpRequest) Validate(validator Validator, jsonResponse bool)
Validate execute a validator, if there has an error, panic error to framework
type HttpResponse ¶
type HttpResponse struct {
// contains filtered or unexported fields
}
HttpResponse is a response object which wrap http.ResponseWriter
func (*HttpResponse) Cookie ¶
func (resp *HttpResponse) Cookie(cookie *http.Cookie)
Cookie set cookie
func (*HttpResponse) Flush ¶
func (resp *HttpResponse) Flush()
Flush send all response contents to client
func (*HttpResponse) Header ¶
func (resp *HttpResponse) Header(key, value string)
Header set response header
func (*HttpResponse) Raw ¶
func (resp *HttpResponse) Raw() http.ResponseWriter
func (*HttpResponse) ResponseWriter ¶
func (resp *HttpResponse) ResponseWriter() http.ResponseWriter
ResponseWriter return the http.ResponseWriter
func (*HttpResponse) SetCode ¶
func (resp *HttpResponse) SetCode(code int)
SetCode set response code
func (*HttpResponse) SetContent ¶
func (resp *HttpResponse) SetContent(content []byte)
SetContent set response content
type JSONAble ¶
type JSONAble interface {
// ToJSON convert a value to jsonable struct or map/array/slice etc
ToJSON() interface{}
}
JSONAble identify a value can convert to json object
type JSONError ¶
type JSONError struct {
// contains filtered or unexported fields
}
JSONError is a error object which implements Error and JSONAble interface
func WrapJSONError ¶
WrapJSONError wrap a error to JSONError
func (JSONError) StatusCode ¶
type JSONResponse ¶
type JSONResponse struct {
// contains filtered or unexported fields
}
JSONResponse json响应
func NewJSONResponse ¶
func NewJSONResponse(response ResponseCreator, code int, res interface{}) *JSONResponse
NewJSONResponse 创建JSONResponse对象
func (*JSONResponse) CreateResponse ¶
func (resp *JSONResponse) CreateResponse() error
CreateResponse create response
func (*JSONResponse) WithCode ¶
func (resp *JSONResponse) WithCode(code int) *JSONResponse
WithCode set response code and return itself
type MiddlewareRouter ¶
type MiddlewareRouter struct {
// contains filtered or unexported fields
}
func (*MiddlewareRouter) Controllers ¶
func (mr *MiddlewareRouter) Controllers(prefix string, controllers ...Controller)
func (*MiddlewareRouter) Group ¶
func (mr *MiddlewareRouter) Group(prefix string, f func(rou *Router))
type NilResponse ¶
type NilResponse struct {
// contains filtered or unexported fields
}
NilResponse 空响应
func NewNilResponse ¶
func NewNilResponse(response ResponseCreator) *NilResponse
NewNilResponse create a RawResponse
func (*NilResponse) CreateResponse ¶
func (resp *NilResponse) CreateResponse() error
CreateResponse flush response to client
func (*NilResponse) Response ¶
func (resp *NilResponse) Response() ResponseCreator
response get real response object
type PlainError ¶
type PlainError struct {
// contains filtered or unexported fields
}
PlainError is a error object which implements Error interface
func WrapPlainError ¶
func WrapPlainError(err error, code int) PlainError
WrapPlainError warps a error only have message and code
func (PlainError) Error ¶
func (p PlainError) Error() string
func (PlainError) StatusCode ¶
func (p PlainError) StatusCode() int
type RawResponse ¶
type RawResponse struct {
// contains filtered or unexported fields
}
RawResponse 原生响应
func NewRawResponse ¶
func NewRawResponse(response ResponseCreator) *RawResponse
NewRawResponse create a RawResponse
func (*RawResponse) CreateResponse ¶
func (resp *RawResponse) CreateResponse() error
CreateResponse flush response to client
func (*RawResponse) Response ¶
func (resp *RawResponse) Response() ResponseCreator
response get real response object
type RedirectResponse ¶
type RedirectResponse struct {
// contains filtered or unexported fields
}
RedirectResponse 页面跳转
func NewRedirectResponse ¶
func NewRedirectResponse(response ResponseCreator, request Request, location string, code int) *RedirectResponse
NewRedirectResponse 创建RedirectResponse对象
func (*RedirectResponse) CreateResponse ¶
func (resp *RedirectResponse) CreateResponse() error
CreateResponse 创建响应内容
func (*RedirectResponse) WithCode ¶
func (resp *RedirectResponse) WithCode(code int) *RedirectResponse
WithCode set response code and return itself
type Request ¶
type Request interface { Raw() *http.Request Decode(v interface{}) error Unmarshal(v interface{}) error UnmarshalYAML(v interface{}) error PathVar(key string) string PathVars() map[string]string Input(key string) string JSONGet(keys ...string) string InputWithDefault(key string, defaultVal string) string ToInt(val string, defaultVal int) int ToInt64(val string, defaultVal int64) int64 ToFloat32(val string, defaultVal float32) float32 ToFloat64(val string, defaultVal float64) float64 IntInput(key string, defaultVal int) int Int64Input(key string, defaultVal int64) int64 Float32Input(key string, defaultVal float32) float32 Float64Input(key string, defaultVal float64) float64 File(key string) (*UploadedFile, error) IsXMLHTTPRequest() bool AJAX() bool IsJSON() bool ContentType() string AllHeaders() http.Header Headers(key string) []string Header(key string) string Is(method string) bool IsGet() bool IsPost() bool IsHead() bool IsDelete() bool IsPut() bool IsPatch() bool IsOptions() bool Method() string Body() []byte Set(key string, value interface{}) Get(key string) interface{} Context() context.Context Cookie(name string) (*http.Cookie, error) Cookies() []*http.Cookie Session() *sessions.Session SetSession(session *sessions.Session) Validate(validator Validator, jsonResponse bool) }
type RequestMiddleware ¶
type RequestMiddleware struct{}
RequestMiddleware is a middleware collections
func NewRequestMiddleware ¶
func NewRequestMiddleware() RequestMiddleware
NewRequestMiddleware create a new RequestMiddleware
func (RequestMiddleware) AccessLog ¶
func (rm RequestMiddleware) AccessLog() HandlerDecorator
AccessLog create a access log middleware
func (RequestMiddleware) AuthHandler ¶
func (rm RequestMiddleware) AuthHandler(cb func(typ string, credential string) error) HandlerDecorator
AuthHandler is a middleware for http auth typ is one of: Basic (see RFC 7617, base64-encoded credentials. See below for more information.), Bearer (see RFC 6750, bearer tokens to access OAuth 2.0-protected resources), Digest (see RFC 7616, only md5 hashing is supported in Firefox, see bug 472823 for SHA encryption support), HOBA (see RFC 7486, Section 3, HTTP Origin-Bound Authentication, digital-signature-based), Mutual (see RFC 8120), AWS4-HMAC-SHA256 (see AWS docs).
func (RequestMiddleware) CORS ¶
func (rm RequestMiddleware) CORS(origin string) HandlerDecorator
CORS create a CORS middleware
func (RequestMiddleware) Session ¶
func (rm RequestMiddleware) Session(store sessions.Store, name string, options *sessions.Options) HandlerDecorator
Session is a middleware for session support
type Response ¶
type Response interface {
CreateResponse() error
}
Response is the response interface
func DefaultExceptionHandler ¶
DefaultExceptionHandler is a default implementation for ExceptionHandler
func ErrorToResponse ¶
ErrorToResponse convert an error to Response
type ResponseCreator ¶
type ResponseCreator interface { Raw() http.ResponseWriter SetCode(code int) ResponseWriter() http.ResponseWriter SetContent(content []byte) Header(key, value string) Cookie(cookie *http.Cookie) Flush() }
ResponseCreator is a response creator
type Route ¶
type Route struct { Name string `json:"name"` Methods []string `json:"methods"` PathTemplate string `json:"path_template"` PathRegexp string `json:"path_regexp"` QueriesRegexp []string `json:"queries_regexp"` QueriesTemplates []string `json:"queries_templates"` HostTemplate string `json:"host_template"` }
Route is a route represent for query all routes
type RouteRule ¶
type RouteRule struct {
// contains filtered or unexported fields
}
RouteRule 路由规则
func (*RouteRule) Headers ¶
Headers adds a matcher for request header values. It accepts a sequence of key/value pairs to be matched. For example:
r.Headers("Content-Type", "application/json", "X-Requested-With", "XMLHttpRequest")
The above route will only match if both request header values match. If the value is an empty string, it will match any value if the key is set.
func (*RouteRule) Host ¶
Host adds a matcher for the URL host. It accepts a template with zero or more URL variables enclosed by {}. Variables can define an optional regexp pattern to be matched:
- {name} matches anything until the next dot.
- {name:pattern} matches the given regexp pattern.
For example:
r.Host("www.example.com") r.Host("{subdomain}.domain.com") r.Host("{subdomain:[a-z]+}.domain.com")
Variable names must be unique in a given route. They can be retrieved calling mux.Vars(request).
func (*RouteRule) Name ¶
Name sets the name for the route, used to build URLs. It is an error to call Name more than once on a route.
func (*RouteRule) Queries ¶
Queries adds a matcher for URL query values. It accepts a sequence of key/value pairs. Values may define variables. For example:
r.Queries("foo", "bar", "id", "{id:[0-9]+}")
The above route will only match if the URL contains the defined queries values, e.g.: ?foo=bar&id=42.
If the value is an empty string, it will match any value if the key is set.
Variables can define an optional regexp pattern to be matched:
- {name} matches anything until the next slash.
- {name:pattern} matches the given regexp pattern.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router 定制的路由
func NewRouter ¶
func NewRouter(conf *Config, decorators ...HandlerDecorator) *Router
NewRouter 创建一个路由器
func NewRouterWithContainer ¶
func NewRouterWithContainer(c *container.Container, conf *Config, decorators ...HandlerDecorator) *Router
NewRouterWithContainer 创建一个路由器,带有依赖注入容器支持
func (*Router) Any ¶
func (router *Router) Any(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Any 指定所有请求方式的路由规则
func (*Router) Controllers ¶
func (router *Router) Controllers(prefix string, controllers ...Controller)
Controllers add controllers to router
func (*Router) Delete ¶
func (router *Router) Delete(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Delete 指定所有DELETE方式的路由规则
func (*Router) Get ¶
func (router *Router) Get(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Get 指定所有GET方式的路由规则
func (*Router) Group ¶
func (router *Router) Group(prefix string, f func(rou *Router), decors ...HandlerDecorator)
Group 创建路由组
func (*Router) Head ¶
func (router *Router) Head(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Head 指定所有Head方式的路由规则
func (*Router) Options ¶
func (router *Router) Options(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Options 指定所有OPTIONS方式的路由规则
func (*Router) Patch ¶
func (router *Router) Patch(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Patch 指定所有Patch方式的路由规则
func (*Router) Perform ¶
func (router *Router) Perform(exceptionHandler ExceptionHandler) *mux.Router
Perform 将路由规则添加到路由器
func (*Router) Post ¶
func (router *Router) Post(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Post 指定所有Post方式的路由规则
func (*Router) Put ¶
func (router *Router) Put(path string, handler interface{}, middlewares ...HandlerDecorator) *RouteRule
Put 指定所有Put方式的路由规则
func (*Router) WebAny ¶
func (router *Router) WebAny(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebAny 指定所有请求方式的路由规则,WebHandler方式
func (*Router) WebDelete ¶
func (router *Router) WebDelete(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebDelete 指定所有DELETE方式的路由规则,WebHandler方式
func (*Router) WebGet ¶
func (router *Router) WebGet(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebGet 指定GET请求方式的路由规则,WebHandler方式
func (*Router) WebHead ¶
func (router *Router) WebHead(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebHead 指定所有HEAD方式的路由规则,WebHandler方式
func (*Router) WebOptions ¶
func (router *Router) WebOptions(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebOptions 指定所有OPTIONS方式的路由规则,WebHandler方式
func (*Router) WebPatch ¶
func (router *Router) WebPatch(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebPatch 指定所有PATCH方式的路由规则,WebHandler方式
func (*Router) WebPost ¶
func (router *Router) WebPost(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebPost 指定POST请求方式的路由规则,WebHandler方式
func (*Router) WebPut ¶
func (router *Router) WebPut(path string, handler WebHandler, middlewares ...HandlerDecorator) *RouteRule
WebPut 指定所有Put方式的路由规则,WebHandler方式
func (*Router) WithMiddleware ¶
func (router *Router) WithMiddleware(decors ...HandlerDecorator) *MiddlewareRouter
WithMiddleware create a MiddlewareRouter
type UploadedFile ¶
type UploadedFile struct { Header *multipart.FileHeader SavePath string }
UploadedFile 上传的文件
func (*UploadedFile) Extension ¶
func (file *UploadedFile) Extension() string
Extension get the file's extension.
func (*UploadedFile) GetTempFilename ¶
func (file *UploadedFile) GetTempFilename() string
GetTempFilename 获取文件临时保存的地址
func (*UploadedFile) Store ¶
func (file *UploadedFile) Store(path string) error
Store store the uploaded file on a filesystem disk.
type Validator ¶
type Validator interface {
Validate() error
}
Validator is an interface for validator
type WebContext ¶
type WebContext struct {
// contains filtered or unexported fields
}
WebContext 作为一个web请求的上下文信息
func (*WebContext) API ¶
func (ctx *WebContext) API(businessCode string, message string, data interface{}) *JSONResponse
API is a shortcut for NewAPIResponse func
func (*WebContext) AllHeaders ¶
func (ctx *WebContext) AllHeaders() http.Header
AllHeaders is a proxy to request.AllHeaders
func (*WebContext) Container ¶
func (ctx *WebContext) Container() *container.Container
Container return underlying container.Container
func (*WebContext) ContentType ¶
func (ctx *WebContext) ContentType() string
ContentType is a proxy to request.ContentType
func (*WebContext) Context ¶
func (ctx *WebContext) Context() context.Context
Context returns the request's context
func (*WebContext) Cookie ¶
func (ctx *WebContext) Cookie(name string) (*http.Cookie, error)
Cookie returns the named cookie provided in the request or ErrNoCookie if not found. If multiple cookies match the given name, only one cookie will be returned.
func (*WebContext) Cookies ¶
func (ctx *WebContext) Cookies() []*http.Cookie
Cookies parses and returns the HTTP cookies sent with the request.
func (*WebContext) Decode ¶
func (ctx *WebContext) Decode(v interface{}) error
Decode decodes form request to a struct
func (*WebContext) Error ¶
func (ctx *WebContext) Error(res string, code int) *ErrorResponse
Error is a shortcut for NewErrorResponse func
func (*WebContext) File ¶
func (ctx *WebContext) File(key string) (*UploadedFile, error)
File is a proxy to request.File
func (*WebContext) Float32Input ¶
func (ctx *WebContext) Float32Input(key string, defaultVal float32) float32
Float32Input is a proxy to request.Float32Input
func (*WebContext) Float64Input ¶
func (ctx *WebContext) Float64Input(key string, defaultVal float64) float64
Float64Input is a proxy to request.Float64Input
func (*WebContext) Get ¶
func (ctx *WebContext) Get(key string) interface{}
Get is a proxy to request.Get
func (*WebContext) HTML ¶
func (ctx *WebContext) HTML(res string) *HTMLResponse
HTML is a shortcut for NewHTMLResponse func
func (*WebContext) HTMLWithCode ¶
func (ctx *WebContext) HTMLWithCode(res string, code int) *HTMLResponse
HTMLWithCode create a HTMLResponse with http status code
func (*WebContext) Header ¶
func (ctx *WebContext) Header(key string) string
Header is a proxy to request.Header
func (*WebContext) Headers ¶
func (ctx *WebContext) Headers(key string) []string
Headers is a proxy to request.Headers
func (*WebContext) Input ¶
func (ctx *WebContext) Input(key string) string
Input is a proxy to request.Input
func (*WebContext) InputWithDefault ¶
func (ctx *WebContext) InputWithDefault(key string, defaultVal string) string
InputWithDefault is a proxy to request.InputWithDefault
func (*WebContext) Int64Input ¶
func (ctx *WebContext) Int64Input(key string, defaultVal int64) int64
Int64Input is a proxy to request.Int64Input
func (*WebContext) IntInput ¶
func (ctx *WebContext) IntInput(key string, defaultVal int) int
IntInput is a proxy to request.IntInput
func (*WebContext) IsDelete ¶
func (ctx *WebContext) IsDelete() bool
IsDelete is a proxy to request.IsDelete
func (*WebContext) IsHead ¶
func (ctx *WebContext) IsHead() bool
IsHead is a proxy to request.IsHead
func (*WebContext) IsJSON ¶
func (ctx *WebContext) IsJSON() bool
IsJSON is a proxy to request.IsJSON
func (*WebContext) IsOptions ¶
func (ctx *WebContext) IsOptions() bool
IsOptions is a proxy to request.IsOptions
func (*WebContext) IsPatch ¶
func (ctx *WebContext) IsPatch() bool
IsPatch is a proxy to request.IsPatch
func (*WebContext) IsPost ¶
func (ctx *WebContext) IsPost() bool
IsPost is a proxy to request.IsPost
func (*WebContext) IsXMLHTTPRequest ¶
func (ctx *WebContext) IsXMLHTTPRequest() bool
IsXMLHTTPRequest is a proxy to IsXMLHTTPRequest
func (*WebContext) JSON ¶
func (ctx *WebContext) JSON(res interface{}) *JSONResponse
JSON is a shortcut for NewJSONResponse func
func (*WebContext) JSONError ¶
func (ctx *WebContext) JSONError(res string, code int) *JSONResponse
JSONError return a json error response for error
func (*WebContext) JSONGet ¶
func (ctx *WebContext) JSONGet(keys ...string) string
JSONGet is a proxy to request.JSONGet
func (*WebContext) JSONWithCode ¶
func (ctx *WebContext) JSONWithCode(res interface{}, code int) *JSONResponse
JSONWithCode create a json response with a http status code
func (*WebContext) Method ¶
func (ctx *WebContext) Method() string
Method is a proxy to request.Method
func (*WebContext) NewAPIResponse ¶
func (ctx *WebContext) NewAPIResponse(businessCode string, message string, data interface{}) *JSONResponse
NewAPIResponse create a new APIResponse
func (*WebContext) NewErrorResponse ¶
func (ctx *WebContext) NewErrorResponse(res string, code int) *ErrorResponse
NewErrorResponse create a error response
func (*WebContext) NewHTMLResponse ¶
func (ctx *WebContext) NewHTMLResponse(res string) *HTMLResponse
NewHTMLResponse create a new HTMLResponse
func (*WebContext) NewJSONResponse ¶
func (ctx *WebContext) NewJSONResponse(res interface{}) *JSONResponse
NewJSONResponse create a new JSONResponse with the http status code equal 200
func (*WebContext) NewRawResponse ¶
func (ctx *WebContext) NewRawResponse() *RawResponse
NewRawResponse create a new RawResponse
func (*WebContext) NewYAMLResponse ¶
func (ctx *WebContext) NewYAMLResponse(res interface{}) *YAMLResponse
NewYAMLResponse create a new YAMLResponse with http status code equal 200
func (*WebContext) PathVar ¶
func (ctx *WebContext) PathVar(key string) string
PathVar is a proxy to request.PathVar
func (*WebContext) PathVars ¶
func (ctx *WebContext) PathVars() map[string]string
PathVars is a proxy to request.PathVars
func (*WebContext) Redirect ¶
func (ctx *WebContext) Redirect(location string, code int) *RedirectResponse
Redirect replies to the request with a redirect to url
func (*WebContext) Request ¶
func (ctx *WebContext) Request() Request
Request return underlying Request
func (*WebContext) Resolve ¶
func (ctx *WebContext) Resolve(callback interface{}) Response
Resolve resolve implements dependency injection for http handler
func (*WebContext) Response ¶
func (ctx *WebContext) Response() ResponseCreator
Response return underlying Response
func (*WebContext) Session ¶
func (ctx *WebContext) Session() *sessions.Session
Session return session instance
func (*WebContext) Set ¶
func (ctx *WebContext) Set(key string, value interface{})
Set is a proxy to request.Set
func (*WebContext) ToFloat32 ¶
func (ctx *WebContext) ToFloat32(val string, defaultVal float32) float32
ToFloat32 is a proxy to request.ToFloat32
func (*WebContext) ToFloat64 ¶
func (ctx *WebContext) ToFloat64(val string, defaultVal float64) float64
ToFloat64 is a proxy to request.ToFloat64
func (*WebContext) ToInt ¶
func (ctx *WebContext) ToInt(val string, defaultVal int) int
ToInt is a proxy to request.ToInt
func (*WebContext) ToInt64 ¶
func (ctx *WebContext) ToInt64(val string, defaultVal int64) int64
ToInt64 is a proxy to request.ToInt64
func (*WebContext) Unmarshal ¶
func (ctx *WebContext) Unmarshal(v interface{}) error
Unmarshal is a proxy to request.Unmarshal
func (*WebContext) UnmarshalYAML ¶
func (ctx *WebContext) UnmarshalYAML(v interface{}) error
UnmarshalYAML is a proxy to request.UnmarshalYAML
func (*WebContext) Validate ¶
func (ctx *WebContext) Validate(validator Validator, jsonResponse bool)
Validate execute a validator, if error happens, panic it
func (*WebContext) View ¶
func (ctx *WebContext) View(tplPath string, data interface{}) *HTMLResponse
View is a helper function for template rendering
func (*WebContext) YAML ¶
func (ctx *WebContext) YAML(res interface{}) *YAMLResponse
YAML is a shortcut for NewYAMLResponse func
type YAMLResponse ¶
type YAMLResponse struct {
// contains filtered or unexported fields
}
YAMLResponse yaml响应
func NewYAMLResponse ¶
func NewYAMLResponse(response ResponseCreator, code int, res interface{}) *YAMLResponse
NewYAMLResponse 创建YAMLResponse对象
func (*YAMLResponse) CreateResponse ¶
func (resp *YAMLResponse) CreateResponse() error
CreateResponse create response
func (*YAMLResponse) WithCode ¶
func (resp *YAMLResponse) WithCode(code int) *YAMLResponse
WithCode set response code and return itself