Routes and Ctx
How to write the Go side of a route in route.go
route.go
package order
func Load(c *tifa.Ctx) (orders.Order, error) {
p, err := tifa.Use(c, project.Layout)
if err != nil {
return orders.Order{}, err
}
return orders.GetOrder(p.ID, c.Param("order"))
}
type ShipInput struct {
Carrier string `json:"carrier"`
}
func POST(c *tifa.Ctx, in ShipInput) (orders.Shipment, error) {
return orders.ShipOrder(c.Param("project"), c.Param("order"), in.Carrier)
}
Loadreturns the page data. Its return type must convert to TypeScript (see "JSON rules" below).GET,POST,PUT,PATCHandDELETEhandle the HTTP method they are named after.Layoutreturns the data of the Layout of the folder (see Layout).- A folder with a
page.tsxis a page. The framework renders its GET, so you cannot also writeGET. A page that needs no data and no other method can bepage.tsxalone, withoutroute.go. - A folder with
route.goand nopage.tsxis an API route when it declares a method handler: every method handler answers as written, andLoadis not allowed, becauseLoadloads the data of a page. Aroute.gowith only aLayoutis no API route. It gives the pages below it their Layout data. - Everything is package-level functions and constants. Use the global variables the app has set up for dependencies. The framework does no dependency injection.
- The names of the switches (
SSR,Cache,Memo) are read as written, so a misspelled one would be ignored. The build fails on a bool constant inroute.gonamedcache,memoorssrin lower case, and on an exported constant that is one or two letters off a switch (Cahce,Memoo) and has a type that switch takes: a bool forSSRandMemo, a bool or atime.DurationforCache. The message names the switch it probably meant. Constants of other types, such asconst Demo = "demo", short names such asIDorMax, and names such asModethat are two edits from a four-letter switch, are left alone.
When an error is returned:
| Returned | Result |
|---|---|
tifa.ErrNotFound |
404 |
tifa.Redirect(url, status) |
A redirect. Page loads and navigation follow it, and so does a handler function called from the browser (see Calling method handlers from the browser). Panics when status is not 3xx |
tifa.Error{Status, Message} |
Answered with its status code and message, does not go to OnError. A status outside 400 to 599 is handled as an unexpected error |
tifa.Fields{"carrier": "pick a carrier"} |
422 with a message per input field by its json name, does not go to OnError |
| Any other error, a panic | Goes to OnError. Without one, it is logged and answered with 500 |
A method handler that fails answers JSON: {"error": "message"}, and for tifa.Fields also "fields": {"carrier": "pick a carrier"}.
In Load you can only set response headers and cookies. Calling c.JSON, c.Text or c.NoContent returns an error.
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 fails the build, also in a struct passed toc.Bindorc.BindQuery. Path parameters, headers and cookies still come fromc. - The input and the result follow the JSON rules below, 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 answered with 400 and itserr.Error()as the message, which the user sees. - 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 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. 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.
Ctx
*tifa.Ctx is the request and its response. Load, Layout, the method handlers, OnRequest and OnError get it.
| Method | What it does |
|---|---|
c.Param(name) |
A path parameter, decoded, such as c.Param("order") for the folder order+ |
c.Query(name) |
The first value of a query parameter, "" when absent |
c.QueryValues(name) |
Every value of a query parameter, such as ["a", "b"] for ?tag=a&tag=b, nil when absent |
c.Header(name), c.Cookie(name) |
A request header, a request cookie (http.ErrNoCookie when absent) |
c.Context(), c.WithContext(ctx) |
The request context, and a copy of c that carries another one, to store request-scoped values. c itself does not change |
c.Pattern() |
The path of the matched route, such as /projects/{project}, "" when no route matched |
c.Path() |
The path of the page as the visitor sees it in the address bar: escaped, with the language prefix, without the query, such as /en-US/store/items/a%20b. The same for a page load and for the data request of a navigation. In a method handler it is the path of the handler. Not allowed in a Layout, see How Load may use Ctx |
c.Language() |
The language of the route being answered: the language in the URL, the default language when the URL has none, "" on a site without languages. See The language in code |
c.SetAlternates(list) |
Replaces the versions of the page in other languages, for hreflang and the sitemap. Call it in a Load. See What search engines get |
c.IsDataRequest() |
Whether this is a navigation data request of the browser router rather than a page load |
c.SetHeader(key, value), c.SetCookie(cookie) |
A response header, a response cookie. Allowed in Load too |
c.JSON(code, v), c.Text(code, s), c.NoContent(code) |
Writes the response. Not allowed in Load |
c.Bind(&v), c.BindQuery(&v) |
Decodes the body or the query string into a struct, see above. Not allowed in Load |
c.SetBodyLimit(n) |
Changes the body limit of this request (server.body_limit by default, see Body limit and read time of a route). Call it before anything reads the body, usually in OnRequest. Once reading has started it returns an error |
c.SetReadTimeout(d) |
Gives the body of this request d to arrive, in place of server.read_timeout, and the answer server.write_timeout after it. Call it before anything reads the body, usually in OnRequest. Once reading has started it returns an error |
c.Request(), c.Writer() |
The raw *http.Request and http.ResponseWriter, for method handlers and OnRequest only |
How Load may use Ctx
This section covers Load, Layout and the functions they call. At build time, Tifa starts from each Load and Layout function and finds the route parameters (c.Param), query parameters (c.Query and c.QueryValues) and Layouts (tifa.Use) it reads. When Load passes *tifa.Ctx to a function in this module, Tifa follows the call level by level, across packages too, and counts every parameter it finds toward that Load. A helper called by several Load functions counts toward each of them. Tifa does not follow into its own functions.
So that Tifa finds everything, Ctx may only be used where Tifa can see it. Pass c.Param, c.Query and c.QueryValues a constant string, such as c.Param("order"), and call them and tifa.Use directly: storing one in a variable or passing it as a value fails the build.
The only types that may hold a Ctx are: *tifa.Ctx, tifa.Ctx, non-generic structs declared in this module (whose fields hold Ctx only through these types, directly, embedded or indirectly), and pointers to such structs.
A value that holds a Ctx may only appear in these five places:
- As the receiver of a direct method call, such as
c.Query("tab")orreq.Page(). - As an argument to a non-generic function from this module or from Tifa, where the parameter has exactly the same type. A function literal called in place also counts.
- In a composite literal of a struct that holds Ctx, for a field of exactly the same type.
- Assigned with
:=orvar x T = ...to a new local variable of exactly the same type. That variable follows the same rules. - With its address taken, where the resulting pointer is still a type that may hold Ctx.
Any other use fails the build and names the file and line. For example: converting it to another type or an interface, assigning it to an existing variable, assigning several values at once, returning it, putting it in a slice, map, array or channel, range, package-level variables, defer, go statements, or passing it to a generic function, an interface method, a function variable or a function outside the module (the standard library and third-party libraries).
Load, Layout and the functions they call must not call c.Request() or c.Writer(). Tifa cannot see parameters read through the raw request. Use c.Param, c.Query, c.QueryValues, c.Header, c.Cookie or c.Context() instead. c.Request() and c.Writer() are only for method handlers and OnRequest.
c.Path() is the way to get the path of the page in Load, such as for a next address in a redirect to a sign-in page. It is the path the visitor asked for, with the language prefix and without the query, for a page load and for the data request of a navigation alike, so it is no reason to call c.Request(). A Load that needs a query parameter in that address reads it with c.Query("name"). A Layout and the functions it calls must not call c.Path(): the data of a Layout is shared by every page below it and is not loaded again when the visitor goes from one of them to another, so it would keep the path of the page that loaded it first. Call it in the Load of the page, in OnRequest, or in a method handler.
They must not call c.BindQuery or c.Bind either. Tifa cannot name the query parameters a struct decodes, so the page would not load again when they change, and a page load or the data request of a navigation has no body. Read each query parameter with c.Query("name"), or c.QueryValues("name") for a repeated one.
When you hit one of these errors, call Ctx methods directly, or pass it to a plain function. If you only need the request context, pass c.Context(). A helper that only calls c.Context() (such as auth.User(c)) collects nothing when Tifa follows it.
Tifa only checks code that Load and Layout can reach. If you store a Ctx in a global variable or another shared place from OnRequest, a method handler or anywhere else, and then take it out in Load or Layout, Tifa cannot catch it. Do not do this.
JSON rules
The value Load or a Layout function returns, and the input and result of a method handler, travel as JSON, and Tifa generates TypeScript types from them. Tifa writes and reads this JSON with encoding/json/v2 and its defaults, so the build accepts only the types below, which json v2 writes without an error and in a form the TypeScript type describes. c.JSON, c.Bind and the session use the same rules.
| Go | TypeScript |
|---|---|
bool |
boolean |
integers, floats, json.Number |
number |
string, time.Time |
string |
tifa.HTML |
HTML of tifa/client, see Showing HTML the app trusts. Not a map key, a handler input, in the session or a whole result |
*multipart.FileHeader, []*multipart.FileHeader |
Blob | null, Blob[]. Only in a field of the input of a POST, PUT or PATCH handler, see Files in the input |
| pointer | T | null |
| slice | T[]. A nil slice is written as [] |
[]byte, [N]byte (the element is exactly byte or uint8) |
string, written as base64. A nil []byte is "". A slice or array of a named byte type is an ordinary array of numbers |
| array | T[], or [] when the length is 0 |
| map | Record<string, V>. A nil map is written as {} |
| struct | an object of its exported fields |
- Map keys are written in sorted order, so the same value always gives the same text. They must be string or integer types, or types that output a string under the
MarshalTextrule below. Interfaces and pointers cannot be keys. - An exported embedded struct (a pointer works too) without a json name has its fields promoted to the outer object. Fields reached through an embedded pointer are optional. When fields of different structs have the same name, the shallowest wins, then the one with a json tag, and if that still leaves several, all of them are dropped. An embedded field needs a json name when its type is not a struct, and an embedded struct may not have json tag options or a method named
MarshalJSON,MarshalJSONTo,MarshalText,AppendText,UnmarshalJSON,UnmarshalJSONFromorUnmarshalText. Unexported embedded fields are not accepted unless taggedjson:"-". - Two fields of one struct may not have the same JSON name. An unexported field may not have a json tag other than
json:"-", and a struct that has fields needs an exported one or a json tag. - A json tag name may only use letters, digits, spaces and
!#$%&()*+-./:;<=>?@[]^_{|}~. The only allowed options areomitempty,omitzeroandstring. Fields withomitemptyoromitzeroare optional.omitzeroleaves out the zero value of the Go type.omitemptyleaves out a value whose JSON is empty:null,"",[]or{}, so it never leaves outfalseor0. Thestringoption is only allowed on fields of a predeclared number type, or on unnamed pointers to them, and writes the number as a string. Thestringoption on anint64oruint64field is how to send a big integer: it is typedstringand written as a JSON string. See the next point. - A browser, and the server render, read every JSON number as a double, so an integer is safe only up to 2^53 - 1 (9007199254740991). Integers are typed
number, and Go writes every digit, so a value such as a snowflake ID, a hash ortime.UnixNano()would arrive rounded, without any error. Tifa refuses to send it instead: the request fails as an unexpected error (OnError, a 500, the dev page andtifa errors, in development and in production), and the message names theLoad,Layout, handler or session, the path of the number (rows[3].id) and the number as written and as a browser reads it. It checks the JSON written, so it does not matter how the Go type is spelled, and afloat64is never refused. The two ways out: addjson:",string"to the field (typedstring), or keep the value within ±9007199254740991 (for a time, send milliseconds). Only what a page reads is checked: the results ofLoadandLayout, the session data ofExposed, and the result of a handler when the call comes from the generated function of a page. A plain HTTP client (curl, a mobile app) gets the raw number from a handler, and so doestifatest.Startwithout the headerX-Tifa-Call: 1, so a test that posts to a handler must set that header to see the refusal.LoadandLayoutare checked undertifatest.Starttoo. What a handler writes withc.JSONand the answer to a client that does not sendX-Tifa-Call, such as a mobile app, are sent as written.tifa build,tifa checkandtifa devwarn, with the file and line, where code puts a number above 2^53 straight into such a field: a constant,time.Time.UnixNano,Int63,Int64,Uint64ofmath/rand, aSum64hash, orbinary.Uint64of bytes fromcrypto/rand, in a composite literal or an assignment to a field, also through a conversion or one local variable. Nothing else is guessed. time.Durationis not accepted, also as a map key, since json v2 has no form for it. Return an integer of the unit you mean, such as milliseconds, or a string.- If a type's value or pointer has a method or field named
MarshalJSON,MarshalJSONTo,AppendTextorAppendJSON, it is rejected whatever the signature (excepttime.Timeandjson.Number). Rename it, or return a different type without these methods. - If a type itself declares a method with exactly the signature
MarshalText() ([]byte, error), on the value or the pointer receiver, it is written as a string. A wrong signature, a method promoted from an embedded field, or a method hidden by a field of the same name is always rejected. - Interfaces are not accepted anywhere, including fields, elements and map keys. Neither are chan, func, complex numbers,
unsafe.Pointeror type parameters. <,>and&are written as they are, also in the data of a page. Tifa escapes them where it puts the data into a page, so no text of the app can end a script element.- A string with bytes that are not UTF-8 is written with U+FFFD for each of them, as the browser would show it, and does not fail the page.
- Input is read strictly: names must match the json name exactly, also in case, a name may appear once in an object, strings must be UTF-8,
time.Timemust be RFC 3339, base64 may not hold line breaks, and an array must have the length of the Go array. Each mistake answers 400. Names the type does not have are skipped. A session record that does not read back this way signs the visitor out.