context

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2024 License: Apache-2.0, Apache-2.0 Imports: 30 Imported by: 0

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

View Source
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 InitGzip

func InitGzip(minLength, compressLevel int, methods []string)

InitGzip initializes the gzipcompress

func NewOutput

func NewOutput() *rungoOutput

NewOutput returns new rungoOutput. Empty when initialized

func ParseEncoding

func ParseEncoding(r *http.Request) string

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

func ParseForm

func ParseForm(form url.Values, obj interface{}) error

ParseForm will parse form values to struct via tag.

func WriteBody

func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string, error)

WriteBody reads writes content to writer by the specific encoding(gzip/deflate)

func WriteFile

func WriteFile(encoding string, writer io.Writer, file *os.File) (bool, string, error)

WriteFile reads from file and writes to writer by the specific encoding(gzip/deflate)

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 NewContext

func NewContext() *Context

NewContext return the Context with Input and Output

func (*Context) Abort

func (ctx *Context) Abort(status int, body string)

Abort stops the request. If rungo.ErrorMaps exists, panic body.

func (*Context) Bind

func (ctx *Context) Bind(obj interface{}) error

func (*Context) BindForm

func (ctx *Context) BindForm(obj interface{}) error

BindForm will parse form values to struct via tag.

func (*Context) BindJSON

func (ctx *Context) BindJSON(obj interface{}) error

BindJSON only read data from http request body

func (*Context) BindProtobuf

func (ctx *Context) BindProtobuf(obj proto.Message) error

BindProtobuf only read data from http request body

func (*Context) BindXML

func (ctx *Context) BindXML(obj interface{}) error

BindXML only read data from http request body

func (*Context) BindYAML

func (ctx *Context) BindYAML(obj interface{}) error

BindYAML only read data from http request body

func (*Context) CheckXSRFCookie

func (ctx *Context) CheckXSRFCookie() bool

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

func (ctx *Context) GetCookie(key string) string

GetCookie gets a cookie from a request for a given key. (Alias of RungoInput.Cookie)

func (*Context) GetSecureCookie

func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool)

GetSecureCookie gets a secure cookie from a request for a given key.

func (*Context) JSONResp

func (ctx *Context) JSONResp(data interface{}) error

func (*Context) ProtoResp

func (ctx *Context) ProtoResp(data proto.Message) error

func (*Context) Redirect

func (ctx *Context) Redirect(status int, localurl string)

Redirect redirects to localurl with http header status code.

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

func (ctx *Context) Resp(data interface{}) error

Resp sends response based on the Accept Header By default response will be in JSON

func (*Context) Session

func (ctx *Context) Session() (store session.Store, err error)

Session return session store of this context of request

func (*Context) SetCookie

func (ctx *Context) SetCookie(name string, value string, others ...interface{})

SetCookie sets a cookie for a response. (Alias of rungoOutput.Cookie)

func (*Context) SetSecureCookie

func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{})

SetSecureCookie sets a secure cookie for a response.

func (*Context) WriteString

func (ctx *Context) WriteString(content string)

WriteString writes a string to response body.

func (*Context) XMLResp

func (ctx *Context) XMLResp(data interface{}) error

func (*Context) XSRFToken

func (ctx *Context) XSRFToken(key string, expire int64) string

XSRFToken creates and returns an xsrf token string

func (*Context) YamlResp

func (ctx *Context) YamlResp(data interface{}) error

type Renderer

type Renderer interface {
	Render(ctx *Context)
}

Renderer defines a http response renderer

type Response

type Response struct {
	http.ResponseWriter
	Started bool
	Status  int
	Elapsed time.Duration
}

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

func (r *Response) CloseNotify() <-chan bool

CloseNotify http.CloseNotifier

func (*Response) Flush

func (r *Response) Flush()

Flush http.Flusher

func (*Response) Hijack

func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack hijacker for http

func (*Response) Pusher

func (r *Response) Pusher() (pusher http.Pusher)

Pusher http.Pusher

func (*Response) Write

func (r *Response) Write(p []byte) (int, error)

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

func (r *Response) WriteHeader(code int)

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 NewInput

func NewInput() *RungoInput

NewInput returns the RungoInput generated by context.

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL