router

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2019 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessKeyType

type AccessKeyType int

AccessKeyType is the type of the access key specified in client request

const (
	// NoAccessKey denotes that an access key is not specified
	NoAccessKey AccessKeyType = 0 + iota

	// ClientAccessKey denotes that a client access key is specified
	ClientAccessKey

	// MasterAccessKey denotes that a master aclieneccess key is specified
	MasterAccessKey
)

func (AccessKeyType) String

func (i AccessKeyType) String() string

type AccessToken

type AccessToken interface {
	// IssuedAt returns the time when the access token is issued. If the
	// information is not available, the IsZero method of the
	// returned time is true.
	IssuedAt() time.Time
}

AccessToken is an interface to access information about the Access Token in the payload.

type CORSMiddleware

type CORSMiddleware struct {
	Origin string
	Next   http.Handler
}

func (*CORSMiddleware) ServeHTTP

func (cors *CORSMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ContextKey added in v0.18.0

type ContextKey string

ContextKey is the name of the values that are saved to context.Context.

var AccessKeyTypeContextKey ContextKey = "AccessKeyType"
var UserIDContextKey ContextKey = "UserID"

type Gateway

type Gateway struct {
	ParamMatch *regexp.Regexp

	Tag string
	// 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, path string, tag string, mux *http.ServeMux) *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) HandlePayload added in v0.24.0

func (r *Gateway) HandlePayload(payload *Payload, resp *Response)

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 interface {
	Setup()
	GetPreprocessors() []Processor
	Handle(*Payload, *Response)
}

Handler specifies the interface of a request handler

func NewFuncHandler

func NewFuncHandler(f HandlerFunc) Handler

NewFuncHandler is intend for using in test, not actual code.

type HandlerFunc

type HandlerFunc func(*Payload, *Response)

HandlerFunc specifies the function signature of a request handler function

type HandlerInjector

type HandlerInjector struct {
	ServiceGraph    *inject.Graph
	PreprocessorMap *PreprocessorRegistry
}

HandlerInjector is standard way to inject Services and Preprocessors specified by the Handler struct.

func (*HandlerInjector) Inject

func (i *HandlerInjector) Inject(h Handler) Handler

func (*HandlerInjector) InjectProcessors

func (i *HandlerInjector) InjectProcessors(h Handler) Handler

func (*HandlerInjector) InjectServices

func (i *HandlerInjector) InjectServices(h Handler) Handler

type LoggingMiddleware

type LoggingMiddleware struct {
	Skips       []string
	MimeConcern []string
	Next        http.Handler
	ByteLimit   *int
}

func (*LoggingMiddleware) ServeHTTP

func (l *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

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

	AppName    string
	AuthInfoID string
	AuthInfo   *skydb.AuthInfo
	AccessKey  AccessKeyType

	// AccessToken stores access token for this payload.
	//
	// The field is injected by preprocessor. The field
	// is nil if the AccessToken does not exist or is not valid.
	AccessToken AccessToken

	DBConn   skydb.Conn
	Database skydb.Database

	User *skydb.Record
	// contains filtered or unexported fields
}

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

func (p *Payload) AccessTokenString() string

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

func (*Payload) Context

func (p *Payload) Context() context.Context

Context returns the payload context.

If the context is nil, the background context will be used. The background context will also be set to the payload. In other words this function has side effect.

func (*Payload) HasMasterKey

func (p *Payload) HasMasterKey() bool

HasMasterKey returns whether the payload has master access key

func (*Payload) RouteAction

func (p *Payload) RouteAction() string

RouteAction must exist for every request

func (*Payload) SetContext added in v1.6.0

func (p *Payload) SetContext(ctx context.Context)

SetContext sets the specified context to the payload.

type PreprocessorRegistry

type PreprocessorRegistry map[string]Processor

PreprocessorRegistry is holding all preprocessors and their mapping with a string name.

func (PreprocessorRegistry) GetByNames

func (r PreprocessorRegistry) GetByNames(names ...string) []Processor

GetByNames returns a list of registered preprocessors by preprocessor names.

type Processor

type Processor interface {
	Preprocess(*Payload, *Response) int
}

Processor specifies the function signature for a Processor

type RequestIDMiddleware added in v1.6.0

type RequestIDMiddleware struct {
	Next http.Handler
}

func (*RequestIDMiddleware) ServeHTTP added in v1.6.0

func (m *RequestIDMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Response

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

Response is interface for handler to write response to router

func NewResponse added in v0.24.0

func NewResponse(writer http.ResponseWriter) *Response

func (*Response) Writer added in v0.22.0

func (resp *Response) Writer() (writer http.ResponseWriter)

Writer returns a http.ResponseWriter only once. If a writer is already returned, this function will always return nil.

Any http.Handler handling the request may get a http.ResponseWriter by calling this function. Once called, the caller is responsible for writing a response. No other Handler will be able to write to the same ResponseWriter.

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) HandlePayload added in v0.24.0

func (r *Router) HandlePayload(payload *Payload, resp *Response)

func (*Router) Map

func (r *Router) Map(action, tag 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