rest

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: MIT Imports: 13 Imported by: 1

README

rest

Build Status GoDoc

Package rest provides functionality to:

  • initiate a REST service,
  • register REST routes and handler,
  • handle REST requests and
  • handles CORS header.

Installation

go get github.com/doozer-de/rest

Usage

This library works well with restgen.

Limitations

Due to usage of golang.org/x/net/context in current Protobuf implementation func(ctx context.Context, w http.ResponseHandler, r *http.Request) signature is used for HTTP handlers instead for http.HandlerFunc.

Credits

Parts of HTTP router: Julien Schmidt github.com/julienschmidt/httprouter Servier based on: Christoph Seufert github.com/0x434d53/service. CORS based on: Jaana Burcu Dogan https://github.com/martini-contrib/cors

License

Copyright © 2016-2017 DOOZER REAL ESTATE SYSTEMS GMBH

Licensed under the MIT license.

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

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.

func ToBool

func ToBool(value string) (bool, bool)

ToBool tries to parse the given string to a bool value.

func ToBytes

func ToBytes(value string) ([]byte, bool)

ToBytes tries to parse the given string to a bytes value.

func ToFloat32

func ToFloat32(value string) (float32, bool)

ToFloat32 tries to parse the given string to a float32 value.

func ToFloat64

func ToFloat64(value string) (float64, bool)

ToFloat64 tries to parse the given string to a float64 value.

func ToInt

func ToInt(value string) (int, bool)

ToInt tries to parse the given string to a int value.

func ToInt32

func ToInt32(value string) (int32, bool)

ToInt32 tries to parse the given string to a int32 value.

func ToInt64

func ToInt64(value string) (int64, bool)

ToInt64 tries to parse the given string to a int64 value.

func ToString

func ToString(value string) (string, bool)

ToString returns the given argument if string not empty.

func ToUint

func ToUint(value string) (uint, bool)

ToUint tries to parse the given string to a uint value.

func ToUint32

func ToUint32(value string) (uint32, bool)

ToUint32 tries to parse the given string to a uint32 value.

func ToUint64

func ToUint64(value string) (uint64, bool)

ToUint64 tries to parse the given string to a uint64 value.

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

func (p Int32Slice) Sort()

Sort sorts an int32 slice.

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 Param

type Param struct {
	Key   string
	Value string
}

Param wraps a key/value pair.

type Params

type Params []Param

Params represents a slice of Param.

func GetParams

func GetParams(ctx context.Context) Params

func (*Params) Get

func (p *Params) Get(key string) string

Get gets the value for the given key.

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

func (s *Service) Delete(uri string, handler http.Handler)

Delete registers a handler for DELETE and the given uri

func (*Service) Get

func (s *Service) Get(uri string, handler http.Handler)

Get registers a handler for GET and the given uri

func (*Service) Head

func (s *Service) Head(uri string, handler http.Handler)

Head registers a handler for HEAD and the given uri

func (*Service) Options

func (s *Service) Options(uri string, handler http.Handler)

Options registers a handler for OPTIONS and the given uri

func (*Service) Patch

func (s *Service) Patch(uri string, handler http.Handler)

Patch registers a handler for PATCH and the given uri

func (*Service) Post

func (s *Service) Post(uri string, handler http.Handler)

Post registers a handler for POST and the given uri

func (*Service) Put

func (s *Service) Put(uri string, handler http.Handler)

Put registers a handler for PUT and the given uri

func (*Service) Register

func (s *Service) Register(r []Register, baseURI string) error

Register registers a list of handers/paths/methods wrapping them in the middleware chain

func (*Service) Route

func (s *Service) Route(method, uri string, handler http.Handler) error

Route registers a handler for certain http method/route

func (*Service) ServeHTTP

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

ServeHTTP is the Entrypoint for an request.

type Statuser

type Statuser interface {
	Status() int
}

Statuser is an interface to get the status from an object.

Jump to

Keyboard shortcuts

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