golax: github.com/fulldump/golax Index | Files | Directories

package golax

import "github.com/fulldump/golax"

Index

Package Files

api.go context.go doc.go extended_writer.go handler.go interceptor.go interceptors.go node.go operation.go splittail.go

Variables

var InterceptorError = &Interceptor{
    Documentation: Doc{
        Name:        "Error",
        Description: "" /* 147 byte string literal not displayed */,
    },
    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: "" /* 204 byte string literal not displayed */,
    },
    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.

func SplitTail Uses

func SplitTail(s, sep string) []string

SplitTail split by separator and return pending tail and last part

type Api Uses

type Api struct {
    Root       *Node
    Prefix     string
    Handler405 Handler
    Handler404 Handler
    Handler500 Handler
}

Api is a complete API that implements http.Handler interface.

func NewApi Uses

func NewApi() *Api

NewApi instances and initializes a new *Api.

func (*Api) Serve Uses

func (a *Api) Serve()

Serve start a default server on address 0.0.0.0:8000

func (*Api) ServeHTTP Uses

func (a *Api) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface. This code is ugly but... It works! This is a critical part for the performance, so it has to be written with love

type Context Uses

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.

func NewContext Uses

func NewContext() *Context

NewContext instances and initializes a Context

func (*Context) Error Uses

func (c *Context) Error(s int, d string) *ContextError

func (*Context) Get Uses

func (c *Context) Get(k string) (interface{}, bool)

Get retrieves a value from a Context

func (*Context) Set Uses

func (c *Context) Set(k string, v interface{})

Set stores a key-value tuple inside a Context

type ContextError Uses

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 Uses

type Doc struct {
    Name        string
    Description string
    Ommit       bool
}

Doc represents documentation information that can be attached to a node, method or interceptor.

type ExtendedWriter Uses

type ExtendedWriter struct {
    StatusCode int

    Length int
    http.ResponseWriter
    // contains filtered or unexported fields
}

ExtendedWriter wraps http.ResponseWriter with StatusCode & Length

func NewExtendedWriter Uses

func NewExtendedWriter(w http.ResponseWriter) *ExtendedWriter

NewExtendedWriter instances a new *ExtendedWriter

func (*ExtendedWriter) Write Uses

func (w *ExtendedWriter) Write(p []byte) (int, error)

Write replaces default behaviour of http.ResponseWriter

func (*ExtendedWriter) WriteHeader Uses

func (w *ExtendedWriter) WriteHeader(statusCode int)

WriteHeader replaces default behaviour of http.ResponseWriter

type Handler Uses

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 Uses

type Interceptor struct {
    Before        Handler
    After         Handler
    Documentation Doc
}

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 Uses

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 NewNode Uses

func NewNode() *Node

NewNode instances and initializes a new node

func (*Node) Doc Uses

func (n *Node) Doc(d Doc) *Node

Doc attaches documentation to a *Node

func (*Node) GetPath Uses

func (n *Node) GetPath() string

GetPath retrieves a node path

func (*Node) Interceptor Uses

func (n *Node) Interceptor(m *Interceptor) *Node

Interceptor attaches an *Interceptor to a *Node

func (*Node) InterceptorDeep Uses

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 Uses

func (n *Node) Method(m string, h Handler, d ...Doc) *Node

Method implements an HTTP method for a handler and optionally allows a third documentation parameter

func (*Node) Node Uses

func (n *Node) Node(p string) *Node

Node appends a child node

func (*Node) Operation Uses

func (n *Node) Operation(p string) *Operation

Operation appends an operation to a *Node

func (*Node) SetPath Uses

func (n *Node) SetPath(p string)

SetPath modifies a node path

type Operation Uses

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 Uses

func NewOperation() *Operation

NewOperation instances and initialize an Operation

func (*Operation) Interceptor Uses

func (o *Operation) Interceptor(m *Interceptor) *Operation

Interceptor attaches an Interceptor to an operation

func (*Operation) Method Uses

func (o *Operation) Method(m string, h Handler) *Operation

Method implement an HTTP method for an operation

Directories

PathSynopsis
example

Package golax imports 8 packages (graph) and is imported by 6 packages. Updated 2018-06-05. Refresh now. Tools for package owners.