rest

package module
v0.0.0-...-3b3535a Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2016 License: Apache-2.0 Imports: 10 Imported by: 2

README

rest

Raiqub/rest is a library for the Go Programming Language. It provides a RESTful router for APIs.

Status

Build Status Coverage GoDoc

Installation

To install raiqub/rest library run the following command:

go get github.com/raiqub/rest

Documentation

Overview

Package rest provides operations to implement a RESTful API.

Route

A Route provides a easy way to define HTTP routes and handle it.

Rest

A Rest provides an URL router for a RESTful API.

Route

A Route provides a easy way to define HTTP routes and handle it.

Index

Constants

View Source
const (
	DEFAULT_CORS_PREFLIGHT_METHOD = "OPTIONS"
	DEFAULT_CORS_MAX_AGE          = time.Hour * 24 / time.Second
	DEFAULT_CORS_METHODS          = "OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT"
	DEFAULT_CORS_ORIGIN           = "*"
)

Variables

This section is empty.

Functions

func RecoverHandlerJson

func RecoverHandlerJson(next http.Handler) http.Handler

RecoverHandlerJson is a HTTP request middleware that captures panic errors and returns it as HTTP JSON response.

Types

type CORSHandler

type CORSHandler struct {
	PredicateOrigin dot.PredicateStringFunc
	Headers         []string
	ExposedHeaders  []string
}

A CORSHandler allows to create a CORS-able API.

func NewCORSHandler

func NewCORSHandler() *CORSHandler

NewCORSHandler creates a new CORSHandler with default values.

func (*CORSHandler) CreatePreflight

func (s *CORSHandler) CreatePreflight(routes Routes) Routes

CreatePreflight creates HTTP routes that handles pre-flight requests.

type CORSMiddleware

type CORSMiddleware struct {
	CORSHandler
	UseCredentials bool
}

A CORSMiddleware represents a HTTP middleware that handle HTTP headers for CORS-able API.

func (*CORSMiddleware) Handle

func (s *CORSMiddleware) Handle(next http.Handler) http.Handler

Handle is a HTTP handler for CORS-able API.

type CORSPreflight

type CORSPreflight struct {
	CORSHandler
	Methods        []string
	UseCredentials bool
}

A CORSPreflight represents a HTTP server that handles pre-flight requests.

func (*CORSPreflight) ServeHTTP

func (s *CORSPreflight) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handle a pre-flight request.

type Rest

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

A Rest register resources and middlewares for a HTTP handler.

It implements the http.Handler interface, so it can be registered to serve requests.

func NewRest

func NewRest() *Rest

NewRest returns a new instance of Rest router.

func (*Rest) AddMiddlewarePrivate

func (rest *Rest) AddMiddlewarePrivate(m web.MiddlewareFunc)

AddMiddlewarePrivate adds a layer to handle private resource requests.

func (*Rest) AddMiddlewarePublic

func (rest *Rest) AddMiddlewarePublic(m web.MiddlewareFunc)

AddMiddlewarePublic adds a layer to handle public resource requests.

func (*Rest) AddResource

func (rest *Rest) AddResource(r Routable)

AddResource adds a new REST-resource to handle requests.

func (*Rest) EnableCORS

func (rest *Rest) EnableCORS()

EnableCORS allows to current API support CORS specification.

func (*Rest) ResourcesRoutes

func (rest *Rest) ResourcesRoutes() Routes

Routes returns the routes from registered resources.

func (*Rest) ServeHTTP

func (rest *Rest) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP dispatches the handler registered in the matched route.

type Routable

type Routable interface {
	Routes() Routes
}

The Routable interface is implemented by objects that has HTTP routes.

type Route

type Route struct {
	// Unique name to current route.
	Name string
	// HTTP method handled by this route.
	Method string
	// HTTP path handled by this route.
	Path string
	// Indicates whether authentication is required to call this route.
	MustAuth bool
	// Defines which method is called to handle this route.
	ActionFunc http.HandlerFunc
}

A Route represents a HTTP route.

type RouteVars

type RouteVars map[string]string

A RouteVars represents the route variables for specified request.

func Vars

func Vars(r *http.Request) RouteVars

Vars returns the route variables for the current request, if any.

func (RouteVars) GetInt

func (s RouteVars) GetInt(key string) (int, bool)

GetObjectId tries to gets the value for specified key as int.

func (RouteVars) GetObjectId

func (s RouteVars) GetObjectId(key string) (bson.ObjectId, bool)

GetObjectId tries to gets the value for specified key as BSON ObjectId.

func (RouteVars) GetString

func (s RouteVars) GetString(key string) (string, bool)

GetObjectId tries to gets the value for specified key as string.

type Routes

type Routes []Route

Routes represents a slice of Route objects.

func MergeRoutes

func MergeRoutes(r ...Routable) Routes

MergeRoutes returns a slice with all routes returned by Routable objects.

Jump to

Keyboard shortcuts

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