Documentation ¶
Overview ¶
Package context provide the context utils Usage:
import "github.com/W3-Engineers-Ltd/Radiant/server/web/context" ctx := context.Context{Request:req,ResponseWriter:rw} more docs http://radiant.me/docs/module/context.md
Index ¶
- Constants
- func InitGzip(minLength, compressLevel int, methods []string)
- 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 RadiantInput
- func (input *RadiantInput) AcceptsHTML() bool
- func (input *RadiantInput) AcceptsJSON() bool
- func (input *RadiantInput) AcceptsXML() bool
- func (input *RadiantInput) AcceptsYAML() bool
- func (input *RadiantInput) Bind(dest interface{}, key string) error
- func (input *RadiantInput) Cookie(key string) string
- func (input *RadiantInput) CopyBody(MaxMemory int64) []byte
- func (input *RadiantInput) Data() map[interface{}]interface{}
- func (input *RadiantInput) Domain() string
- func (input *RadiantInput) GetData(key interface{}) interface{}
- func (input *RadiantInput) Header(key string) string
- func (input *RadiantInput) Host() string
- func (input *RadiantInput) IP() string
- func (input *RadiantInput) Is(method string) bool
- func (input *RadiantInput) IsAjax() bool
- func (input *RadiantInput) IsDelete() bool
- func (input *RadiantInput) IsGet() bool
- func (input *RadiantInput) IsHead() bool
- func (input *RadiantInput) IsOptions() bool
- func (input *RadiantInput) IsPatch() bool
- func (input *RadiantInput) IsPost() bool
- func (input *RadiantInput) IsPut() bool
- func (input *RadiantInput) IsSecure() bool
- func (input *RadiantInput) IsUpload() bool
- func (input *RadiantInput) IsWebsocket() bool
- func (input *RadiantInput) Method() string
- func (input *RadiantInput) Param(key string) string
- func (input *RadiantInput) Params() map[string]string
- func (input *RadiantInput) ParamsLen() int
- func (input *RadiantInput) ParseFormOrMultiForm(maxMemory int64) error
- func (input *RadiantInput) Port() int
- func (input *RadiantInput) Protocol() string
- func (input *RadiantInput) Proxy() []string
- func (input *RadiantInput) Query(key string) string
- func (input *RadiantInput) Refer() string
- func (input *RadiantInput) Referer() string
- func (input *RadiantInput) Reset(ctx *Context)
- func (input *RadiantInput) ResetParams()
- func (input *RadiantInput) Scheme() string
- func (input *RadiantInput) Session(key interface{}) interface{}
- func (input *RadiantInput) SetData(key, val interface{})
- func (input *RadiantInput) SetParam(key, val string)
- func (input *RadiantInput) Site() string
- func (input *RadiantInput) SubDomains() string
- func (input *RadiantInput) URI() string
- func (input *RadiantInput) URL() string
- func (input *RadiantInput) UserAgent() string
- type RadiantOutput
- func (output *RadiantOutput) Body(content []byte) error
- func (output *RadiantOutput) ContentType(ext string)
- func (output *RadiantOutput) Cookie(name string, value string, others ...interface{})
- func (output *RadiantOutput) Download(file string, filename ...string)
- func (output *RadiantOutput) Header(key, val string)
- func (output *RadiantOutput) IsCachable() bool
- func (output *RadiantOutput) IsClientError() bool
- func (output *RadiantOutput) IsEmpty() bool
- func (output *RadiantOutput) IsForbidden() bool
- func (output *RadiantOutput) IsNotFound() bool
- func (output *RadiantOutput) IsOk() bool
- func (output *RadiantOutput) IsRedirect() bool
- func (output *RadiantOutput) IsServerError() bool
- func (output *RadiantOutput) IsSuccessful() bool
- func (output *RadiantOutput) JSON(data interface{}, hasIndent bool, encoding bool) error
- func (output *RadiantOutput) JSONP(data interface{}, hasIndent bool) error
- func (output *RadiantOutput) Proto(data proto.Message) error
- func (output *RadiantOutput) Reset(ctx *Context)
- func (output *RadiantOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool) error
- func (output *RadiantOutput) Session(name interface{}, value interface{})
- func (output *RadiantOutput) SetStatus(status int)
- func (output *RadiantOutput) XML(data interface{}, hasIndent bool) error
- func (output *RadiantOutput) YAML(data interface{}) error
- type Renderer
- type Response
- 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 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 *RadiantInput Output *RadiantOutput Request *http.Request ResponseWriter *Response // contains filtered or unexported fields }
Context Http request context struct including RadiantInput, RadiantOutput, http.Request and http.ResponseWriter. RadiantInput and RadiantOutput 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 RadiantInput.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, RadiantInput and RadiantOutput
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 RadiantInput ¶
type RadiantInput struct { Context *Context CruSession session.Store RequestBody []byte RunMethod string RunController reflect.Type // contains filtered or unexported fields }
RadiantInput operates the http request header, data, cookie and body. Contains router params and current session.
func NewInput ¶
func NewInput() *RadiantInput
NewInput returns the RadiantInput generated by context.
func (*RadiantInput) AcceptsHTML ¶
func (input *RadiantInput) AcceptsHTML() bool
AcceptsHTML Checks if request accepts html response
func (*RadiantInput) AcceptsJSON ¶
func (input *RadiantInput) AcceptsJSON() bool
AcceptsJSON Checks if request accepts json response
func (*RadiantInput) AcceptsXML ¶
func (input *RadiantInput) AcceptsXML() bool
AcceptsXML Checks if request accepts xml response
func (*RadiantInput) AcceptsYAML ¶
func (input *RadiantInput) AcceptsYAML() bool
AcceptsYAML Checks if request accepts json response
func (*RadiantInput) Bind ¶
func (input *RadiantInput) 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 radiantInput.Bind(&id, "id") id ==123 var isok bool radiantInput.Bind(&isok, "isok") isok ==true var ft float64 radiantInput.Bind(&ft, "ft") ft ==1.2 ol := make([]int, 0, 2) radiantInput.Bind(&ol, "ol") ol ==[1 2] ul := make([]string, 0, 2) radiantInput.Bind(&ul, "ul") ul ==[str array] user struct{Name} radiantInput.Bind(&user, "user") user == {Name:"astaxie"}
func (*RadiantInput) Cookie ¶
func (input *RadiantInput) Cookie(key string) string
Cookie returns request cookie item string by a given key. if non-existed, return empty string.
func (*RadiantInput) CopyBody ¶
func (input *RadiantInput) CopyBody(MaxMemory int64) []byte
CopyBody returns the raw request body data as bytes.
func (*RadiantInput) Data ¶
func (input *RadiantInput) Data() map[interface{}]interface{}
Data returns the implicit data in the input
func (*RadiantInput) Domain ¶
func (input *RadiantInput) Domain() string
Domain returns the host name (alias of host method)
func (*RadiantInput) GetData ¶
func (input *RadiantInput) GetData(key interface{}) interface{}
GetData returns the stored data in this context.
func (*RadiantInput) Header ¶
func (input *RadiantInput) Header(key string) string
Header returns request header item string by a given string. if non-existed, return empty string.
func (*RadiantInput) Host ¶
func (input *RadiantInput) Host() string
Host returns the host name. If no host info in request, return localhost.
func (*RadiantInput) IP ¶
func (input *RadiantInput) IP() string
IP returns request client ip. if in proxy, return first proxy id. if error, return RemoteAddr.
func (*RadiantInput) Is ¶
func (input *RadiantInput) Is(method string) bool
Is returns the boolean value of this request is on given method, such as Is("POST").
func (*RadiantInput) IsAjax ¶
func (input *RadiantInput) IsAjax() bool
IsAjax returns boolean of is this request generated by ajax.
func (*RadiantInput) IsDelete ¶
func (input *RadiantInput) IsDelete() bool
IsDelete Is this a DELETE method request?
func (*RadiantInput) IsGet ¶
func (input *RadiantInput) IsGet() bool
IsGet Is this a GET method request?
func (*RadiantInput) IsHead ¶
func (input *RadiantInput) IsHead() bool
IsHead Is this a Head method request?
func (*RadiantInput) IsOptions ¶
func (input *RadiantInput) IsOptions() bool
IsOptions Is this a OPTIONS method request?
func (*RadiantInput) IsPatch ¶
func (input *RadiantInput) IsPatch() bool
IsPatch Is this a PATCH method request?
func (*RadiantInput) IsPost ¶
func (input *RadiantInput) IsPost() bool
IsPost Is this a POST method request?
func (*RadiantInput) IsPut ¶
func (input *RadiantInput) IsPut() bool
IsPut Is this a PUT method request?
func (*RadiantInput) IsSecure ¶
func (input *RadiantInput) IsSecure() bool
IsSecure returns boolean of this request is in https.
func (*RadiantInput) IsUpload ¶
func (input *RadiantInput) IsUpload() bool
IsUpload returns boolean of whether file uploads in this request or not..
func (*RadiantInput) IsWebsocket ¶
func (input *RadiantInput) IsWebsocket() bool
IsWebsocket returns boolean of this request is in webSocket.
func (*RadiantInput) Method ¶
func (input *RadiantInput) Method() string
Method returns http request method.
func (*RadiantInput) Param ¶
func (input *RadiantInput) Param(key string) string
Param returns router param by a given key.
func (*RadiantInput) Params ¶
func (input *RadiantInput) Params() map[string]string
Params returns the map[key]value.
func (*RadiantInput) ParamsLen ¶
func (input *RadiantInput) ParamsLen() int
ParamsLen return the length of the params
func (*RadiantInput) ParseFormOrMultiForm ¶
func (input *RadiantInput) ParseFormOrMultiForm(maxMemory int64) error
ParseFormOrMultiForm parseForm or parseMultiForm based on Content-type
func (*RadiantInput) Port ¶
func (input *RadiantInput) Port() int
Port returns request client port. when error or empty, return 80.
func (*RadiantInput) Protocol ¶
func (input *RadiantInput) Protocol() string
Protocol returns the request protocol name, such as HTTP/1.1 .
func (*RadiantInput) Proxy ¶
func (input *RadiantInput) Proxy() []string
Proxy returns proxy client ips slice.
func (*RadiantInput) Query ¶
func (input *RadiantInput) Query(key string) string
Query returns input data item string by a given string.
func (*RadiantInput) Refer ¶
func (input *RadiantInput) Refer() string
Refer returns http referer header.
func (*RadiantInput) Referer ¶
func (input *RadiantInput) Referer() string
Referer returns http referer header.
func (*RadiantInput) Reset ¶
func (input *RadiantInput) Reset(ctx *Context)
Reset initializes the RadiantInput
func (*RadiantInput) ResetParams ¶
func (input *RadiantInput) ResetParams()
ResetParams clears any of the input's params Used to clear parameters so they may be reset between filter passes.
func (*RadiantInput) Scheme ¶
func (input *RadiantInput) Scheme() string
Scheme returns the request scheme as "http" or "https".
func (*RadiantInput) Session ¶
func (input *RadiantInput) Session(key interface{}) interface{}
Session returns current session item value by a given key. if non-existed, return nil.
func (*RadiantInput) SetData ¶
func (input *RadiantInput) SetData(key, val interface{})
SetData stores data with given key in this context. This data is only available in this context.
func (*RadiantInput) SetParam ¶
func (input *RadiantInput) SetParam(key, val string)
SetParam sets the param with key and value
func (*RadiantInput) Site ¶
func (input *RadiantInput) Site() string
Site returns the base site url as scheme://domain type.
func (*RadiantInput) SubDomains ¶
func (input *RadiantInput) SubDomains() string
SubDomains returns sub domain string. if aa.bb.domain.com, returns aa.bb
func (*RadiantInput) URI ¶
func (input *RadiantInput) URI() string
URI returns the full request url with query, string and fragment.
func (*RadiantInput) URL ¶
func (input *RadiantInput) URL() string
URL returns the request url path (without query, string and fragment).
func (*RadiantInput) UserAgent ¶
func (input *RadiantInput) UserAgent() string
UserAgent returns request client user agent string.
type RadiantOutput ¶
RadiantOutput does work for sending response header.
func NewOutput ¶
func NewOutput() *RadiantOutput
NewOutput returns new RadiantOutput. Empty when initialized
func (*RadiantOutput) Body ¶
func (output *RadiantOutput) Body(content []byte) error
Body sets the response body content. if EnableGzip, content is compressed. Sends out response body directly.
func (*RadiantOutput) ContentType ¶
func (output *RadiantOutput) ContentType(ext string)
ContentType sets the content type from ext string. MIME type is given in mime package.
func (*RadiantOutput) Cookie ¶
func (output *RadiantOutput) Cookie(name string, value string, others ...interface{})
Cookie sets a cookie value via given key. others: used to set a cookie's max age time, path,domain, secure and httponly.
func (*RadiantOutput) Download ¶
func (output *RadiantOutput) Download(file string, filename ...string)
Download forces response for download file. Prepares the download response header automatically.
func (*RadiantOutput) Header ¶
func (output *RadiantOutput) Header(key, val string)
Header sets response header item string via given key.
func (*RadiantOutput) IsCachable ¶
func (output *RadiantOutput) IsCachable() bool
IsCachable returns boolean of if this request is cached. HTTP 304 means cached.
func (*RadiantOutput) IsClientError ¶
func (output *RadiantOutput) IsClientError() bool
IsClientError returns boolean of if this request client sends error data. HTTP 4xx means client error.
func (*RadiantOutput) IsEmpty ¶
func (output *RadiantOutput) IsEmpty() bool
IsEmpty returns boolean of if this request is empty. HTTP 201,204 and 304 means empty.
func (*RadiantOutput) IsForbidden ¶
func (output *RadiantOutput) IsForbidden() bool
IsForbidden returns boolean of if this request is forbidden. HTTP 403 means forbidden.
func (*RadiantOutput) IsNotFound ¶
func (output *RadiantOutput) IsNotFound() bool
IsNotFound returns boolean of if this request is not found. HTTP 404 means not found.
func (*RadiantOutput) IsOk ¶
func (output *RadiantOutput) IsOk() bool
IsOk returns boolean of if this request was ok. HTTP 200 means ok.
func (*RadiantOutput) IsRedirect ¶
func (output *RadiantOutput) IsRedirect() bool
IsRedirect returns boolean of if this request is redirected. HTTP 301,302,307 means redirection.
func (*RadiantOutput) IsServerError ¶
func (output *RadiantOutput) IsServerError() bool
IsServerError returns boolean of if this server handler errors. HTTP 5xx means server internal error.
func (*RadiantOutput) IsSuccessful ¶
func (output *RadiantOutput) IsSuccessful() bool
IsSuccessful returns boolean of this request was successful. HTTP 2xx means ok.
func (*RadiantOutput) JSON ¶
func (output *RadiantOutput) JSON(data interface{}, hasIndent bool, encoding bool) error
JSON writes json to the response body. if encoding is true, it converts utf-8 to \u0000 type.
func (*RadiantOutput) JSONP ¶
func (output *RadiantOutput) JSONP(data interface{}, hasIndent bool) error
JSONP writes jsonp to the response body.
func (*RadiantOutput) Proto ¶
func (output *RadiantOutput) Proto(data proto.Message) error
Proto writes protobuf to the response body.
func (*RadiantOutput) Reset ¶
func (output *RadiantOutput) Reset(ctx *Context)
Reset initializes RadiantOutput
func (*RadiantOutput) ServeFormatted ¶
func (output *RadiantOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool) error
ServeFormatted serves YAML, XML or JSON, depending on the value of the Accept header
func (*RadiantOutput) Session ¶
func (output *RadiantOutput) Session(name interface{}, value interface{})
Session sets session item value with given key.
func (*RadiantOutput) SetStatus ¶
func (output *RadiantOutput) SetStatus(status int)
SetStatus sets the response status code. Writes response header directly.
func (*RadiantOutput) XML ¶
func (output *RadiantOutput) XML(data interface{}, hasIndent bool) error
XML writes xml string to the response body.
func (*RadiantOutput) YAML ¶
func (output *RadiantOutput) YAML(data interface{}) error
YAML writes yaml to the 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 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