Method handlers
POST and other method handlers
Method handlers with input and result
A method handler may take an input after the Ctx and return a result before the error. The framework decodes the input and writes the result:
func POST(c *tifa.Ctx) error // writes its own response
func POST(c *tifa.Ctx, in ShipInput) error // answers 204 on success
func POST(c *tifa.Ctx) (Shipment, error) // answers 200 with the result as JSON
func POST(c *tifa.Ctx, in ShipInput) (Shipment, error)
- The input is a struct, not a pointer to one. POST, PUT and PATCH decode it from the request body: JSON, or a URL-encoded or multipart form. A form fills only fields that are strings, numbers, bools or slices of them, and the files (see Files in the input), and a form value for any other field is answered with 400. A form decodes each value by its kind and never calls
MarshalTextorUnmarshalText, so a type with one of them, or a slice of such types, is such another field, and so is a slice type with one of its own, such astype Tags []string, which TypeScript takes for one string. GET and DELETE decode it from the query string, so its fields may only be strings, numbers, bools or slices of them, by the same rule, with no embedded fields, and noMarshalTextorUnmarshalTexton the input type itself. The build checks this, andc.BindQueryreturns an error for any other field whatever the query holds. - Fields are named by their
jsontags, in a form body and the query string too. Aformtag that names a field otherwise than itsjsontag fails the build, also in a struct passed toc.Bindorc.BindQuery. One that says the same (json:"email" form:"email", as a type shared with another framework has) is fine and changes nothing. Path parameters, headers and cookies still come fromc. - A constant name in
c.Paramthat the path of the folder does not have fails the build, in the handler and in the module functions it passes the Ctx to:c.Param("id")in thePOSToforder+/reads"", so writec.Param("order"). - The input and the result follow the JSON rules, the same as the result of
Load. The input must also read back from the JSON its TypeScript type describes: a type that is a string because ofMarshalTextneedsUnmarshalText([]byte) errortoo, a type withUnmarshalTextmust be a string in TypeScript unless it is a map key (a key is always a JSON string), andUnmarshalJSONis not accepted (except ontime.Time). - When the input type has a method
Validate(it must befunc() errororfunc(c *tifa.Ctx) error), it runs after decoding and before the handler.nilgoes on. With the Ctx,Validatecan read the request:c.Language()for a message in the visitor's language (see Validation messages),c.Param,c.Query,c.QueryValues,c.Header,c.Cookie,c.Path(),c.Context(),c.Request(), andtifa.Session. It checks and answers with its error only, so the build fails whenValidate, or a function it passes the Ctx to, writes the response (c.JSON,c.Text,c.NoContent,c.SetHeader,c.SetCookie,c.Writer), reads the body again (c.Bind,c.BindQuery,c.SetBodyLimit,c.SetReadTimeout), callstifa.SetSessionortifa.ClearSession, or callstifa.Use. The Ctx may go where it may inLoad(see How Load may use Ctx). Atifa.Errorortifa.Fieldsis answered as is, so returntifa.Fieldsto mark single fields. Collect them in atifa.Fields{}and end withreturn fields.Err(), which is nil when nothing was added (an emptytifa.Fieldsis still an error). Any other error is an error of the app, like one the handler returns: it goes toOnError, is answered with 500 when there is none, and the user never sees its text (see What an error you return becomes). - A POST, PUT, PATCH or DELETE that a browser sends from another site, a form or multipart included, is answered with 403 before the handler runs, so a handler needs no check of its own for it.
server.trusted_originsandserver.cross_site_pathslet chosen origins and paths through (see Cross-site writes). - A body or query string that does not decode is answered with 400 (413 for a body over the limit, 415 for another content type or none, or for JSON to an input that holds files, 408 for a body that does not arrive in time), and the handler does not run.
- A handler that writes its own response decodes input with
c.Bind(&in)for the body andc.BindQuery(&in)for the query string. These name a field by itsjsontag, then its Go name, as a typed input does. Aformtag with another name in a struct passed to them fails the build, as it does for a typed input (a value the build cannot see the type of, such as one behind an interface, is not checked, and the tag is ignored).c.Bindfills*multipart.FileHeaderand[]*multipart.FileHeaderfields from a multipart form too.
Files in the input
A field of the input of a POST, PUT or PATCH handler of type *multipart.FileHeader or []*multipart.FileHeader holds files of an upload, and so does a named type whose underlying type is one of them, such as type Photos []*multipart.FileHeader. The generated function then sends the input as a multipart form, so a page uploads a file the way it sends any other input:
package photos
type UploadInput struct {
Title string `json:"title"`
Photo *multipart.FileHeader `json:"photo"`
Extras []*multipart.FileHeader `json:"extras"`
}
func (in UploadInput) Validate() error {
if in.Photo == nil {
return tifa.Fields{"photo": "Choose a picture."}
}
return nil
}
func POST(c *tifa.Ctx, in UploadInput) (Photo, error) {
f, err := in.Photo.Open()
if err != nil {
return Photo{}, err
}
defer f.Close()
// check the bytes, copy the file where it is kept
}
const file = form.get('photo') // a File from <input type="file">
await POST({ input: { title, photo: file instanceof File ? file : null, extras: [] } })
- In TypeScript a
*multipart.FileHeaderisBlob | nulland a[]*multipart.FileHeaderisBlob[]. AFileis aBlob, and its name goes with it. A field withomitemptyoromitzerois optional. *multipart.FileHeadergets the first file of its name and staysnilwhen there is none. The slice gets every file of the name,nilfor none. An empty text value of the name, which a browser sends for a file input left empty, counts as no file. A text value with something in it is answered with 400.- Such an input comes only as a multipart form or a URL-encoded one (which can hold no file): a JSON body is answered with 415. Its other fields come as text, so they follow the rules of a GET input: strings, numbers, bools or slices of them, no embedded fields, and no
MarshalTextorUnmarshalTexton the input type, on a field or on the element of a field. - A file can only be a field of the input itself. A file in a GET or DELETE input, in a nested struct, a map or an array, in a result, in the data of
Loador aLayout, or in the session fails the build. So does a file type with aMarshalTextorUnmarshalTextmethod, which TypeScript would take for a string. - The name, the size and the
Content-Typeof a file (in.Photo.Header.Get("Content-Type")) are what the client sent. Decide what a file is from its bytes, and make the name you keep it under yourself, see Uploads. - The form is parsed with the budget of Go's
mime/multipart: up to 32 MiB of file content stays in memory, sharing about 42 MiB with the text fields and the names and headers of the parts, and a file that does not fit goes to a temporary file, which Tifa removes when the request ends. The peak can be higher than the budget: a file that ends on disk is first read into a growing buffer of up to 32 MiB, so one large file can take about 64 MiB for a moment, and each upload at the same time takes its own. On a machine with little memory, let only a few uploads parse at once inOnRequest, see Uploads. Copy what you keep before the handler returns, and do not hand the*multipart.FileHeaderto a goroutine that outlives the request.
Body limit and read time of a route
Every request body is limited to server.body_limit, and every request has server.read_timeout to send it. A route that takes uploads needs more of both. Set them in OnRequest, for the requests that need them, before anything reads the body. Match the method too: a GET of a page has no body to wait for.
// hooks.go
func OnRequest(c *tifa.Ctx, next func(*tifa.Ctx) error) error {
if c.Pattern() == "/photos" && c.Request().Method == http.MethodPost {
if err := c.SetBodyLimit(100 << 20); err != nil {
return err
}
if err := c.SetReadTimeout(5 * time.Minute); err != nil {
return err
}
}
return next(c)
}
c.SetBodyLimit(n)is a number of bytes. A body over it is answered with 413 before the handler runs. A handler without an input that readsc.Request().Bodyitself gets an*http.MaxBytesErrorfrom the read instead: answer it withtifa.Error{Status: 413, Message: "The file is too large."}, or it is an unexpected error and goes toOnError. The limit counts the whole body: the files, the other fields and the multipart framing. Take the number from an option of the app if it should change without a build:c.SetBodyLimit(int64(UploadLimit))withUploadLimit = tifa.Size("upload.limit", 4<<20, "Largest upload").c.SetReadTimeout(d)gives the bodydfrom now to arrive, and the answerserver.write_timeoutafter it, since it is written once the body is in. A body still coming when the time is over is answered with 408. A request with no body, such as a GET, keeps its deadlines and the call does nothing.- Both return an error once the body is being read, and
SetReadTimeoutalso whendis not over 0 or when the response cannot reach the connection, such as behind a middleware oftifa.SetHTTPHandlerthat does not unwrap it. Check the error and return it.
HEAD, OPTIONS and TRACE
These three take the Ctx alone, func HEAD(c *tifa.Ctx) error, and write their own answer with c.SetHeader, c.NoContent, c.Text or c.Writer(). tifa routes lists them with the others, and route.gen has no function for them: a page has no use for them, and fetch refuses TRACE.
- HEAD. You rarely need one: without it,
GETanswers HEAD with the same headers and no body, and a page answers it as it renders. Write one when the headers are cheap and the body is not, such as a download whose size,ETagandLast-Modifiedcome from a database row while the file sits elsewhere, or when the path answers HEAD alone. Send the headers the GET would send, since a client or a cache takes them for those of the GET. A route withHEADand noGETanswers GET with 405. A folder withpage.tsxcannot haveHEAD. - OPTIONS. Without one, Tifa answers OPTIONS with 204 and an
Allowheader that lists the methods of the path: those ofroute.go,GETandHEADfor a page or aGET, andOPTIONS.OnRequestruns first, as for any request. Write your own to say more thanAllowdoes, such as theDAVheader of a WebDAV path. The preflight of a call from another site belongs inOnRequest, not here, since its CORS headers go on every answer of the routes it opens: see Calls from another site. - TRACE. Sends the request back, for finding out what a proxy on the way changed. A handler that writes the request headers into its answer writes back the cookies and
Authorizationof the request too.
CONNECT
CONNECT asks for a tunnel: after the answer, the connection carries whatever the two ends send. It works on the connection, not on a route, and a client asking a proxy sends a host and port in place of a path (CONNECT db.internal:5432), which no route could match. So route.go has no CONNECT: a function of that name that takes the Ctx fails the build. Take it in the wrapper of tifa.SetHTTPHandler (see Setting up in OnStart), which sees every request before routing:
func OnStart(ctx context.Context) error {
tifa.SetHTTPHandler(func(app http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodConnect {
app.ServeHTTP(w, r)
return
}
// CONNECT db.internal:5432 has r.Host "db.internal:5432" and no r.URL.Path.
conn, rw, err := http.NewResponseController(w).Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rw.WriteString("HTTP/1.1 200 Connection established\r\n\r\n")
rw.Flush()
tunnel(conn, rw.Reader, r.Host) // yours: the connection is now the tunnel's
})
})
return nil
}
The wrapper is plain net/http: OnRequest and the cross-site check do not run, so check who may open a tunnel yourself. Hijack works over HTTP/1.1. Over HTTP/2 a CONNECT is one stream of a shared connection and Hijack returns an error, and Go turns extended CONNECT off by default. Without a wrapper, a CONNECT for a host and port gets 404, and one with the path of a route gets 405 and its Allow, which never lists CONNECT.
Where a write goes
A folder has one handler for each method, so a write is a method and a folder. Three shapes cover what apps need:
| The write | Where it goes | Such as |
|---|---|---|
| Changes fields of a thing | The PATCH of the folder of the thing |
PATCH /orders/42 with the fields to change |
| Makes a new thing below another | The POST of a folder for those things |
POST /orders/42/notes |
| An operation with a name of its own | A folder with that name, and its POST |
POST /orders/42/ship |
-
One
PATCHchanges any of the fields. A field of the input withomitemptyoromitzerois optional in TypeScript, and a pointer field tells a field that was left out (nil) from an empty one. -
An operation gets a folder when it has rules of its own: who may do it, when it is allowed, or what else it sets off. Shipping an order is allowed once, changes the status of the order and may send a mail, so it is
ship/, not one more field of thePATCH. -
The folder is small. It is a Go package with a
route.goof a few lines that calls the domain:// app/orders/order+/ship/route.go package ship type Input struct { Carrier string `json:"carrier"` TrackingNo string `json:"trackingNo"` } func POST(c *tifa.Ctx, in Input) (orders.Order, error) { return orders.Ship(c.Context(), c.Param("order"), in.Carrier, in.TrackingNo) }The page imports its function under a name that says what it does:
import { POST as shipOrder } from './ship/route.gen'.
Each write then has an address, an input type and a result type of its own. OnRequest can decide who may do it from the method and c.Pattern(), and a proxy, a rate limit and the log tell the writes apart by method and path alone.
Tifa does not check any of this. A handler may also take an action field in its input and do one of several things, but those then share one address, one input type and one result type. Why Tifa has no functions that the browser calls by name is in Philosophy.
Calling method handlers from the browser
The route.gen module of a folder with method handlers exports one function per handler, named after its method. API routes get a route.gen module too, so a page can import the handlers of any folder:
import { POST } from './route.gen'
import { TifaError } from 'tifa/client'
async function ship() {
try {
const shipment = await POST({ params: { project, order }, input: { carrier: 'SF' } })
} catch (e) {
if (e instanceof TifaError) setErrors(e.fields) // e.status, e.message, e.fields
}
}
- The function takes one object.
paramsholds the path parameters and is left out when the folder has none. Aname++folder's key is_splat. A value that would send the request to another path is refused before any request and rejects the promise: an empty value,.or..for aname+parameter, and a.or..segment or an empty segment before the end for_splat. An empty_splatis fine.inputis the handler's input and is left out when it takes none.signalaborts the request. - GET and DELETE send the input in the query string, the other methods as a JSON body, or as a multipart form when the input holds files (see Files in the input). The result is typed from the Go result, with
| undefinedfor a redirect (below):func(c, in) (Out, error)givesPromise<POSTOutput | undefined>.func(c, in) errorgivesvoid.func(c) errorwrites its own response, so its function gives back theResponseas it is, also for an error status. - A call only sends its request and, when the answer carries a session, applies it (see below). It never reloads the data of a page, on success or on failure, for any method. When the page should show what the write changed, call
router.invalidate()after it (see the example below). - Tifa never reloads page data for you. After a write, a handler function or a
fetchof your own, the page shows what it showed before until you ask: call TanStack'srouter.invalidate()to load the data of the current page and all its Layouts again, merged into one request.await router.invalidate({ sync: true })resolves once the page shows the new data. A plainawait router.invalidate()resolves at once and the page updates in the background. - An error answer throws
TifaErrorwithstatus,messageandfields(the messages of atifa.Fields, empty otherwise). A network failure throws asfetchdoes. A call from a page of an older build than the server fails with status 409, see Pages open while you deploy. - When the handler or
OnRequestreturnstifa.Redirect(url, status), the browser goes to that URL: through the router for a URL of this site, without reloading the page, and as a whole page load for another site. The new page shows what the router has for it and loads what is missing, as after any navigation. A redirect to the page that is showing keeps the page and loads its data and the data of its Layouts again, asrouter.invalidate()does, with no new history entry, as a browser does when a form without script is redirected to the address it is on. It counts as the same page when the path, the hash and the query values match, whatever the order and encoding of the query. The promise resolves once the navigation has finished, toundefined, also for a handler with a result. Whether a call is redirected is only known from its answer, sinceOnRequestmay redirect any request, so the result type of every handler function with a result includesundefined. The function of a handler that writes its own response gets theResponseof the redirect (ok, with the status inX-Tifa-Redirect). A relative URL is resolved against the URL of the handler, not of the page. A 3xx the handler wrote itself throughc.Writer(), or one a proxy wrote, cannot be read in the browser and rejects the promise with anError. - A call that signs someone in or out brings the new session in its answer, and the copy of the session in the browser follows (see Sessions in the browser).
- Call these functions from event handlers or effects. Calling one while the page renders on the server throws.
route.genalso exportsPOSTInput,POSTOutputand so on for the types of each handler that has them.
The error that tifa.Redirect returns is a *tifa.RedirectError with the fields URL and Status. In OnRequest or a test, recognize it with errors.As.
To go to the page of what a handler created, return a redirect from the handler, or return the result and navigate yourself. A call reloads nothing, so the page you leave is not loaded again just before. A write the page itself shows stays on screen after the call. Ask for it with router.invalidate():
func POST(c *tifa.Ctx, in AddInput) error {
todo, err := AddTodo(in.Title)
if err != nil {
return err
}
return tifa.Redirect("/todos/"+todo.ID, http.StatusSeeOther)
}
const router = useRouter() // from @tanstack/react-router
// in the event handler, where POST returns (Todo, error):
const todo = await POST({ input: { title } })
if (todo) await router.navigate({ to: '/todos/$todo', params: { todo: todo.id } }) // undefined: redirected