context

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tree

func Tree() *node

Types

type App

type App struct {
	Requests []Path

	MiddlewareList []Middleware
	Prefix         string
	// 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)

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)

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)

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

func (*App) Group

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

Group add middlewares and prefix for App.

func (*App) HEAD

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

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

func (*App) OPTIONS

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

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

func (*App) POST

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

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

func (*App) PUT

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

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{}
}

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) Json

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) Path

func (ctx *Context) Path() string

Path return the url path.

func (*Context) SetContentType

func (ctx *Context) SetContentType(contentType string)

SetStatusCode 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) 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) 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) WriteString

func (ctx *Context) WriteString(Body string)

Write save the given body string into the response.

type Handler

type Handler func(ctx *Context)

type Middleware

type Middleware func(handler Handler) Handler

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.

Jump to

Keyboard shortcuts

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