context

package
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: Apache-2.0 Imports: 15 Imported by: 233

Documentation

Index

Constants

View Source
const (
	HeaderContentType = "Content-Type"

	HeaderLastModified    = "Last-Modified"
	HeaderIfModifiedSince = "If-Modified-Since"
	HeaderCacheControl    = "Cache-Control"
	HeaderETag            = "ETag"

	HeaderContentDisposition = "Content-Disposition"
	HeaderContentLength      = "Content-Length"
	HeaderContentEncoding    = "Content-Encoding"

	GzipHeaderValue      = "gzip"
	HeaderAcceptEncoding = "Accept-Encoding"
	HeaderVary           = "Vary"
)

Variables

View Source
var ParseTime = func(text string) (t time.Time, err error) {
	t, err = time.Parse(http.TimeFormat, text)
	if err != nil {
		return http.ParseTime(text)
	}

	return
}

ParseTime parses a time header (such as the Date: header), trying each forth formats that are allowed by HTTP/1.1: time.RFC850, and time.ANSIC.

Functions

func IsZeroTime added in v1.2.8

func IsZeroTime(t time.Time) bool

IsZeroTime reports whether t is obviously unspecified (either zero or Unix()=0).

Types

type App

type App struct {
	Requests    []Path
	Handlers    HandlerMap
	Middlewares Handlers
	Prefix      string

	Routers RouterMap
	// contains filtered or unexported fields
}

App is the key struct of the package. App as a member of plugin entity contains the request and the corresponding handler. Prefix is the url prefix and MiddlewareList is for control flow.

func NewApp

func NewApp() *App

NewApp return an empty app.

func (*App) ANY

func (app *App) ANY(url string, handler ...Handler) *App

ANY registers a route that matches all the HTTP methods. GET, POST, PUT, HEAD, OPTIONS, DELETE.

func (*App) AppendReqAndResp

func (app *App) AppendReqAndResp(url, method string, handler []Handler)

AppendReqAndResp stores the request info and handle into app. support the route parameter. The route parameter will be recognized as wildcard store into the RegUrl of Path struct. For example:

/user/:id      => /user/(.*)
/user/:id/info => /user/(.*?)/info

The RegUrl will be used to recognize the incoming path and find the handler.

func (*App) DELETE

func (app *App) DELETE(url string, handler ...Handler) *App

DELETE is a shortcut for app.AppendReqAndResp(url, "delete", handler).

func (*App) Find

func (app *App) Find(url, method string) []Handler

Find is public helper method for findPath of tree.

func (*App) GET

func (app *App) GET(url string, handler ...Handler) *App

GET is a shortcut for app.AppendReqAndResp(url, "get", handler).

func (*App) Group

func (app *App) Group(prefix string, middleware ...Handler) *RouterGroup

Group add middlewares and prefix for App.

func (*App) HEAD

func (app *App) HEAD(url string, handler ...Handler) *App

HEAD is a shortcut for app.AppendReqAndResp(url, "head", handler).

func (*App) Name added in v1.2.1

func (app *App) Name(name string)

func (*App) OPTIONS

func (app *App) OPTIONS(url string, handler ...Handler) *App

OPTIONS is a shortcut for app.AppendReqAndResp(url, "options", handler).

func (*App) POST

func (app *App) POST(url string, handler ...Handler) *App

POST is a shortcut for app.AppendReqAndResp(url, "post", handler).

func (*App) PUT

func (app *App) PUT(url string, handler ...Handler) *App

PUT is a shortcut for app.AppendReqAndResp(url, "put", handler).

type Context

type Context struct {
	Request   *http.Request
	Response  *http.Response
	UserValue map[string]interface{}
	// contains filtered or unexported fields
}

Context is the simplify version of web framework context. But it is important which will be used in plugins to custom the request and response. And adapter will help to transform the Context to the web framework`s context. It has three attributes. Request and response are belongs to net/http package. UserValue is the custom key-value store of context.

func NewContext

func NewContext(req *http.Request) *Context

NewContext used in adapter which return a Context with request and slice of UserValue and a default Response.

func (*Context) Abort added in v1.0.0

func (ctx *Context) Abort()

Abort abort the context.

func (*Context) AddHeader added in v1.0.0

func (ctx *Context) AddHeader(key, value string)

AddHeader adds the key, value pair to the header.

func (*Context) BindJSON added in v1.2.0

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

func (*Context) CheckIfModifiedSince added in v1.2.8

func (ctx *Context) CheckIfModifiedSince(modtime time.Time) (bool, error)

func (*Context) Data added in v1.0.0

func (ctx *Context) Data(code int, contentType string, data []byte)

Data writes some data into the body stream and updates the HTTP code.

func (*Context) FormValue added in v1.0.0

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

FormValue get the value of request form key.

func (*Context) GetContentType added in v1.2.8

func (ctx *Context) GetContentType() string

func (*Context) HTML added in v1.0.8

func (ctx *Context) HTML(code int, body string)

HTML output html response.

func (*Context) HTMLByte added in v1.2.7

func (ctx *Context) HTMLByte(code int, body []byte)

HTMLByte output html response.

func (*Context) Headers added in v1.0.0

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

Headers get the value of request headers key.

func (*Context) IsPjax added in v1.2.7

func (ctx *Context) IsPjax() bool

IsPjax check request is pjax or not.

func (*Context) JSON added in v1.0.8

func (ctx *Context) JSON(code int, Body map[string]interface{})

JSON serializes the given struct as JSON into the response body. It also sets the Content-Type as "application/json".

func (*Context) LocalIP

func (ctx *Context) LocalIP() string

LocalIP return the request client ip.

func (*Context) Method

func (ctx *Context) Method() string

Method return the request method.

func (*Context) MustBindJSON added in v1.2.0

func (ctx *Context) MustBindJSON(data interface{})

func (*Context) Next added in v1.0.0

func (ctx *Context) Next()

Next should be used only inside middleware.

func (*Context) Path

func (ctx *Context) Path() string

Path return the url path.

func (*Context) PjaxUrl added in v1.2.5

func (ctx *Context) PjaxUrl(url string)

PjaxUrl add pjax url header.

func (*Context) PostForm added in v1.1.6

func (ctx *Context) PostForm() url.Values

PostForm get the values of request form.

func (*Context) Query added in v1.0.0

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

Query get the query parameter of url.

func (*Context) QueryDefault added in v1.0.0

func (ctx *Context) QueryDefault(key, def string) string

QueryDefault get the query parameter of url. If it is empty, return the default.

func (*Context) Redirect added in v1.0.9

func (ctx *Context) Redirect(path string)

Redirect add redirect url to header.

func (*Context) ServeContent added in v1.2.8

func (ctx *Context) ServeContent(content io.ReadSeeker, filename string, modtime time.Time, gzipCompression bool) error

ServeContent serves content, headers are autoset receives three parameters, it's low-level function, instead you can use .ServeFile(string,bool)/SendFile(string,string)

You can define your own "Content-Type" header also, after this function call Doesn't implements resuming (by range), use ctx.SendFile instead

func (*Context) ServeFile added in v1.2.8

func (ctx *Context) ServeFile(filename string, gzipCompression bool) error

ServeFile serves a view file, to send a file ( zip for example) to the client you should use the SendFile(serverfilename,clientfilename)

func (*Context) SetContentType

func (ctx *Context) SetContentType(contentType string)

SetContentType save the given content type header into the response header.

func (*Context) SetCookie

func (ctx *Context) SetCookie(cookie *http.Cookie)

SetCookie save the given cookie obj into the response Set-Cookie header.

func (*Context) SetHandlers added in v1.0.0

func (ctx *Context) SetHandlers(handlers Handlers) *Context

SetHandlers set the handlers of Context.

func (*Context) SetHeader added in v1.0.0

func (ctx *Context) SetHeader(key, value string)

SetHeader set the key, value pair to the header.

func (*Context) SetLastModified added in v1.2.8

func (ctx *Context) SetLastModified(modtime time.Time)

func (*Context) SetStatusCode

func (ctx *Context) SetStatusCode(code int)

SetStatusCode save the given status code into the response.

func (*Context) SetUserValue

func (ctx *Context) SetUserValue(key string, value interface{})

SetUserValue set the value of user context.

func (*Context) User added in v1.0.0

func (ctx *Context) User() interface{}

User return the current login user.

func (*Context) WantHTML added in v1.2.8

func (ctx *Context) WantHTML() bool

func (*Context) WantJSON added in v1.2.8

func (ctx *Context) WantJSON() bool

func (*Context) Write

func (ctx *Context) Write(code int, Header map[string]string, Body string)

Write save the given status code, header and body string into the response.

func (*Context) WriteNotModified added in v1.2.8

func (ctx *Context) WriteNotModified()

func (*Context) WriteString

func (ctx *Context) WriteString(body string)

WriteString save the given body string into the response.

type Handler

type Handler func(ctx *Context)

Handler defines the handler used by the middleware as return value.

type HandlerMap added in v1.2.5

type HandlerMap map[Path]Handlers

type Handlers added in v1.0.0

type Handlers []Handler

Handlers is the array of Handler

type Node added in v1.1.8

type Node struct {
	Path     string
	Method   string
	Handlers []Handler
	Value    map[string]interface{}
}

type Path

type Path struct {
	URL    string
	Method string
}

Path is used in the matching of request and response. Url stores the raw register url. RegUrl contains the wildcard which on behalf of the route params.

type Router added in v1.2.1

type Router struct {
	Methods []string
	Patten  string
}

func (Router) GetURL added in v1.2.1

func (r Router) GetURL(value ...string) string

func (Router) Method added in v1.2.1

func (r Router) Method() string

type RouterGroup added in v1.0.0

type RouterGroup struct {
	Middlewares Handlers
	Prefix      string
	// contains filtered or unexported fields
}

RouterGroup is a group of routes.

func (*RouterGroup) ANY added in v1.0.0

func (g *RouterGroup) ANY(url string, handler ...Handler) *RouterGroup

ANY registers a route that matches all the HTTP methods. GET, POST, PUT, HEAD, OPTIONS, DELETE.

func (*RouterGroup) AppendReqAndResp added in v1.0.0

func (g *RouterGroup) AppendReqAndResp(url, method string, handler []Handler)

AppendReqAndResp stores the request info and handle into app. support the route parameter. The route parameter will be recognized as wildcard store into the RegUrl of Path struct. For example:

/user/:id      => /user/(.*)
/user/:id/info => /user/(.*?)/info

The RegUrl will be used to recognize the incoming path and find the handler.

func (*RouterGroup) DELETE added in v1.0.0

func (g *RouterGroup) DELETE(url string, handler ...Handler) *RouterGroup

DELETE is a shortcut for app.AppendReqAndResp(url, "delete", handler).

func (*RouterGroup) GET added in v1.0.0

func (g *RouterGroup) GET(url string, handler ...Handler) *RouterGroup

GET is a shortcut for app.AppendReqAndResp(url, "get", handler).

func (*RouterGroup) Group added in v1.0.0

func (g *RouterGroup) Group(prefix string, middleware ...Handler) *RouterGroup

Group add middlewares and prefix for RouterGroup.

func (*RouterGroup) HEAD added in v1.0.0

func (g *RouterGroup) HEAD(url string, handler ...Handler) *RouterGroup

HEAD is a shortcut for app.AppendReqAndResp(url, "head", handler).

func (*RouterGroup) Name added in v1.2.1

func (g *RouterGroup) Name(name string)

func (*RouterGroup) OPTIONS added in v1.0.0

func (g *RouterGroup) OPTIONS(url string, handler ...Handler) *RouterGroup

OPTIONS is a shortcut for app.AppendReqAndResp(url, "options", handler).

func (*RouterGroup) POST added in v1.0.0

func (g *RouterGroup) POST(url string, handler ...Handler) *RouterGroup

POST is a shortcut for app.AppendReqAndResp(url, "post", handler).

func (*RouterGroup) PUT added in v1.0.0

func (g *RouterGroup) PUT(url string, handler ...Handler) *RouterGroup

PUT is a shortcut for app.AppendReqAndResp(url, "put", handler).

type RouterMap added in v1.2.1

type RouterMap map[string]Router

func (RouterMap) Get added in v1.2.1

func (r RouterMap) Get(name string) Router

Jump to

Keyboard shortcuts

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