app

package
v0.0.0-...-15608e1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2016 License: Apache-2.0 Imports: 14 Imported by: 0

README

app

import "github.com/ardanlabs/kit/web/app"

Package app provides a thin layer of support for writing web services. It integrates with the ardanlabs kit repo to provide support for logging, configuration, database, routing and application context. The base things you need to write a web service is provided.

Package app provides application support for context and MongoDB access. Current Status Codes:

200 OK           : StatusOK                  : Call is success and returning data.
204 No Content   : StatusNoContent           : Call is success and returns no data.
400 Bad Request  : StatusBadRequest          : Invalid post data (syntax or semantics).
401 Unauthorized : StatusUnauthorized        : Authentication failure.
404 Not Found    : StatusNotFound            : Invalid URL or identifier.
500 Internal     : StatusInternalServerError : Application specific beyond scope of user.

Variables

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

func Init

func Init(p cfg.Provider)

Init is called to initialize the application.

func Run

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

Run is called to start the web service.

type App

type App struct {
    *httptreemux.TreeMux
    Ctx 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) CORS
func (a *App) CORS()

CORS providing support for Cross-Origin Resource Sharing. https://metajack.im/2010/01/19/crossdomain-ajax-for-xmpp-http-binding-made-easy/

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.

type Context

type Context struct {
    http.ResponseWriter
    Request   *http.Request
    Now       time.Time
    Params    map[string]string
    SessionID string
    Status    int
    Ctx       map[string]interface{}
    App       *App
}

Context contains data associated with a single request.

func (*Context) Error
func (c *Context) Error(err error)

Error handles all error responses for the API.

func (*Context) Respond
func (c *Context) Respond(data interface{}, code int)

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

func (*Context) RespondError
func (c *Context) RespondError(error string, code int)

RespondError sends JSON describing the error

func (*Context) RespondInvalid
func (c *Context) RespondInvalid(fields []Invalid)

RespondInvalid sends JSON describing field validation errors.

type Handler

type Handler func(*Context) error

A Handler is a type that handles an http request within our own little mini framework. The fun part is that our context is fully controlled and configured by us so we can extend the functionality of the Context whenever we want.

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 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.


Generated by godoc2md

Documentation

Overview

Package app provides a thin layer of support for writing web services. It integrates with the ardanlabs kit repo to provide support for logging, configuration, database, routing and application context. The base things you need to write a web service is provided.

Package app provides application support for context and MongoDB access. Current Status Codes:

200 OK           : StatusOK                  : Call is success and returning data.
204 No Content   : StatusNoContent           : Call is success and returns no data.
400 Bad Request  : StatusBadRequest          : Invalid post data (syntax or semantics).
401 Unauthorized : StatusUnauthorized        : Authentication failure.
404 Not Found    : StatusNotFound            : Invalid URL or identifier.
500 Internal     : StatusInternalServerError : Application specific beyond scope of user.

Index

Constants

This section is empty.

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 Init

func Init(p cfg.Provider)

Init is called to initialize the application.

func Run

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

Run is called to start the web service.

Types

type App

type App struct {
	*httptreemux.TreeMux
	Ctx 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) CORS

func (a *App) CORS()

CORS providing support for Cross-Origin Resource Sharing. https://metajack.im/2010/01/19/crossdomain-ajax-for-xmpp-http-binding-made-easy/

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.

type Context

type Context struct {
	http.ResponseWriter
	Request   *http.Request
	Now       time.Time
	Params    map[string]string
	SessionID string
	Status    int
	Ctx       map[string]interface{}
	App       *App
}

Context contains data associated with a single request.

func (*Context) Error

func (c *Context) Error(err error)

Error handles all error responses for the API.

func (*Context) Respond

func (c *Context) Respond(data interface{}, code int)

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

func (*Context) RespondError

func (c *Context) RespondError(error string, code int)

RespondError sends JSON describing the error

func (*Context) RespondInvalid

func (c *Context) RespondInvalid(fields []Invalid)

RespondInvalid sends JSON describing field validation errors.

type Handler

type Handler func(*Context) error

A Handler is a type that handles an http request within our own little mini framework. The fun part is that our context is fully controlled and configured by us so we can extend the functionality of the Context whenever we want.

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 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.

Jump to

Keyboard shortcuts

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