routes

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 12 Imported by: 0

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

View Source
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

View Source
var HTTPMethods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}

HTTPMethods are the endpoint handler names a route package may export.

Functions

func Build

func Build(segs []Segment, params map[string]string) (string, error)

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 Kind

type Kind int

Kind is the kind of a route segment.

const (
	// Static is literal text, such as "blog".
	Static Kind = iota
	// Param is a required parameter: "slug.param".
	Param
	// OptionalParam is a parameter that may be absent: "lang.optional".
	OptionalParam
	// Rest matches zero or more path segments: "path.rest".
	Rest
)

func (Kind) String

func (k Kind) String() string

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.

func Scan

func Scan(fsys fs.FS, root string) (*Manifest, error)

Scan reads the routes directory root in fsys and returns its manifest. All problems found are reported together, joined into one error.

type Matcher

type Matcher func(value string) bool

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

func (r *Route) HasActions() bool

HasActions reports whether the route's page has form actions.

func (*Route) HasLoad

func (r *Route) HasLoad() bool

HasLoad reports whether the route's page has a Load function.

func (*Route) IsEndpoint

func (r *Route) IsEndpoint() bool

IsEndpoint reports whether the route handles requests with HTTP method handlers (GET, POST, …).

func (*Route) Params

func (r *Route) Params() []Segment

Params returns the route's parameter segments in URL order.

func (*Route) Pattern

func (r *Route) Pattern() string

Pattern returns the route's URL pattern in bracket notation, e.g. "/blog/[slug]".

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router matches URL paths to routes.

func NewRouter

func NewRouter(routes []*Route, matchers map[string]Matcher) (*Router, error)

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

func (r *Router) Match(urlPath string) (*Route, map[string]string, bool)

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.

func ParseID

func ParseID(id string) ([]Segment, error)

ParseID returns the URL segments of a route ID such as "/app.group/blog/slug.param". It is the inverse of how Scan builds IDs.

func (Segment) Pattern

func (s Segment) Pattern() string

Pattern returns the segment in URL pattern notation: "blog", "[slug]", "[id=integer]", "[[lang]]" or "[...path]".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL