Documentation ¶
Overview ¶
Package context provide the context utils Usage:
import "github.com/izi-global/izigo/context" ctx := context.Context{Request:req,ResponseWriter:rw} more docs http://go.izi.asia/docs/module/context.md
Index ¶
- func InitGzip(minLength, compressLevel int, methods []string)
- func ParseEncoding(r *http.Request) string
- func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string, error)
- func WriteFile(encoding string, writer io.Writer, file *os.File) (bool, string, error)
- type Context
- func (ctx *Context) Abort(status int, body string)
- func (ctx *Context) CheckXSRFCookie() bool
- func (ctx *Context) GetCookie(key string) string
- func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool)
- func (ctx *Context) Redirect(status int, localurl string)
- func (ctx *Context) RenderMethodResult(result interface{})
- func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request)
- func (ctx *Context) SetCookie(name string, value string, others ...interface{})
- func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{})
- func (ctx *Context) WriteString(content string)
- func (ctx *Context) XSRFToken(key string, expire int64) string
- type IZIGoInput
- func (input *IZIGoInput) AcceptsHTML() bool
- func (input *IZIGoInput) AcceptsJSON() bool
- func (input *IZIGoInput) AcceptsXML() bool
- func (input *IZIGoInput) AcceptsYAML() bool
- func (input *IZIGoInput) Bind(dest interface{}, key string) error
- func (input *IZIGoInput) Cookie(key string) string
- func (input *IZIGoInput) CopyBody(MaxMemory int64) []byte
- func (input *IZIGoInput) Data() map[interface{}]interface{}
- func (input *IZIGoInput) Domain() string
- func (input *IZIGoInput) GetData(key interface{}) interface{}
- func (input *IZIGoInput) Header(key string) string
- func (input *IZIGoInput) Host() string
- func (input *IZIGoInput) IP() string
- func (input *IZIGoInput) Is(method string) bool
- func (input *IZIGoInput) IsAjax() bool
- func (input *IZIGoInput) IsDelete() bool
- func (input *IZIGoInput) IsGet() bool
- func (input *IZIGoInput) IsHead() bool
- func (input *IZIGoInput) IsOptions() bool
- func (input *IZIGoInput) IsPatch() bool
- func (input *IZIGoInput) IsPost() bool
- func (input *IZIGoInput) IsPut() bool
- func (input *IZIGoInput) IsSecure() bool
- func (input *IZIGoInput) IsUpload() bool
- func (input *IZIGoInput) IsWebsocket() bool
- func (input *IZIGoInput) Method() string
- func (input *IZIGoInput) Param(key string) string
- func (input *IZIGoInput) Params() map[string]string
- func (input *IZIGoInput) ParamsLen() int
- func (input *IZIGoInput) ParseFormOrMulitForm(maxMemory int64) error
- func (input *IZIGoInput) Port() int
- func (input *IZIGoInput) Protocol() string
- func (input *IZIGoInput) Proxy() []string
- func (input *IZIGoInput) Query(key string) string
- func (input *IZIGoInput) Refer() string
- func (input *IZIGoInput) Referer() string
- func (input *IZIGoInput) Reset(ctx *Context)
- func (input *IZIGoInput) ResetParams()
- func (input *IZIGoInput) Scheme() string
- func (input *IZIGoInput) Session(key interface{}) interface{}
- func (input *IZIGoInput) SetData(key, val interface{})
- func (input *IZIGoInput) SetParam(key, val string)
- func (input *IZIGoInput) Site() string
- func (input *IZIGoInput) SubDomains() string
- func (input *IZIGoInput) URI() string
- func (input *IZIGoInput) URL() string
- func (input *IZIGoInput) UserAgent() string
- type IZIGoOutput
- func (output *IZIGoOutput) Body(content []byte) error
- func (output *IZIGoOutput) ContentType(ext string)
- func (output *IZIGoOutput) Cookie(name string, value string, others ...interface{})
- func (output *IZIGoOutput) Download(file string, filename ...string)
- func (output *IZIGoOutput) Header(key, val string)
- func (output *IZIGoOutput) IsCachable() bool
- func (output *IZIGoOutput) IsClientError() bool
- func (output *IZIGoOutput) IsEmpty() bool
- func (output *IZIGoOutput) IsForbidden() bool
- func (output *IZIGoOutput) IsNotFound() bool
- func (output *IZIGoOutput) IsOk() bool
- func (output *IZIGoOutput) IsRedirect() bool
- func (output *IZIGoOutput) IsServerError() bool
- func (output *IZIGoOutput) IsSuccessful() bool
- func (output *IZIGoOutput) JSON(data interface{}, hasIndent bool, encoding bool) error
- func (output *IZIGoOutput) JSONP(data interface{}, hasIndent bool) error
- func (output *IZIGoOutput) Reset(ctx *Context)
- func (output *IZIGoOutput) Session(name interface{}, value interface{})
- func (output *IZIGoOutput) SetStatus(status int)
- func (output *IZIGoOutput) XML(data interface{}, hasIndent bool) error
- func (output *IZIGoOutput) YAML(data interface{}) error
- type Renderer
- type Response
- type StatusCode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseEncoding ¶
ParseEncoding will extract the right encoding for response the Accept-Encoding's sec is here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
Types ¶
type Context ¶
type Context struct { Input *IZIGoInput Output *IZIGoOutput Request *http.Request ResponseWriter *Response // contains filtered or unexported fields }
Context Http request context struct including IZIGoInput, IZIGoOutput, http.Request and http.ResponseWriter. IZIGoInput and IZIGoOutput provides some api to operate request and response more easily.
func (*Context) CheckXSRFCookie ¶
CheckXSRFCookie checks xsrf token in this request is valid or not. the token can provided in request header "X-Xsrftoken" and "X-CsrfToken" or in form field value named as "_xsrf".
func (*Context) GetCookie ¶
GetCookie Get cookie from request by a given key. It's alias of IZIGoInput.Cookie.
func (*Context) GetSecureCookie ¶
GetSecureCookie Get secure cookie from request by a given key.
func (*Context) RenderMethodResult ¶
func (ctx *Context) RenderMethodResult(result interface{})
RenderMethodResult renders the return value of a controller method to the output
func (*Context) Reset ¶
func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request)
Reset init Context, IZIGoInput and IZIGoOutput
func (*Context) SetSecureCookie ¶
SetSecureCookie Set Secure cookie for response.
func (*Context) WriteString ¶
WriteString Write string to response body. it sends response body.
type IZIGoInput ¶
type IZIGoInput struct { Context *Context CruSession session.Store RequestBody []byte RunMethod string RunController reflect.Type // contains filtered or unexported fields }
IZIGoInput operates the http request header, data, cookie and body. it also contains router params and current session.
func (*IZIGoInput) AcceptsHTML ¶
func (input *IZIGoInput) AcceptsHTML() bool
AcceptsHTML Checks if request accepts html response
func (*IZIGoInput) AcceptsJSON ¶
func (input *IZIGoInput) AcceptsJSON() bool
AcceptsJSON Checks if request accepts json response
func (*IZIGoInput) AcceptsXML ¶
func (input *IZIGoInput) AcceptsXML() bool
AcceptsXML Checks if request accepts xml response
func (*IZIGoInput) AcceptsYAML ¶
func (input *IZIGoInput) AcceptsYAML() bool
AcceptsYAML Checks if request accepts json response
func (*IZIGoInput) Bind ¶
func (input *IZIGoInput) Bind(dest interface{}, key string) error
Bind data from request.Form[key] to dest like /?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=diepdt var id int izigoInput.Bind(&id, "id") id ==123 var isok bool izigoInput.Bind(&isok, "isok") isok ==true var ft float64 izigoInput.Bind(&ft, "ft") ft ==1.2 ol := make([]int, 0, 2) izigoInput.Bind(&ol, "ol") ol ==[1 2] ul := make([]string, 0, 2) izigoInput.Bind(&ul, "ul") ul ==[str array] user struct{Name} izigoInput.Bind(&user, "user") user == {Name:"diepdt"}
func (*IZIGoInput) Cookie ¶
func (input *IZIGoInput) Cookie(key string) string
Cookie returns request cookie item string by a given key. if non-existed, return empty string.
func (*IZIGoInput) CopyBody ¶
func (input *IZIGoInput) CopyBody(MaxMemory int64) []byte
CopyBody returns the raw request body data as bytes.
func (*IZIGoInput) Data ¶
func (input *IZIGoInput) Data() map[interface{}]interface{}
Data return the implicit data in the input
func (*IZIGoInput) Domain ¶
func (input *IZIGoInput) Domain() string
Domain returns host name. Alias of Host method.
func (*IZIGoInput) GetData ¶
func (input *IZIGoInput) GetData(key interface{}) interface{}
GetData returns the stored data in this context.
func (*IZIGoInput) Header ¶
func (input *IZIGoInput) Header(key string) string
Header returns request header item string by a given string. if non-existed, return empty string.
func (*IZIGoInput) Host ¶
func (input *IZIGoInput) Host() string
Host returns host name. if no host info in request, return localhost.
func (*IZIGoInput) IP ¶
func (input *IZIGoInput) IP() string
IP returns request client ip. if in proxy, return first proxy id. if error, return RemoteAddr.
func (*IZIGoInput) Is ¶
func (input *IZIGoInput) Is(method string) bool
Is returns boolean of this request is on given method, such as Is("POST").
func (*IZIGoInput) IsAjax ¶
func (input *IZIGoInput) IsAjax() bool
IsAjax returns boolean of this request is generated by ajax.
func (*IZIGoInput) IsDelete ¶
func (input *IZIGoInput) IsDelete() bool
IsDelete Is this a DELETE method request?
func (*IZIGoInput) IsGet ¶
func (input *IZIGoInput) IsGet() bool
IsGet Is this a GET method request?
func (*IZIGoInput) IsHead ¶
func (input *IZIGoInput) IsHead() bool
IsHead Is this a Head method request?
func (*IZIGoInput) IsOptions ¶
func (input *IZIGoInput) IsOptions() bool
IsOptions Is this a OPTIONS method request?
func (*IZIGoInput) IsPatch ¶
func (input *IZIGoInput) IsPatch() bool
IsPatch Is this a PATCH method request?
func (*IZIGoInput) IsPost ¶
func (input *IZIGoInput) IsPost() bool
IsPost Is this a POST method request?
func (*IZIGoInput) IsPut ¶
func (input *IZIGoInput) IsPut() bool
IsPut Is this a PUT method request?
func (*IZIGoInput) IsSecure ¶
func (input *IZIGoInput) IsSecure() bool
IsSecure returns boolean of this request is in https.
func (*IZIGoInput) IsUpload ¶
func (input *IZIGoInput) IsUpload() bool
IsUpload returns boolean of whether file uploads in this request or not..
func (*IZIGoInput) IsWebsocket ¶
func (input *IZIGoInput) IsWebsocket() bool
IsWebsocket returns boolean of this request is in webSocket.
func (*IZIGoInput) Method ¶
func (input *IZIGoInput) Method() string
Method returns http request method.
func (*IZIGoInput) Param ¶
func (input *IZIGoInput) Param(key string) string
Param returns router param by a given key.
func (*IZIGoInput) Params ¶
func (input *IZIGoInput) Params() map[string]string
Params returns the map[key]value.
func (*IZIGoInput) ParamsLen ¶
func (input *IZIGoInput) ParamsLen() int
ParamsLen return the length of the params
func (*IZIGoInput) ParseFormOrMulitForm ¶
func (input *IZIGoInput) ParseFormOrMulitForm(maxMemory int64) error
ParseFormOrMulitForm parseForm or parseMultiForm based on Content-type
func (*IZIGoInput) Port ¶
func (input *IZIGoInput) Port() int
Port returns request client port. when error or empty, return 80.
func (*IZIGoInput) Protocol ¶
func (input *IZIGoInput) Protocol() string
Protocol returns request protocol name, such as HTTP/1.1 .
func (*IZIGoInput) Proxy ¶
func (input *IZIGoInput) Proxy() []string
Proxy returns proxy client ips slice.
func (*IZIGoInput) Query ¶
func (input *IZIGoInput) Query(key string) string
Query returns input data item string by a given string.
func (*IZIGoInput) Refer ¶
func (input *IZIGoInput) Refer() string
Refer returns http referer header.
func (*IZIGoInput) Referer ¶
func (input *IZIGoInput) Referer() string
Referer returns http referer header.
func (*IZIGoInput) ResetParams ¶
func (input *IZIGoInput) ResetParams()
ResetParams clears any of the input's Params This function is used to clear parameters so they may be reset between filter passes.
func (*IZIGoInput) Scheme ¶
func (input *IZIGoInput) Scheme() string
Scheme returns request scheme as "http" or "https".
func (*IZIGoInput) Session ¶
func (input *IZIGoInput) Session(key interface{}) interface{}
Session returns current session item value by a given key. if non-existed, return nil.
func (*IZIGoInput) SetData ¶
func (input *IZIGoInput) SetData(key, val interface{})
SetData stores data with given key in this context. This data are only available in this context.
func (*IZIGoInput) SetParam ¶
func (input *IZIGoInput) SetParam(key, val string)
SetParam will set the param with key and value
func (*IZIGoInput) Site ¶
func (input *IZIGoInput) Site() string
Site returns base site url as scheme://domain type.
func (*IZIGoInput) SubDomains ¶
func (input *IZIGoInput) SubDomains() string
SubDomains returns sub domain string. if aa.bb.domain.com, returns aa.bb .
func (*IZIGoInput) URI ¶
func (input *IZIGoInput) URI() string
URI returns full request url with query string, fragment.
func (*IZIGoInput) URL ¶
func (input *IZIGoInput) URL() string
URL returns request url path (without query string, fragment).
func (*IZIGoInput) UserAgent ¶
func (input *IZIGoInput) UserAgent() string
UserAgent returns request client user agent string.
type IZIGoOutput ¶
IZIGoOutput does work for sending response header.
func NewOutput ¶
func NewOutput() *IZIGoOutput
NewOutput returns new IZIGoOutput. it contains nothing now.
func (*IZIGoOutput) Body ¶
func (output *IZIGoOutput) Body(content []byte) error
Body sets response body content. if EnableGzip, compress content string. it sends out response body directly.
func (*IZIGoOutput) ContentType ¶
func (output *IZIGoOutput) ContentType(ext string)
ContentType sets the content type from ext string. MIME type is given in mime package.
func (*IZIGoOutput) Cookie ¶
func (output *IZIGoOutput) Cookie(name string, value string, others ...interface{})
Cookie sets cookie value via given key. others are ordered as cookie's max age time, path,domain, secure and httponly.
func (*IZIGoOutput) Download ¶
func (output *IZIGoOutput) Download(file string, filename ...string)
Download forces response for download file. it prepares the download response header automatically.
func (*IZIGoOutput) Header ¶
func (output *IZIGoOutput) Header(key, val string)
Header sets response header item string via given key.
func (*IZIGoOutput) IsCachable ¶
func (output *IZIGoOutput) IsCachable() bool
IsCachable returns boolean of this request is cached. HTTP 304 means cached.
func (*IZIGoOutput) IsClientError ¶
func (output *IZIGoOutput) IsClientError() bool
IsClientError returns boolean of this request client sends error data. HTTP 4xx means client error.
func (*IZIGoOutput) IsEmpty ¶
func (output *IZIGoOutput) IsEmpty() bool
IsEmpty returns boolean of this request is empty. HTTP 201,204 and 304 means empty.
func (*IZIGoOutput) IsForbidden ¶
func (output *IZIGoOutput) IsForbidden() bool
IsForbidden returns boolean of this request is forbidden. HTTP 403 means forbidden.
func (*IZIGoOutput) IsNotFound ¶
func (output *IZIGoOutput) IsNotFound() bool
IsNotFound returns boolean of this request is not found. HTTP 404 means not found.
func (*IZIGoOutput) IsOk ¶
func (output *IZIGoOutput) IsOk() bool
IsOk returns boolean of this request runs well. HTTP 200 means ok.
func (*IZIGoOutput) IsRedirect ¶
func (output *IZIGoOutput) IsRedirect() bool
IsRedirect returns boolean of this request is redirection header. HTTP 301,302,307 means redirection.
func (*IZIGoOutput) IsServerError ¶
func (output *IZIGoOutput) IsServerError() bool
IsServerError returns boolean of this server handler errors. HTTP 5xx means server internal error.
func (*IZIGoOutput) IsSuccessful ¶
func (output *IZIGoOutput) IsSuccessful() bool
IsSuccessful returns boolean of this request runs successfully. HTTP 2xx means ok.
func (*IZIGoOutput) JSON ¶
func (output *IZIGoOutput) JSON(data interface{}, hasIndent bool, encoding bool) error
JSON writes json to response body. if encoding is true, it converts utf-8 to \u0000 type.
func (*IZIGoOutput) JSONP ¶
func (output *IZIGoOutput) JSONP(data interface{}, hasIndent bool) error
JSONP writes jsonp to response body.
func (*IZIGoOutput) Session ¶
func (output *IZIGoOutput) Session(name interface{}, value interface{})
Session sets session item value with given key.
func (*IZIGoOutput) SetStatus ¶
func (output *IZIGoOutput) SetStatus(status int)
SetStatus sets response status code. It writes response header directly.
func (*IZIGoOutput) XML ¶
func (output *IZIGoOutput) XML(data interface{}, hasIndent bool) error
XML writes xml string to response body.
func (*IZIGoOutput) YAML ¶
func (output *IZIGoOutput) YAML(data interface{}) error
YAML writes yaml to response body.
type Renderer ¶
type Renderer interface {
Render(ctx *Context)
}
Renderer defines an http response renderer
type Response ¶
type Response struct { http.ResponseWriter Started bool Status int }
Response is a wrapper for the http.ResponseWriter started set to true if response was written to then don't execute other handler
func (*Response) CloseNotify ¶
CloseNotify http.CloseNotifier
func (*Response) Write ¶
Write writes the data to the connection as part of an HTTP reply, and sets `started` to true. started means the response has sent out.
func (*Response) WriteHeader ¶
WriteHeader sends an HTTP response header with status code, and sets `started` to true.
type StatusCode ¶
type StatusCode int
StatusCode sets the http response status code
const ( //BadRequest indicates http error 400 BadRequest StatusCode = http.StatusBadRequest //NotFound indicates http error 404 NotFound StatusCode = http.StatusNotFound )
func (StatusCode) Error ¶
func (s StatusCode) Error() string
func (StatusCode) Render ¶
func (s StatusCode) Render(ctx *Context)
Render sets the http status code