Documentation ¶
Index ¶
- Variables
- func SplitTail(s, sep string) []string
- type Api
- type Context
- type ContextError
- type Doc
- type ExtendedWriter
- type Handler
- type Interceptor
- type Node
- func (n *Node) Doc(d Doc) *Node
- func (n *Node) GetPath() string
- func (n *Node) Interceptor(m *Interceptor) *Node
- func (n *Node) InterceptorDeep(m *Interceptor) *Node
- func (n *Node) Method(m string, h Handler, d ...Doc) *Node
- func (n *Node) Node(p string) *Node
- func (n *Node) Operation(p string) *Operation
- func (n *Node) SetPath(p string)
- type Operation
Constants ¶
This section is empty.
Variables ¶
var InterceptorError = &Interceptor{ Documentation: Doc{ Name: "Error", Description: ` Print JSON error in this form: ´´´json { "status_code": 404, "error_code": 21, "description_code": "User '231223' not found." } ´´´ `, }, After: func(c *Context) { if nil != c.LastError { json.NewEncoder(c.Response).Encode(c.LastError) } }, }
InterceptorError prints an error in JSON format if Context.LastError is not nil. Example:
{ "status_code": 404, "error_code": 1000023, "description_code": "User 'fulanez' not found.", }
var InterceptorLog = &Interceptor{ Documentation: Doc{ Name: "Log", Description: ` Log all HTTP requests to stdout in this form: ´´´ 2016/02/20 11:09:17 GET /favicon.ico 404 59B 2016/02/20 11:09:34 GET /service/v1/ 405 68B 2016/02/20 11:09:46 GET /service/v1/doc 405 68B ´´´ `, }, After: func(c *Context) { log.Printf( "%s\t%s\t%d\t%dB", c.Request.Method, c.Request.URL.RequestURI(), c.Response.StatusCode, c.Response.Length, ) }, }
InterceptorLog prints an access log to standard output. Example: 2016/02/20 11:09:17 GET /favicon.ico 404 59B 2016/02/20 11:09:34 GET /service/v1/ 405 68B 2016/02/20 11:09:46 GET /service/v1/doc 405 68B
var InterceptorNoCache = &Interceptor{ Documentation: Doc{ Name: "InterceptorNoCache", Description: ` Avoid caching via http headers `, }, Before: func(c *Context) { add := c.Response.Header().Add add("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0") add("Pragma", "no-cache") add("Expires", "0") }, }
InterceptorNoCache set some headers to force response to not be cached by user agent.
Functions ¶
Types ¶
type Api ¶
type Api struct { Root *Node Prefix string Handler405 Handler Handler404 Handler Handler500 Handler }
Api is a complete API that implements http.Handler interface.
type Context ¶
type Context struct { Request *http.Request Response *ExtendedWriter Parameter string Parameters map[string]string LastError *ContextError Scope map[string]interface{} PathHandlers string // contains filtered or unexported fields }
Context is a space to store information to be passed between interceptors and the final handler.
type ContextError ¶
type ContextError struct { StatusCode int `json:"status_code"` ErrorCode int `json:"error_code"` Description string `json:"description_code"` }
ContextError is the error passed back when context.Error is called. It can be used inside an interceptor or a handler
type Doc ¶ added in v0.4.0
Doc represents documentation information that can be attached to a node, method or interceptor.
type ExtendedWriter ¶ added in v0.2.0
type ExtendedWriter struct { StatusCode int Length int http.ResponseWriter // contains filtered or unexported fields }
ExtendedWriter wraps http.ResponseWriter with StatusCode & Length
func NewExtendedWriter ¶ added in v0.2.0
func NewExtendedWriter(w http.ResponseWriter) *ExtendedWriter
NewExtendedWriter instances a new *ExtendedWriter
func (*ExtendedWriter) Write ¶ added in v0.2.0
func (w *ExtendedWriter) Write(p []byte) (int, error)
Write replaces default behaviour of http.ResponseWriter
func (*ExtendedWriter) WriteHeader ¶ added in v0.2.0
func (w *ExtendedWriter) WriteHeader(statusCode int)
WriteHeader replaces default behaviour of http.ResponseWriter
type Handler ¶
type Handler func(c *Context)
Handler is a function that implements an HTTP method for a Node, Operation, Interceptor.Before and Interceptor.After items.
type Interceptor ¶
Interceptor are pieces of code attached to nodes that can interact with the context and break the execution. A interceptor has two pieces of code, `Before` is executed before the handler and `After` is executed after the handler. The `After` code is executed always if the `Before` code has been executed successfully (without calling to `context.Error(...)`.
type Node ¶
type Node struct { Interceptors []*Interceptor InterceptorsDeep []*Interceptor Methods map[string]Handler Children []*Node Documentation Doc DocumentationMethods map[string]Doc Operations map[string]*Operation // contains filtered or unexported fields }
Node represents a path part of an URL
func (*Node) Interceptor ¶
func (n *Node) Interceptor(m *Interceptor) *Node
Interceptor attaches an *Interceptor to a *Node
func (*Node) InterceptorDeep ¶ added in v0.6.0
func (n *Node) InterceptorDeep(m *Interceptor) *Node
InterceptorDeep attaches an *Interceptor to a *Node but will be executed after all regular interceptors.
func (*Node) Method ¶
Method implements an HTTP method for a handler and optionally allows a third documentation parameter
type Operation ¶ added in v0.5.0
type Operation struct { Path string // Operation name Interceptors []*Interceptor Methods map[string]Handler }
Operation is a terminal node, ready to execute code but exposed as Google custom methods (with :operation syntax)
func NewOperation ¶ added in v0.5.0
func NewOperation() *Operation
NewOperation instances and initialize an Operation
func (*Operation) Interceptor ¶ added in v0.5.0
func (o *Operation) Interceptor(m *Interceptor) *Operation
Interceptor attaches an Interceptor to an operation