Documentation ¶
Index ¶
- func HandleError(req *Req, err error)
- type API
- type DataSource
- type Endpoint
- type Error
- type Errors
- type Getter
- type GetterSetter
- type Handler
- type HandlerFunc
- type Middleware
- type MiddlewareFunc
- type MiddlewareStack
- type Params
- type Req
- func (r *Req) AddLog(s string)
- func (r *Req) Clear()
- func (r *Req) Decode(v interface{}) error
- func (r *Req) Get(k interface{}) interface{}
- func (r *Req) GetOk(k interface{}) (interface{}, bool)
- func (r *Req) JsonBody() ([]byte, error)
- func (r *Req) JsonForm() ([]byte, error)
- func (r *Req) ParseParams() error
- func (r *Req) ResolveContentType() string
- func (r *Req) ResponseStatus() int
- func (r *Req) Set(k interface{}, v interface{})
- type RequestModel
- type Resource
- func (r *Resource) HandleCreate(req *Req, mod RequestModel) (interface{}, error)
- func (r *Resource) HandleDelete(req *Req) (interface{}, error)
- func (r *Resource) HandleError(req *Req, err error)
- func (r *Resource) HandleIndex(req *Req) (interface{}, error)
- func (r *Resource) HandleRead(req *Req) (interface{}, error)
- func (r *Resource) HandleUpdate(req *Req, mod RequestModel) (interface{}, error)
- type Router
- type Setter
- type Values
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleError ¶
Types ¶
type API ¶
type API struct { Endpoints []Endpoint // contains filtered or unexported fields }
Top-level API structure
type DataSource ¶
type DataSource interface { // FindAll returns all objects FindAll(*Req) (interface{}, error) // FindOne returns an object by its ID FindOne(ID string, req *Req) (interface{}, error) // FindMultiple returns all objects for the specified IDs FindMultiple(IDs []string, req *Req) (interface{}, error) // Create a new object and return its ID Create(v interface{}, req *Req) (string, error) // Delete an object Delete(id string, req *Req) error // Update an object Update(obj interface{}, req *Req) error }
DataSource provides methods needed for CRUD.
type Endpoint ¶
type Endpoint struct { Verb string Path string // The middlewares to execute on the request. Middleware MiddlewareStack // Called after middleware stack was executed on the request Implementation func(r *Req) }
An Endpoint is the structure representing an API endpoint encapsulating information needed to dispatch a request and generate documentation.
func (Endpoint) Handle ¶
func (e Endpoint) Handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
Handle is a httprouter.Handle function
func (Endpoint) HandlerFunc ¶
func (e Endpoint) HandlerFunc() http.HandlerFunc
HandlerFunc converts an Endpoint to a http.HandlerFunc
func (Endpoint) ServeHTTP ¶
func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface
func (Endpoint) Use ¶
func (e Endpoint) Use(mw Middleware)
Append a middleware to the middleware stack.
type Error ¶
type Error struct { ID string `json:"id,omitempty"` Href string `json:"href,omitempty"` Status string `json:"status,omitempty,required"` Code string `json:"code,omitempty"` Title string `json:"title,omitempty,required"` Detail string `json:"detail,omitempty"` Path string `json:"path,omitempty"` Stack []byte `json:"-"` }
Error implements the jsonapi.org spec for errors
func (*Error) CaptureStackTrace ¶
func (e *Error) CaptureStackTrace()
func (Error) HTTPStatus ¶
HTTPStatus returns the int value of the error status
type Errors ¶
type Errors struct {
Err []Error `json:"errors"`
}
ErrorStack represents several errors
func (Errors) HTTPStatus ¶
HTTPStatus returns the int value of the error status
type GetterSetter ¶
type HandlerFunc ¶
type HandlerFunc func(*Req)
HandlerFunc
func (HandlerFunc) Serve ¶
func (f HandlerFunc) Serve(req *Req)
type Middleware ¶
type MiddlewareFunc ¶
MiddlewareFunc transforms a function with the right signature into a Middleware
func (MiddlewareFunc) Name ¶
func (m MiddlewareFunc) Name() string
type MiddlewareStack ¶
type MiddlewareStack []Middleware
type Params ¶
type Params struct { Values // A unified view of all the individual param maps below. Query Values // Parameters from the query string, e.g. /index?limit=10 Form Values // Parameters from the request body. Path Values // Parameters from the url path e.g. /users/:id Files map[string][]*multipart.FileHeader // Files uploaded in a multipart form RequiredParams []string // The required that have to be present for the params to be valid // contains filtered or unexported fields }
Wrapper for the request params.
type Req ¶
type Req struct { ID string // The request id Response http.ResponseWriter Request *http.Request Params *Params // Parameters from URL and form (including multipart). ContentType string // Content-Type of the request Log []string // A slice of log messages attached to the request. // contains filtered or unexported fields }
Req is a HTTP request wrapper giving you easy access to the params content type, and much more.
func WrapHttpRouterReq ¶
func WrapHttpRouterReq(w http.ResponseWriter, r *http.Request, ps httprouter.Params) *Req
WrapReq wraps a request dispatched by httprouter.
func WrapReq ¶
func WrapReq(w http.ResponseWriter, r *http.Request) *Req
WrapReq wraps a standard request.
func (*Req) JsonBody ¶
JsonBody() extracts the body from a request as a byte array so it cam be unmarshalled if desired.
func (*Req) ParseParams ¶
ParseParams from form, multipart, and query
func (*Req) ResolveContentType ¶
ResolveContentType extracts content type from the request.
func (*Req) ResponseStatus ¶
ResponseStatus returns the response status code if available yet (0 otherwise).
type RequestModel ¶
Request unmarshals a *Req into an interface value
type Resource ¶
type Resource struct {
// contains filtered or unexported fields
}
func NewResource ¶
func NewResource(model interface{}, source DataSource) *Resource
func (*Resource) HandleCreate ¶
func (r *Resource) HandleCreate(req *Req, mod RequestModel) (interface{}, error)
func (*Resource) HandleDelete ¶
func (*Resource) HandleError ¶
func (*Resource) HandleIndex ¶
func (*Resource) HandleRead ¶
func (*Resource) HandleUpdate ¶
func (r *Resource) HandleUpdate(req *Req, mod RequestModel) (interface{}, error)
type Router ¶
Router is an interface that helps activating an API to different types of router libraries (pat, httprouter, http, etc.)
func WrapRouter ¶
Wrap a router to be used with Activate i.e. api.Activate(WrapRouter(router))