Documentation ¶
Overview ¶
Package presto gives you a strong foundation for creating REST interfaces using Go and the [Echo Router](http://echo.labstack.com)
Index ¶
- func Get(ctx Context, service Service, cache Cache, scopes ScopeFuncSlice, ...) (int, data.Object)
- func RequestInfo(context Context) map[string]string
- func UseCache(cache Cache)
- func UseRouter(router *echo.Echo)
- func UseScopes(scopes ...ScopeFunc)
- type Cache
- type Collection
- func (collection *Collection) Delete(roles ...RoleFunc) *Collection
- func (collection *Collection) Get(roles ...RoleFunc) *Collection
- func (collection *Collection) List(roles ...RoleFunc) *Collection
- func (collection *Collection) Method(name string, handler echo.HandlerFunc)
- func (collection *Collection) Patch(roles ...RoleFunc) *Collection
- func (collection *Collection) Post(roles ...RoleFunc) *Collection
- func (collection *Collection) Put(roles ...RoleFunc) *Collection
- func (collection *Collection) UseCache(cache Cache) *Collection
- func (collection *Collection) UseScopes(scopes ...ScopeFunc) *Collection
- func (collection *Collection) UseToken(token string) *Collection
- type Context
- type ETagger
- type RoleFunc
- type RoleFuncSlice
- type ScopeFunc
- type ScopeFuncSlice
- type Service
- type ServiceFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶ added in v0.4.2
func Get(ctx Context, service Service, cache Cache, scopes ScopeFuncSlice, roles RoleFuncSlice) (int, data.Object)
Get does *most* of the work of handling an http.GET request. It checks scopes and roles, then loads an object and returns it along with a valid HTTP status code. You can use this as a shortcut in your own HTTP handler functions, and can wrap additional logic before and after this work.
func RequestInfo ¶
RequestInfo inspects a request and returns any information that might be useful for debugging problems. It is primarily used by internal methods whenever there's a problem with a request.
func UseCache ¶ added in v0.3.0
func UseCache(cache Cache)
UseCache sets the global cache for all presto endpoints.
Types ¶
type Cache ¶
type Cache interface { // Get returns the cache value (ETag) corresponding to the argument (objectID) provided. // If a value is not found, then Get returns empty string ("") Get(objectID string) string // Set updates the value in the cache, returning a derp.Error in case there was a problem. Set(objectID string, value string) *derp.Error }
Cache maintains fast access to key/value pairs that are used to check ETags of incoming requests. By default, Presto uses a Null cache, that simply reports cache misses for every request. However, this can be extended by the user, with any external caching system that matches this interface.
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection provides all of the HTTP hanlers for a specific domain object, or collection of records
func NewCollection ¶
func NewCollection(serviceFunc ServiceFunc, prefix string) *Collection
NewCollection returns a fully populated Collection object
func (*Collection) Delete ¶
func (collection *Collection) Delete(roles ...RoleFunc) *Collection
Delete returns an HTTP handler that knows how to delete records from the collection
func (*Collection) Get ¶
func (collection *Collection) Get(roles ...RoleFunc) *Collection
Get returns an HTTP handler that knows how to retrieve a single record from the collection
func (*Collection) List ¶
func (collection *Collection) List(roles ...RoleFunc) *Collection
List returns an HTTP handler that knows how to list a series of records from the collection
func (*Collection) Method ¶
func (collection *Collection) Method(name string, handler echo.HandlerFunc)
Method defines a custom "method"-style endpoint
func (*Collection) Patch ¶
func (collection *Collection) Patch(roles ...RoleFunc) *Collection
Patch returns an HTTP handler that knows how to update in the collection
func (*Collection) Post ¶
func (collection *Collection) Post(roles ...RoleFunc) *Collection
Post returns an HTTP handler that knows how to create new objects in the collection
func (*Collection) Put ¶
func (collection *Collection) Put(roles ...RoleFunc) *Collection
Put returns an HTTP handler that knows how to update in the collection
func (*Collection) UseCache ¶ added in v0.3.0
func (collection *Collection) UseCache(cache Cache) *Collection
UseCache adds a local ETag cache for this collection only
func (*Collection) UseScopes ¶ added in v0.3.0
func (collection *Collection) UseScopes(scopes ...ScopeFunc) *Collection
UseScopes replaces the default scope with a new list of ScopeFuncs
func (*Collection) UseToken ¶ added in v0.3.0
func (collection *Collection) UseToken(token string) *Collection
UseToken overrides the default "token" variable that is appended to all GET, PUT, PATCH, and DELETE routes, and is used as the unique identifier of the record being created, read, updated, or deleted.
type Context ¶ added in v0.3.2
type Context interface { // Request returns the raw HTTP request object that we're responding to Request() *http.Request // Path returns the registered path for the handler. Path() string // RealIP returns the client's network address based on `X-Forwarded-For` // or `X-Real-IP` request header. RealIP() string // ParamNames returns a slice of route parameter names that are present in the request path ParamNames() []string // Param returns the value of an individual route parameter in the request path Param(name string) string // QueryParams returns the raw values of all query parameters passed in the request URI. QueryParams() url.Values // FormParams returns the raw values of all form parameters passed in the request body. FormParams() (url.Values, error) // Bind binds the request body into provided type `i`. The default binder // does it based on Content-Type header. Bind(interface{}) error // JSON sends a JSON response with status code. JSON(code int, value interface{}) error // HTML sends an HTTP response with status code. HTML(code int, html string) error // NoContent sends a response with no body and a status code. NoContent(code int) error }
Context represents the minimum interface that a presto HTTP handler can depend on. It is essentially a subset of the Context interface, and adapters will be written in github.com/benpate/multipass to bridge this API over to other routers.
type ETagger ¶ added in v0.2.8
type ETagger interface { // ETag returns a version-unique string that helps determine if an object has changed or not. ETag() string }
ETagger interface wraps the ETag function, which tells presto whether or not an object supports ETags. Presto uses ETags to automatically support optimistic locking of files, as well as saving time and bandwidth using 304: "Not Modified" responses when possible.
type RoleFunc ¶
RoleFunc is a function signature that validates a user's permission to access a particular object
type RoleFuncSlice ¶ added in v0.4.2
type RoleFuncSlice []RoleFunc
RoleFuncSlice defines behaviors for a slice of RoleFuncs
type ScopeFunc ¶ added in v0.2.0
type ScopeFunc func(context Context) (expression.Expression, *derp.Error)
ScopeFunc is the function signature for a function that can limit database queries to a particular "scope". It inspects the provided context and returns criteria that will be passed to all database queries.
type ScopeFuncSlice ¶ added in v0.4.2
type ScopeFuncSlice []ScopeFunc
ScopeFuncSlice defines behaviors for a slice of Scopes
func (*ScopeFuncSlice) Add ¶ added in v0.8.0
func (scopes *ScopeFuncSlice) Add(new ...ScopeFunc)
func (ScopeFuncSlice) Evaluate ¶ added in v0.4.2
func (scopes ScopeFuncSlice) Evaluate(ctx Context) (expression.AndExpression, *derp.Error)
Evaluate resolves all scopes into an expression (or error) using the provided Context
type Service ¶ added in v0.2.0
type Service interface { // NewObject creates a newly initialized object that is ready to use NewObject() data.Object // ListObjects returns an iterator the returns all objects ListObjects(criteria expression.Expression, options ...option.Option) (data.Iterator, *derp.Error) // LoadObject retrieves a single object from the database LoadObject(criteria expression.Expression) (data.Object, *derp.Error) // SaveObject inserts/updates a single object in the database SaveObject(object data.Object, comment string) *derp.Error // DeleteObject removes a single object from the database DeleteObject(object data.Object, comment string) *derp.Error // Close cleans up any connections opened by the service. Close() }
Service defines all of the functions that a service must provide to work with Presto. It relies on the generic Object interface to load and save objects of any type. GenericServices will likely include additional business logic that is triggered when a domain object is created, edited, or deleted, but this is hidden from presto.
type ServiceFunc ¶ added in v0.2.0
ServiceFunc is a function that can generate new services/sessions. Each session represents a single HTTP request, which can potentially span multiple database calls. This gives the factory an opportunity to initialize a new database session for each HTTP request.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cache
|
|
mongocache
Package mongocache implements the presto.Cache interface using a mongodb database for persistent storage.
|
Package mongocache implements the presto.Cache interface using a mongodb database for persistent storage. |
nullcache
Package nullcache implements the presto.Cache interface with an empty data structure that never stores any data, and always reports a cache "miss".
|
Package nullcache implements the presto.Cache interface with an empty data structure that never stores any data, and always reports a cache "miss". |