Documentation ¶
Overview ¶
Package pastis implements a simple library for building RESTful APIs. This package provides you with everything you will need for most of your applications. Pastis has 3 building blocks : 1) An API associated to a set of Resource and callback functions. It is paired with an arbitrary port. 2) Resource paired with an URL-pattern. Its represents a REST resource. 3) A callback paired with a URL-pattern and a request method. Note that a pastis server can support more than one API. Pastis rich features are : Nice URL-pattern matching, Path parameter parsing, Configurable loggers, CORS support, Speaks JSON and Type-safe request
Index ¶
- Constants
- func CORSFilter(rw http.ResponseWriter, request *http.Request, chain *FilterChain)
- func ErrorResponse(err error) interface{}
- func HandlerPath(pattern string) string
- func LevelInt(level string) int
- func LoggingFilter(w http.ResponseWriter, request *http.Request, chain *FilterChain)
- func Match(r *regexp.Regexp, path string) (bool, map[string]string)
- func Regexp(pattern string) *regexp.Regexp
- type API
- func (api *API) AddFilter(filter Filter)
- func (api *API) AddResource(pattern string, resource interface{})
- func (api *API) Delete(fn interface{}, pattern string)
- func (api *API) Do(requestMethod string, pattern string, fn interface{})
- func (api *API) Get(pattern string, fn interface{})
- func (api *API) HandleFunc()
- func (api *API) Head(pattern string, fn interface{})
- func (api *API) Link(pattern string, fn interface{})
- func (api *API) Options(pattern string, fn interface{})
- func (api *API) Patch(pattern string, fn interface{})
- func (api *API) Post(pattern string, fn interface{})
- func (api *API) Put(pattern string, fn interface{})
- func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (api *API) SetLevel(level string)
- func (api *API) SetOutput(level string, w io.Writer, flag int) *Logger
- func (api *API) Start(port int) error
- func (api *API) Unlink(pattern string, fn interface{})
- type AdminResource
- type Filter
- type FilterChain
- type Logger
- func (logger *Logger) Debug(s string)
- func (logger *Logger) Debugf(format string, v ...interface{})
- func (logger *Logger) Error(s string)
- func (logger *Logger) Errorf(format string, v ...interface{})
- func (logger *Logger) Fatal(s string)
- func (logger *Logger) Fatalf(format string, v ...interface{})
- func (logger *Logger) Info(s string)
- func (logger *Logger) Infof(format string, v ...interface{})
- func (logger *Logger) SetLevel(level string)
- func (logger *Logger) SetOutput(level string, w io.Writer, flag int)
- func (logger *Logger) SetOutputs(w io.Writer, flag int, levels ...string)
- func (logger *Logger) Warn(s string)
- func (logger *Logger) Warnf(format string, v ...interface{})
- type Router
Constants ¶
const ( HEADER_Allow_Methods = "GET,POST,DELETE,PUT,PATCH,HEAD" HEADER_Access_Control_Allow_Headers = "Origin,Accept,Produce,Content-Type,X-Requested-With,Authorization,Token" HEADER_Origin = "Origin" HEADER_Allow_Credentials = "Access-Control-Allow-Credentials" HEADER_Allow_Origin = "Access-Control-Allow-Origin" HEADER_AccessControlRequestHeaders = "Access-Control-Request-Headers" HEADER_Access_Control_Request_Method = "Access-Control-Request-Method" )
const ( DEBUG = 1 INFO = 2 WARN = 3 ERROR = 4 FATAL = 5 OFF = 6 )
Variables ¶
This section is empty.
Functions ¶
func CORSFilter ¶
func CORSFilter(rw http.ResponseWriter, request *http.Request, chain *FilterChain)
A Cross Origin Filter
func LoggingFilter ¶
func LoggingFilter(w http.ResponseWriter, request *http.Request, chain *FilterChain)
Filter (post-process) Filter (as a struct that defines a FilterFunction)
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
An API manages a group of resources by routing to requests to the correct method and URL.
You can instantiate multiple APIs on separate ports. Each API will manage its own set of resources.
func (*API) AddFilter ¶
AddFilter adds a new filter to an API. The API will execute the filter before calling the target function.
func (*API) AddResource ¶
AddResource adds a new resource to an API. The API will route requests that match the given path to its HTTP method on the resource.
func (*API) HandleFunc ¶
func (api *API) HandleFunc()
func (*API) ServeHTTP ¶
func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request)
Implements HandlerFunc
func (*API) SetOutput ¶
SetOutput sets the output destination for the standard logger of the given level. Example: api.SetOutput("ERROR", os.StdErr, log.Lmicroseconds)
type AdminResource ¶
type AdminResource struct { }
A simple admin REST resource
func (AdminResource) Get ¶
func (api AdminResource) Get() (int, interface{})
Simply GETs request and returns OK response
type Filter ¶
type Filter func(http.ResponseWriter, *http.Request, *FilterChain)
Filter function definition. Must be called on the FilterChain to pass on the control and eventually call the Target function
type FilterChain ¶
type FilterChain struct { Filters []Filter // ordered list of Filter function Index int // index into filters that is currently in progress Target http.HandlerFunc // function to call after passing all filters }
FilterChain is a request scoped object to process one or more filters before calling the target function.
func (*FilterChain) Copy ¶
func (f *FilterChain) Copy() FilterChain
func (*FilterChain) NextFilter ¶
func (f *FilterChain) NextFilter(rw http.ResponseWriter, request *http.Request)
NextFilter passes the request,response pair through the next of Filters. Each filter can decide to proceed to the next Filter or handle the Response itself.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
An API Logger whose level is configurable
func GetLogger ¶
GetLogger retrieves a logger having the given level. The level defines the minimum set of levels recognized by the system, that is OFF, FATAL, ERROR, WARN, INFO, DEBUG and ALL.
func (*Logger) Debug ¶
Log a message object with the DEBUG level. Arguments are handled in the manner of fmt.Print.
func (*Logger) Debugf ¶
Log a message object with the DEBUG level. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Error ¶
Log a message object with the ERROR level. Arguments are handled in the manner of fmt.Print.
func (*Logger) Errorf ¶
Log a message object with the ERROR level. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Fatal ¶
Log a message object with the FATAL level. Arguments are handled in the manner of fmt.Print.
func (*Logger) Fatalf ¶
Log a message object with the FATAL level. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Info ¶
Log a message object with the INFO level. Arguments are handled in the manner of fmt.Print.
func (*Logger) Infof ¶
Log a message object with the INFO level. Arguments are handled in the manner of fmt.Printf.
func (*Logger) SetLevel ¶
Set the minimum set of levels recognized by the system, that is OFF, FATAL, ERROR, WARN, INFO, DEBUG and ALL.
func (*Logger) SetOutput ¶
SetOutput sets the output destination for the logger having the given level.
func (*Logger) SetOutputs ¶
SetOutput sets the output destination for the logger having the given level.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is a struct consisting of a set of method paired with URL-matchin pattern where each pair is mapped to an handler function.
func (*Router) Add ¶
func (router *Router) Add(pattern string, method string, handler http.HandlerFunc)
Add adds a route wichi consist of an URL-pattern matching, a method and an handler of type http.HandlerFunc
func (*Router) Handler ¶
func (router *Router) Handler(logger *Logger) http.HandlerFunc
Handler returns an handler function of the API. This handler is built from the set of routes that have been defined previously.
func (*Router) OpsFriendlyLog ¶
Prints out the routes in a friendly manner