Documentation ¶
Overview ¶
Package context provide the context utils Usage:
import "github.com/rachelos/rungo/server/web/context" ctx := context.Context{Request:req,ResponseWriter:rw}
Index ¶
- Constants
- func InitGzip(minLength, compressLevel int, methods []string)
- func NewOutput() *rungoOutput
- func ParseEncoding(r *http.Request) string
- func ParseForm(form url.Values, obj interface{}) error
- 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) Bind(obj interface{}) error
- func (ctx *Context) BindForm(obj interface{}) error
- func (ctx *Context) BindJSON(obj interface{}) error
- func (ctx *Context) BindProtobuf(obj proto.Message) error
- func (ctx *Context) BindXML(obj interface{}) error
- func (ctx *Context) BindYAML(obj interface{}) error
- func (ctx *Context) CheckXSRFCookie() bool
- func (ctx *Context) GetCookie(key string) string
- func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool)
- func (ctx *Context) JSONResp(data interface{}) error
- func (ctx *Context) ProtoResp(data proto.Message) error
- 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) Resp(data interface{}) error
- func (ctx *Context) Session() (store session.Store, err error)
- 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) XMLResp(data interface{}) error
- func (ctx *Context) XSRFToken(key string, expire int64) string
- func (ctx *Context) YamlResp(data interface{}) error
- type Renderer
- type Response
- type RungoInput
- func (input *RungoInput) AcceptsHTML() bool
- func (input *RungoInput) AcceptsJSON() bool
- func (input *RungoInput) AcceptsXML() bool
- func (input *RungoInput) AcceptsYAML() bool
- func (input *RungoInput) Bind(dest interface{}, key string) error
- func (input *RungoInput) Cookie(key string) string
- func (input *RungoInput) CopyBody(MaxMemory int64) []byte
- func (input *RungoInput) Data() map[interface{}]interface{}
- func (input *RungoInput) Domain() string
- func (input *RungoInput) GetData(key interface{}) interface{}
- func (input *RungoInput) Header(key string) string
- func (input *RungoInput) Host() string
- func (input *RungoInput) IP() string
- func (input *RungoInput) Is(method string) bool
- func (input *RungoInput) IsAjax() bool
- func (input *RungoInput) IsDelete() bool
- func (input *RungoInput) IsGet() bool
- func (input *RungoInput) IsHead() bool
- func (input *RungoInput) IsOptions() bool
- func (input *RungoInput) IsPatch() bool
- func (input *RungoInput) IsPost() bool
- func (input *RungoInput) IsPut() bool
- func (input *RungoInput) IsSecure() bool
- func (input *RungoInput) IsUpload() bool
- func (input *RungoInput) IsWebsocket() bool
- func (input *RungoInput) Method() string
- func (input *RungoInput) Param(key string) string
- func (input *RungoInput) Params() map[string]string
- func (input *RungoInput) ParamsLen() int
- func (input *RungoInput) ParseFormOrMultiForm(maxMemory int64) error
- func (input *RungoInput) Port() int
- func (input *RungoInput) Protocol() string
- func (input *RungoInput) Proxy() []string
- func (input *RungoInput) Query(key string) string
- func (input *RungoInput) Refer() string
- func (input *RungoInput) Referer() string
- func (input *RungoInput) Reset(ctx *Context)
- func (input *RungoInput) ResetParams()
- func (input *RungoInput) Scheme() string
- func (input *RungoInput) Session(key interface{}) interface{}
- func (input *RungoInput) SetData(key, val interface{})
- func (input *RungoInput) SetParam(key, val string)
- func (input *RungoInput) Site() string
- func (input *RungoInput) SubDomains() string
- func (input *RungoInput) URI() string
- func (input *RungoInput) URL() string
- func (input *RungoInput) UserAgent() string
- type StatusCode
Constants ¶
const ( ApplicationJSON = "application/json" ApplicationXML = "application/xml" ApplicationForm = "application/x-www-form-urlencoded" ApplicationProto = "application/x-protobuf" ApplicationYAML = "application/x-yaml" TextXML = "text/xml" )
Commonly used mime-types
Variables ¶
This section is empty.
Functions ¶
func NewOutput ¶
func NewOutput() *rungoOutput
NewOutput returns new rungoOutput. Empty when initialized
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 *RungoInput Output *rungoOutput Request *http.Request ResponseWriter *Response // contains filtered or unexported fields }
Context Http request context struct including RungoInput, rungoOutput, http.Request and http.ResponseWriter. RungoInput and rungoOutput provides an api to operate request and response more easily.
func (*Context) BindProtobuf ¶
BindProtobuf only read data from http request body
func (*Context) CheckXSRFCookie ¶
CheckXSRFCookie checks if the XSRF token in this request is valid or not. The token can be provided in the request header in the form "X-Xsrftoken" or "X-CsrfToken" or in form field value named as "_xsrf".
func (*Context) GetCookie ¶
GetCookie gets a cookie from a request for a given key. (Alias of RungoInput.Cookie)
func (*Context) GetSecureCookie ¶
GetSecureCookie gets a secure cookie from a request for 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 initializes Context, RungoInput and rungoOutput
func (*Context) Resp ¶
Resp sends response based on the Accept Header By default response will be in JSON
func (*Context) SetSecureCookie ¶
SetSecureCookie sets a secure cookie for a response.
func (*Context) WriteString ¶
WriteString writes a string to response body.
type Renderer ¶
type Renderer interface {
Render(ctx *Context)
}
Renderer defines a http response renderer
type Response ¶
Response is a wrapper for the http.ResponseWriter Started: if true, response was already written to so the other handler will not be executed
func (*Response) CloseNotify ¶
CloseNotify http.CloseNotifier
func (*Response) Write ¶
Write writes the data to the connection as part of a HTTP reply, and sets `Started` to true. Started: if true, the response was already sent
func (*Response) WriteHeader ¶
WriteHeader sends a HTTP response header with status code, and sets `Started` to true.
type RungoInput ¶ added in v1.0.2
type RungoInput struct { Context *Context CruSession session.Store RequestBody []byte RunMethod string RunController reflect.Type // contains filtered or unexported fields }
RungoInput operates the http request header, data, cookie and body. Contains router params and current session.
func (*RungoInput) AcceptsHTML ¶ added in v1.0.2
func (input *RungoInput) AcceptsHTML() bool
AcceptsHTML Checks if request accepts html response
func (*RungoInput) AcceptsJSON ¶ added in v1.0.2
func (input *RungoInput) AcceptsJSON() bool
AcceptsJSON Checks if request accepts json response
func (*RungoInput) AcceptsXML ¶ added in v1.0.2
func (input *RungoInput) AcceptsXML() bool
AcceptsXML Checks if request accepts xml response
func (*RungoInput) AcceptsYAML ¶ added in v1.0.2
func (input *RungoInput) AcceptsYAML() bool
AcceptsYAML Checks if request accepts json response
func (*RungoInput) Bind ¶ added in v1.0.2
func (input *RungoInput) 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=astaxie var id int RungoInput.Bind(&id, "id") id ==123 var isok bool RungoInput.Bind(&isok, "isok") isok ==true var ft float64 RungoInput.Bind(&ft, "ft") ft ==1.2 ol := make([]int, 0, 2) RungoInput.Bind(&ol, "ol") ol ==[1 2] ul := make([]string, 0, 2) RungoInput.Bind(&ul, "ul") ul ==[str array] user struct{Name} RungoInput.Bind(&user, "user") user == {Name:"astaxie"}
func (*RungoInput) Cookie ¶ added in v1.0.2
func (input *RungoInput) Cookie(key string) string
Cookie returns request cookie item string by a given key. if non-existed, return empty string.
func (*RungoInput) CopyBody ¶ added in v1.0.2
func (input *RungoInput) CopyBody(MaxMemory int64) []byte
CopyBody returns the raw request body data as bytes.
func (*RungoInput) Data ¶ added in v1.0.2
func (input *RungoInput) Data() map[interface{}]interface{}
Data returns the implicit data in the input
func (*RungoInput) Domain ¶ added in v1.0.2
func (input *RungoInput) Domain() string
Domain returns the host name (alias of host method)
func (*RungoInput) GetData ¶ added in v1.0.2
func (input *RungoInput) GetData(key interface{}) interface{}
GetData returns the stored data in this context.
func (*RungoInput) Header ¶ added in v1.0.2
func (input *RungoInput) Header(key string) string
Header returns request header item string by a given string. if non-existed, return empty string.
func (*RungoInput) Host ¶ added in v1.0.2
func (input *RungoInput) Host() string
Host returns the host name. If no host info in request, return localhost.
func (*RungoInput) IP ¶ added in v1.0.2
func (input *RungoInput) IP() string
IP returns request client ip. if in proxy, return first proxy id. if error, return RemoteAddr.
func (*RungoInput) Is ¶ added in v1.0.2
func (input *RungoInput) Is(method string) bool
Is returns the boolean value of this request is on given method, such as Is("POST").
func (*RungoInput) IsAjax ¶ added in v1.0.2
func (input *RungoInput) IsAjax() bool
IsAjax returns boolean of is this request generated by ajax.
func (*RungoInput) IsDelete ¶ added in v1.0.2
func (input *RungoInput) IsDelete() bool
IsDelete Is this a DELETE method request?
func (*RungoInput) IsGet ¶ added in v1.0.2
func (input *RungoInput) IsGet() bool
IsGet Is this a GET method request?
func (*RungoInput) IsHead ¶ added in v1.0.2
func (input *RungoInput) IsHead() bool
IsHead Is this a Head method request?
func (*RungoInput) IsOptions ¶ added in v1.0.2
func (input *RungoInput) IsOptions() bool
IsOptions Is this an OPTIONS method request?
func (*RungoInput) IsPatch ¶ added in v1.0.2
func (input *RungoInput) IsPatch() bool
IsPatch Is this a PATCH method request?
func (*RungoInput) IsPost ¶ added in v1.0.2
func (input *RungoInput) IsPost() bool
IsPost Is this a POST method request?
func (*RungoInput) IsPut ¶ added in v1.0.2
func (input *RungoInput) IsPut() bool
IsPut Is this a PUT method request?
func (*RungoInput) IsSecure ¶ added in v1.0.2
func (input *RungoInput) IsSecure() bool
IsSecure returns boolean of this request is in https.
func (*RungoInput) IsUpload ¶ added in v1.0.2
func (input *RungoInput) IsUpload() bool
IsUpload returns boolean of whether file uploads in this request or not..
func (*RungoInput) IsWebsocket ¶ added in v1.0.2
func (input *RungoInput) IsWebsocket() bool
IsWebsocket returns boolean of this request is in webSocket.
func (*RungoInput) Method ¶ added in v1.0.2
func (input *RungoInput) Method() string
Method returns http request method.
func (*RungoInput) Param ¶ added in v1.0.2
func (input *RungoInput) Param(key string) string
Param returns router param by a given key.
func (*RungoInput) Params ¶ added in v1.0.2
func (input *RungoInput) Params() map[string]string
Params returns the map[key]value.
func (*RungoInput) ParamsLen ¶ added in v1.0.2
func (input *RungoInput) ParamsLen() int
ParamsLen return the length of the params
func (*RungoInput) ParseFormOrMultiForm ¶ added in v1.0.2
func (input *RungoInput) ParseFormOrMultiForm(maxMemory int64) error
ParseFormOrMultiForm parseForm or parseMultiForm based on Content-type
func (*RungoInput) Port ¶ added in v1.0.2
func (input *RungoInput) Port() int
Port returns request client port. when error or empty, return 80.
func (*RungoInput) Protocol ¶ added in v1.0.2
func (input *RungoInput) Protocol() string
Protocol returns the request protocol name, such as HTTP/1.1 .
func (*RungoInput) Proxy ¶ added in v1.0.2
func (input *RungoInput) Proxy() []string
Proxy returns proxy client ips slice.
func (*RungoInput) Query ¶ added in v1.0.2
func (input *RungoInput) Query(key string) string
Query returns input data item string by a given string.
func (*RungoInput) Refer ¶ added in v1.0.2
func (input *RungoInput) Refer() string
Refer returns http referer header.
func (*RungoInput) Referer ¶ added in v1.0.2
func (input *RungoInput) Referer() string
Referer returns http referer header.
func (*RungoInput) Reset ¶ added in v1.0.2
func (input *RungoInput) Reset(ctx *Context)
Reset initializes the RungoInput
func (*RungoInput) ResetParams ¶ added in v1.0.2
func (input *RungoInput) ResetParams()
ResetParams clears any of the input's params Used to clear parameters so they may be reset between filter passes.
func (*RungoInput) Scheme ¶ added in v1.0.2
func (input *RungoInput) Scheme() string
Scheme returns the request scheme as "http" or "https".
func (*RungoInput) Session ¶ added in v1.0.2
func (input *RungoInput) Session(key interface{}) interface{}
Session returns current session item value by a given key. if non-existed, return nil.
func (*RungoInput) SetData ¶ added in v1.0.2
func (input *RungoInput) SetData(key, val interface{})
SetData stores data with given key in this context. This data is only available in this context.
func (*RungoInput) SetParam ¶ added in v1.0.2
func (input *RungoInput) SetParam(key, val string)
SetParam sets the param with key and value
func (*RungoInput) Site ¶ added in v1.0.2
func (input *RungoInput) Site() string
Site returns the base site url as scheme://domain type.
func (*RungoInput) SubDomains ¶ added in v1.0.2
func (input *RungoInput) SubDomains() string
SubDomains returns sub domain string. if aa.bb.domain.com, returns aa.bb
func (*RungoInput) URI ¶ added in v1.0.2
func (input *RungoInput) URI() string
URI returns the full request url with query, string and fragment.
func (*RungoInput) URL ¶ added in v1.0.2
func (input *RungoInput) URL() string
URL returns the request url path (without query, string and fragment).
func (*RungoInput) UserAgent ¶ added in v1.0.2
func (input *RungoInput) UserAgent() string
UserAgent returns request client user agent string.
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