Documentation
¶
Overview ¶
Package routes discovers Tern routes on the filesystem and matches URLs against them.
Every directory under the routes root is a URL segment. Svelte files starting with "+" define the UI, and the Go package in the directory, if any, defines the server side:
+page.svelte page component
+layout.svelte layout wrapping this directory and its children
+error.svelte error boundary
*.go Go package; exports Load and Actions (page),
LayoutLoad (layout) and GET, POST, … (endpoint)
Go import paths cannot contain brackets or parentheses, so dynamic segments use suffixes: slug.param, id.param.integer, lang.optional, path.rest and app.group.
Index ¶
Constants ¶
const ( FilePage = "+page.svelte" FileLayout = "+layout.svelte" FileError = "+error.svelte" // FilePageMarkdown is a page written in Markdown; tern gen renders it // into a Svelte component. FilePageMarkdown = "+page.md" )
Svelte route file names.
Variables ¶
var HTTPMethods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
HTTPMethods are the endpoint handler names a route package may export.
Functions ¶
func Build ¶
Build returns the URL path for a route's segments with the given parameters filled in, escaping each value. Optional parameters may be omitted; a rest parameter may contain slashes.
func Sort ¶
func Sort(routes []*Route)
Sort orders routes by matching priority, most specific first. Segments are compared left to right: static text beats the end of a route, which beats mixed segments ("[slug].json"), then parameters, then optional parameters, then rest parameters. Parameters with a matcher beat those without. Ties are broken by pattern, then ID, so the order is deterministic.
Types ¶
type GoPackage ¶
type GoPackage struct {
Dir string
Name string // package name
Files []string // non-test .go files
// Load reports whether the package exports a page Load function.
Load bool
// Actions reports whether the package exports a page Actions variable.
Actions bool
// LayoutLoad reports whether the package exports a layout load function.
LayoutLoad bool
// Methods lists the exported endpoint handlers, in HTTPMethods order.
Methods []string
}
GoPackage describes the Go package in a route directory.
type Layout ¶
type Layout struct {
Dir string
// Component is the +layout.svelte file, or "".
Component string
// Go is the directory's Go package if it exports LayoutLoad, or nil.
Go *GoPackage
}
Layout is a directory that has a +layout.svelte, a LayoutLoad, or both.
type Manifest ¶
type Manifest struct {
// Root is the routes directory that was scanned.
Root string
// Routes, in matching priority order.
Routes []*Route
}
Manifest is the result of scanning a routes directory.
type Matcher ¶
Matcher reports whether a parameter value is acceptable, e.g. whether "42" is an integer for [id=integer].
type Route ¶
type Route struct {
// ID identifies the route by its directory, including groups,
// e.g. "/app.group/blog/slug.param". The root route's ID is "/".
ID string
// Dir is the route's directory, or "" for the root.
Dir string
// Segments are the URL segments, with groups removed.
Segments []Segment
// Page is the +page.svelte file, or "".
Page string
// Go is the route directory's Go package, or nil.
Go *GoPackage
// Layouts that wrap the page, outermost first.
Layouts []Layout
// ErrorPages are the +error.svelte files that apply, outermost first.
ErrorPages []string
}
Route is a URL pattern backed by a page, an endpoint, or both. All file paths are slash-separated and relative to the routes root.
func (*Route) HasActions ¶
HasActions reports whether the route's page has form actions.
func (*Route) IsEndpoint ¶
IsEndpoint reports whether the route handles requests with HTTP method handlers (GET, POST, …).
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router matches URL paths to routes.
func NewRouter ¶
NewRouter compiles routes for matching. Every matcher a route refers to must be present in matchers. The routes are matched in the order given; use Sort (or a Manifest's order) to match by priority.
func (*Router) Match ¶
Match finds the first route matching urlPath, which should be the escaped path of a request URL (url.URL.EscapedPath). It returns the route and its decoded parameter values. Absent optional parameters are left out of the map; an empty rest parameter is present with the value "".
A trailing slash is ignored: "/about/" matches the same route as "/about".
type Segment ¶
type Segment struct {
// Raw is the directory name, e.g. "slug.param".
Raw string
Kind Kind
// Value is the literal text for Static segments and the parameter name otherwise.
Value string
// Matcher is the optional matcher name, from "id.param.integer".
Matcher string
}
Segment is one URL path segment, parsed from a route directory name.