Documentation ¶
Index ¶
- func HandleError(req *Req, err error)
- func Must(data []byte, err error) []byte
- func Send(ctx context.Context, req *Req, rm ResponseMarshaller) error
- type API
- type DataSource
- type Encoder
- type Endpoint
- type Error
- type Errors
- type Getter
- type GetterSetter
- type Handler
- type HandlerFunc
- type JsonEncoder
- type Middleware
- type MiddlewareFunc
- type MiddlewareStack
- type Params
- type Req
- func (r *Req) Decode(v interface{}) error
- func (r *Req) JsonBody() ([]byte, error)
- func (r *Req) JsonForm() ([]byte, error)
- func (r *Req) NoContent(code int)
- func (r *Req) ParseParams() error
- func (r *Req) Redirect(code int, url string)
- func (r *Req) ResolveContentType() string
- func (r *Req) ResponseStatus() int
- type RequestParser
- type Resource
- func (r *Resource) Create(ctx context.Context) (context.Context, error)
- func (r *Resource) Delete(ctx context.Context) (context.Context, error)
- func (r *Resource) HandleCreate(ctx context.Context, rp RequestParser) (context.Context, error)
- func (r *Resource) HandleDelete(ctx context.Context, rp RequestParser) (context.Context, error)
- func (r *Resource) HandleError(err error)
- func (r *Resource) HandleIndex(ctx context.Context, rp RequestParser) (context.Context, error)
- func (r *Resource) HandleRead(ctx context.Context, rp RequestParser) (context.Context, error)
- func (r *Resource) HandleUpdate(ctx context.Context, rp RequestParser) (context.Context, error)
- func (r *Resource) Index(ctx context.Context) (context.Context, error)
- func (r *Resource) Read(ctx context.Context) (context.Context, error)
- func (r *Resource) Send(ctx context.Context, rm ResponseMarshaller) error
- func (r *Resource) Update(ctx context.Context) (context.Context, error)
- type ResponseMarshaller
- 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 Middleware MiddlewareStack Prefix string // contains filtered or unexported fields }
Top-level API structure
type DataSource ¶
type DataSource interface { // FindOne returns a model from a parsed query FindOne(context.Context) (context.Context, error) // FindAll returns all objects specified in query FindAll(context.Context) (context.Context, error) // Create a new object and return its ID Create(context.Context) (context.Context, error) // Update an object and return its ID Update(context.Context) (context.Context, error) // Delete an object Delete(context.Context) (context.Context, error) }
DataSource provides methods needed for CRUD.
type Encoder ¶
An Encoder implements an encoding format of values to be sent as response to requests on the API endpoints.
type Endpoint ¶
type Endpoint struct { Method string Path string // The middlewares to execute on the request. Middleware MiddlewareStack // Called after middleware stack was executed on the request Implementation func(ctx context.Context, 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"` }
Error implements the jsonapi.org spec for errors
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 ¶
HandlerFunc
type JsonEncoder ¶
type JsonEncoder struct{}
func (JsonEncoder) Encode ¶
func (_ JsonEncoder) Encode(v ...interface{}) ([]byte, error)
jsonEncoder is an Encoder that produces JSON-formatted responses.
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). Keep in ctx instead? ContentType string // Content-Type of 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 RequestParser ¶
type Resource ¶
type Resource struct { Req *Req Source DataSource }
func NewResource ¶
func NewResource(req *Req, source DataSource) *Resource
func (*Resource) HandleCreate ¶
func (*Resource) HandleDelete ¶
func (*Resource) HandleError ¶
func (*Resource) HandleIndex ¶
func (*Resource) HandleRead ¶
func (*Resource) HandleUpdate ¶
type ResponseMarshaller ¶
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))