web

package
v0.0.0-...-8a36715 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2016 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package web provides a thin layer of support for writing web services.

Index

Constants

View Source
const KeyValues ctxKey = 1

KeyValues is how request values or stored/retrieved.

View Source
const TraceIDHeader = "X-Trace-ID"

TraceIDHeader is the header added to outgoing requests which adds the traceID to it.

Variables

View Source
var (
	// ErrNotAuthorized occurs when the call is not authorized.
	ErrNotAuthorized = errors.New("Not authorized")

	// ErrDBNotConfigured occurs when the DB is not initialized.
	ErrDBNotConfigured = errors.New("DB not initialized")

	// ErrNotFound is abstracting the mgo not found error.
	ErrNotFound = errors.New("Entity not found")

	// ErrInvalidID occurs when an ID is not in a valid form.
	ErrInvalidID = errors.New("ID is not in it's proper form")

	// ErrValidation occurs when there are validation errors.
	ErrValidation = errors.New("Validation errors occurred")
)

Functions

func Error

func Error(cxt context.Context, w http.ResponseWriter, traceID string, err error)

Error handles all error responses for the API.

func Respond

func Respond(ctx context.Context, w http.ResponseWriter, traceID string, data interface{}, code int)

Respond sends JSON to the client. If code is StatusNoContent, v is expected to be nil.

func RespondError

func RespondError(ctx context.Context, w http.ResponseWriter, traceID string, err error, code int)

RespondError sends JSON describing the error

func Run

func Run(host string, routes http.Handler, readTimeout, writeTimeout time.Duration) error

Run is called to start the web service.

func Unmarshal

func Unmarshal(r io.Reader, v interface{}) error

Unmarshal decodes the input to the struct type and checks the fields to verify the value is in a proper state.

Types

type App

type App struct {
	*httptreemux.TreeMux
	Values map[string]interface{}
	// contains filtered or unexported fields
}

App is the entrypoint into our application and what configures our context object for each of our http handlers. Feel free to add any configuration data/logic on this App struct

func New

func New(mw ...Middleware) *App

New create an App value that handle a set of routes for the application. You can provide any number of middleware and they'll be used to wrap every request handler.

func (*App) Group

func (a *App) Group(mw ...Middleware) *Group

Group creates a new App Group based on the current App and provided middleware.

func (*App) Handle

func (a *App) Handle(verb, path string, handler Handler, mw ...Middleware)

Handle is our mechanism for mounting Handlers for a given HTTP verb and path pair, this makes for really easy, convenient routing.

func (*App) Use

func (a *App) Use(mw ...Middleware)

Use adds the set of provided middleware onto the Application middleware chain. Any route running off of this App will use all the middleware provided this way always regardless of the ordering of the Handle/Use functions.

type Group

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

Group allows a segment of middleware to be shared amongst handlers.

func (*Group) Handle

func (g *Group) Handle(verb, path string, handler Handler, mw ...Middleware)

Handle proxies the Handle function of the underlying App.

func (*Group) Use

func (g *Group) Use(mw ...Middleware)

Use adds the set of provided middleware onto the Application middleware chain.

type Handler

type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string)

A Handler is a type that handles an http request within our own little mini framework.

type Invalid

type Invalid struct {
	Fld string `json:"field_name"`
	Err string `json:"error"`
}

Invalid describes a validation error belonging to a specific field.

type InvalidError

type InvalidError []Invalid

InvalidError is a custom error type for invalid fields.

func (InvalidError) Error

func (err InvalidError) Error() string

Error implements the error interface for InvalidError.

type JSONError

type JSONError struct {
	Error  string       `json:"error"`
	Fields InvalidError `json:"fields,omitempty"`
}

JSONError is the response for errors that occur within the API.

type Middleware

type Middleware func(Handler) Handler

A Middleware is a type that wraps a handler to remove boilerplate or other concerns not direct to any given Handler.

type Values

type Values struct {
	DB         *mgo.Session
	TraceID    string
	Now        time.Time
	StatusCode int
}

Values represent state for each request.

Jump to

Keyboard shortcuts

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