Documentation
¶
Overview ¶
Package core holds the request context behind the public tifa package. The server package builds contexts through the functions here, which application code cannot import.
Index ¶
- Variables
- func Finish(c *Ctx) error
- func SetNext(c *Ctx, next func() error)
- func Use[T any](c *Ctx, load func(*Ctx) (T, error)) (T, error)
- type Ctx
- func (c *Ctx) Bind(v any) error
- func (c *Ctx) BindQuery(v any) error
- func (c *Ctx) Context() context.Context
- func (c *Ctx) Cookie(name string) (*http.Cookie, error)
- func (c *Ctx) Header(name string) string
- func (c *Ctx) IsData() bool
- func (c *Ctx) JSON(code int, v any) error
- func (c *Ctx) Next() error
- func (c *Ctx) NoContent(code int) error
- func (c *Ctx) Param(name string) string
- func (c *Ctx) Pattern() string
- func (c *Ctx) Query(name string) string
- func (c *Ctx) QueryAll(name string) []string
- func (c *Ctx) Redirect(url string, code int) error
- func (c *Ctx) Request() *http.Request
- func (c *Ctx) SetBodyLimit(n int64) error
- func (c *Ctx) SetContext(ctx context.Context)
- func (c *Ctx) SetCookie(cookie *http.Cookie)
- func (c *Ctx) SetHeader(key, value string)
- func (c *Ctx) Text(code int, s string) error
- func (c *Ctx) Writer() http.ResponseWriter
- type Error
- type Fields
- type RedirectError
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("tifa: not found")
ErrNotFound is an expected result. The server answers 404 without calling OnError.
Functions ¶
func Finish ¶
Finish releases what the request copies hold. The server calls it once the request is done.
Types ¶
type Ctx ¶
type Ctx struct {
// contains filtered or unexported fields
}
Ctx wraps one request and its response.
func New ¶
New builds the context for one request. pattern is the matched route pattern, empty when no route matched. The request body is limited to bodyLimit bytes unless SetBodyLimit changes it before the first read.
func (*Ctx) Bind ¶
Bind decodes the request body into v by Content-Type: JSON, URL-encoded form or multipart form. Client mistakes come back as Error with status 400, 413 or 415.
func (*Ctx) BindQuery ¶
BindQuery decodes the query string into the struct v, with the names and value types of a form body. Query parameters that name no field are ignored. A query string that does not parse, or a value that does not, comes back as Error with status 400.
func (*Ctx) Next ¶
Next runs the matched route from OnRequest and returns its error. It may be called once.
func (*Ctx) QueryAll ¶
QueryAll returns every value of the query parameter name, in order, and nil when there is none. Query returns only the first.
func (*Ctx) Redirect ¶
Redirect returns an error value. Returning it from Load, a method handler or OnRequest makes the server redirect.
func (*Ctx) SetBodyLimit ¶
SetBodyLimit changes the request body limit. Call it before anything reads the body, on the goroutine that handles the request, usually in OnRequest. Once reading has started it returns an error and the limit stays as it was. The body stays an http.MaxBytesReader, so ParseForm honours the limit instead of its own 10 MiB cap.
func (*Ctx) SetContext ¶
SetContext replaces the request context, for middleware that stores request-scoped values.
func (*Ctx) Writer ¶
func (c *Ctx) Writer() http.ResponseWriter
type Error ¶
Error is a failure that is safe to show to the client. The server answers it as is, without calling OnError.
type Fields ¶
Fields is a message per input field, by its json name, for a form to show next to each field. The server answers it with 422 without calling OnError, and an answer in JSON carries it as "fields". It is a separate type so that Error stays comparable.
type RedirectError ¶
RedirectError is returned by Ctx.Redirect. The server performs the redirect without calling OnError.
func (*RedirectError) Error ¶
func (e *RedirectError) Error() string