api

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: Apache-2.0 Imports: 22 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMux

func GetMux(req *http.Request) http.Handler

GetMux extracts the API mux from the request.

func GetPath

func GetPath(req *http.Request) riposo.Path

GetPath extracts the resource path from the request.

func Parse

func Parse(r *http.Request, v interface{}) error

Parse parses a request body into v.

func Render

func Render(w http.ResponseWriter, v interface{})

Render renders any value as JSON.

func WithTxn

func WithTxn(ctx context.Context, txn *Txn) context.Context

WithTxn adds the transaction as a value to the context.

Types

type Config

type Config struct {
	// Guard is a custom authorization guard.
	Guard Guard

	// Pagination configures resource pagination options.
	Pagination struct {
		TokenValidity time.Duration
		MaxLimit      int
	}
}

Config holds API configuration values.

type Guard

type Guard map[string][]string

A Guard represents an authorization guard and can verify access of user principals to target entities.

func (Guard) Verify

func (v Guard) Verify(tx permission.Transaction, principals []string, target []permission.ACE) (bool, error)

Verify returns true if any of the user principals can access any of the target entities.

type HandlerFunc

type HandlerFunc func(out http.Header, req *http.Request) interface{}

HandlerFunc is a simplified API handler function.

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware represents a middleware handler function.

type Model

type Model interface {
	// Get retrieves a single resource.
	Get(txn *Txn, path riposo.Path) (*schema.Resource, error)
	// Create creates a single resource.
	Create(txn *Txn, path riposo.Path, payload *schema.Resource) error
	// Update updates a resource.
	Update(txn *Txn, path riposo.Path, hs storage.UpdateHandle, payload *schema.Resource) error
	// Patch patches a resource.
	Patch(txn *Txn, path riposo.Path, hs storage.UpdateHandle, payload *schema.Resource) error
	// Delete deletes a resource.
	Delete(txn *Txn, path riposo.Path) (*schema.Object, error)
	// DeleteAll deletes resources.
	DeleteAll(txn *Txn, path riposo.Path, objIDs ...string) (riposo.Epoch, error)
}

Model is a CRUD resource operator.

func StdModel

func StdModel() Model

StdModel inits a standard model.

type Routes

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

Routes contains the main API route defintions.

func NewRoutes

func NewRoutes(cfg *Config) *Routes

NewRoutes inits a new routes instance.

func (*Routes) Handle

func (r *Routes) Handle(pattern string, handler http.Handler)

Handle registers a new API handler.

func (*Routes) Method

func (r *Routes) Method(method, pattern string, handler http.Handler)

Method registers a new HTTP handler under a particular HTTP method.

func (*Routes) Mux

func (r *Routes) Mux() http.Handler

Mux returns the mountable API mux.

func (*Routes) Resource

func (r *Routes) Resource(prefix string, model Model)

Resource registers a new resource under a prefix.

func (*Routes) Use

func (r *Routes) Use(middleware Middleware)

Use registers a new middleware.

type Txn

type Txn struct {
	context.Context
	Store   storage.Transaction
	Perms   permission.Transaction
	Cache   cache.Transaction
	Helpers *riposo.Helpers
	User    *User
	Data    map[string]interface{}
}

Txn wraps an API transaction.

func GetTxn

func GetTxn(req *http.Request) *Txn

GetTxn extracts the current transaction from the request.

func NewTxn

func NewTxn(ctx context.Context, cns *conn.Set, hlp *riposo.Helpers) (*Txn, error)

NewTxn inits a new transaction.

func (*Txn) Abort

func (t *Txn) Abort() error

Abort is used internally to abort all transactions.

func (*Txn) Commit

func (t *Txn) Commit() error

Commit is used internally to commit all transactions.

type User

type User struct {
	ID         string
	Principals []string // extra principals
}

User is the authenticated API user.

func (*User) IsAuthenticated

func (u *User) IsAuthenticated() bool

IsAuthenticated reports true if the User is an authenticated user.

Jump to

Keyboard shortcuts

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