Documentation ¶
Overview ¶
Based on [https://github.com/martini-contrib/cors](https://github.com/martini-contrib/cors)
Package rest provides functionality to initiate a REST service, register routes and handles and handling CORS header.
Index ¶
- func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)
- func NewCORS(opts *CORSOptions) http.HandlerFunc
- func NotFoundHandler(w http.ResponseWriter, r *http.Request, err error)
- func SetStatus(w http.ResponseWriter, v interface{})
- func ToBool(value string) (bool, bool)
- func ToBytes(value string) ([]byte, bool)
- func ToFloat32(value string) (float32, bool)
- func ToFloat64(value string) (float64, bool)
- func ToInt(value string) (int, bool)
- func ToInt32(value string) (int32, bool)
- func ToInt64(value string) (int64, bool)
- func ToString(value string) (string, bool)
- func ToUint(value string) (uint, bool)
- func ToUint32(value string) (uint32, bool)
- func ToUint64(value string) (uint64, bool)
- type CORSOptions
- type Configuration
- type ErrorHandler
- type HandlerRegistration
- type Int32Slice
- type Middleware
- type Param
- type Params
- type Register
- type Service
- func (s *Service) Delete(uri string, handler http.Handler)
- func (s *Service) Get(uri string, handler http.Handler)
- func (s *Service) Head(uri string, handler http.Handler)
- func (s *Service) Options(uri string, handler http.Handler)
- func (s *Service) Patch(uri string, handler http.Handler)
- func (s *Service) Post(uri string, handler http.Handler)
- func (s *Service) Put(uri string, handler http.Handler)
- func (s *Service) Register(r []Register, baseURI string) error
- func (s *Service) Route(method, uri string, handler http.Handler) error
- func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type Statuser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultErrorHandler ¶
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)
DefaultErrorHandler is a default implementation of an Error Handler taken by the service framework.
func NewCORS ¶
func NewCORS(opts *CORSOptions) http.HandlerFunc
NewCORS enables CORS for requests those match the provided options.
func NotFoundHandler ¶
func NotFoundHandler(w http.ResponseWriter, r *http.Request, err error)
NotFoundHandler is a default implementation of an NotFound Handler taken by the service framework.
func SetStatus ¶
func SetStatus(w http.ResponseWriter, v interface{})
SetStatus sets the HTTP status in w if v satifies the Statuser interface.
Types ¶
type CORSOptions ¶
type CORSOptions struct { // If set, all origins are allowed. AllowAllOrigins bool // A list of allowed origins. Wild cards and FQDNs are supported. AllowOrigins []string // If set, allows to share auth credentials such as cookies. AllowCredentials bool // A list of allowed HTTP methods. AllowMethods []string // A list of allowed HTTP headers. AllowHeaders []string // A list of exposed HTTP headers. ExposeHeaders []string // Max age of the CORS headers. MaxAge time.Duration }
CORSOptions represents the available CORS options
func DefaultCORSOptions ¶
func DefaultCORSOptions() *CORSOptions
DefaultCORSOptions creates new options, allows all origins and returns them.
func (*CORSOptions) Header ¶
func (o *CORSOptions) Header(origin string) (headers map[string]string)
Header converts options into CORS headers.
func (*CORSOptions) IsOriginAllowed ¶
func (o *CORSOptions) IsOriginAllowed(origin string) (allowed bool)
IsOriginAllowed looks up if the origin matches one of the patterns generated from Options.AllowOrigins patterns.
func (*CORSOptions) PreflightHeader ¶
func (o *CORSOptions) PreflightHeader(origin, rMethod, rHeaders string) (headers map[string]string)
PreflightHeader converts options into CORS headers for a preflight response.
type Configuration ¶
type Configuration struct { // BaseURI can prefix the path for this handler. A common example for REST would be the version like "/v1" BaseURI string //ErrorHandler will be called in the middlewares and the framework if an error occurs. If not present the DefaultErrorHandler will be used ErrorHandler ErrorHandler // Chain allow to execute some middlewares around the actual handler. In the slice the the most left (index 0) middleware will be executed first (from the view of an incoming request) Chain []Middleware // Middlewares that wrap the Route Handlers. The Route Selection happens before // CORS set if CORS Headers should be provided, CORS bool // CORSOptions allows to give the CORS Options to the service. If CORS is set to true and no CORSOptions are given all origins will be allowed CORSOptions *CORSOptions }
Configuration container the configuration Parameter needed to initialize the GRPCRESTService
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
ErrorHandler is an interface to provide error handling.
type HandlerRegistration ¶
type HandlerRegistration interface { GetBaseURI() string GetHandlersToRegister() []Register SetErrorHandler(h ErrorHandler) error }
HandlerRegistration provides methods neccessary to register routes and handlers.
type Int32Slice ¶
type Int32Slice []int32
Int32Slice represents a slice of int32.
func (Int32Slice) Len ¶
func (p Int32Slice) Len() int
func (Int32Slice) Less ¶
func (p Int32Slice) Less(i, j int) bool
func (Int32Slice) Swap ¶
func (p Int32Slice) Swap(i, j int)
type Middleware ¶
type Middleware func(http.HandlerFunc) http.HandlerFunc
Middleware is an interface to concatenate functions to chains.
type Register ¶
type Register struct { Method string Path string Handler http.HandlerFunc }
Register wraps a method, a path and a handler.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements a HTTP Router + Some Helpers for chaining and error handling. It is used for the GRPC-REST Gateway
func New ¶
func New(cfg Configuration, registrators []HandlerRegistration) *Service
New created a new GRPCRESTServices and applies the configuration and register the handlers given by the registrators
func (*Service) Register ¶
Register registers a list of handers/paths/methods wrapping them in the middleware chain