router

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2016 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckAuth

func CheckAuth(payload *Payload, response *Response) (status int, err error)

CheckAuth will check on the AccessToken, attach DB/RequestID to the response This is a no-op if the request action belong to "auth:" group

func LoggingMiddleware

func LoggingMiddleware(next http.Handler, skipBody bool) http.Handler

Types

type Gateway

type Gateway struct {
	ParamMatch *regexp.Regexp
	// contains filtered or unexported fields
}

Gateway is a man in the middle to inject dependency It currently bind to HTTP method, it disregard path.

func NewGateway

func NewGateway(pattern string) *Gateway

func (*Gateway) GET

func (g *Gateway) GET(handler Handler, preprocessors ...Processor)

GET register a URL handler by method GET

func (*Gateway) Handle

func (g *Gateway) Handle(method string, handler Handler, preprocessors ...Processor)

Handle registers a handler matched by a request's method and URL's path. Pattern is a regexp that defines a matched URL.

func (*Gateway) POST

func (g *Gateway) POST(handler Handler, preprocessors ...Processor)

POST register a URL handler by method POST

func (*Gateway) PUT

func (g *Gateway) PUT(handler Handler, preprocessors ...Processor)

PUT register a URL handler by method PUT

func (*Gateway) ServeHTTP

func (g *Gateway) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Handler

type Handler func(*Payload, *Response)

Handler specifies the function signature of a request handler function

type Payload

type Payload struct {
	// the raw http.Request of this payload
	// Think twice before accessing it
	Req *http.Request
	// URL parameters
	Params []string

	// Map of params such as Auth, TimeSteam, version
	Meta map[string]interface{}
	// Map of action payload
	Data map[string]interface{}

	TokenStore       authtoken.Store
	AssetStore       asset.Store
	HookRegistry     *hook.Registry
	ProviderRegistry *provider.Registry

	AppName    string
	UserInfoID string
	UserInfo   *skydb.UserInfo

	DBConn   skydb.Conn
	Database skydb.Database

	NotificationSender push.Sender
}

Payload is for passing payload to the actual handler

func (*Payload) APIKey

func (p *Payload) APIKey() string

APIKey returns the api key in the request.

func (*Payload) AccessToken

func (p *Payload) AccessToken() string

AccessToken return the user input string TODO: accept all header, json payload, query string(in order)

func (*Payload) NewPayload

func (p *Payload) NewPayload(req *http.Request) *Payload

func (*Payload) RouteAction

func (p *Payload) RouteAction() string

RouteAction must exist for every request

type Processor

type Processor func(*Payload, *Response) int

Processor specifies the function signature for a Preprocessor

type Response

type Response struct {
	Meta       map[string]interface{} `json:"-"`
	Info       interface{}            `json:"info,omitempty"`
	Result     interface{}            `json:"result,omitempty"`
	Err        skyerr.Error           `json:"error,omitempty"`
	RequestID  string                 `json:"request_id,omitempty"`
	DatabaseID string                 `json:"database_id,omitempty"`
	// contains filtered or unexported fields
}

Response is interface for handler to write response to router

func (*Response) Header

func (resp *Response) Header() http.Header

Header returns the header map being written before return a response. Mutating the map after calling WriteEntity has no effects.

func (*Response) Hijack

func (resp *Response) Hijack() (c net.Conn, w *bufio.ReadWriter, e error)

func (*Response) Write

func (resp *Response) Write(b []byte) (int, error)

Write writes raw bytes as response to a request.

func (*Response) WriteEntity

func (resp *Response) WriteEntity(i interface{}) error

WriteEntity writes a value as response to a request. Currently it only writes JSON response.

func (*Response) WriteHeader

func (resp *Response) WriteHeader(status int)

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router to dispatch HTTP request to respective handler

func NewRouter

func NewRouter() *Router

NewRouter is factory for Router

func (*Router) Map

func (r *Router) Map(action string, handler Handler, preprocessors ...Processor)

Map to register action to handle mapping

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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