staticroutes

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package staticroutes parses and evaluates the in-repo routing rules of a static site: Netlify style _redirects and _headers files, the app.yaml routes and headers blocks, and the SPA, clean URL and 404 options.

Index

Constants

View Source
const (
	EnvMaxRules       = spec.EnvStaticMaxRules
	EnvMaxHeaderRules = spec.EnvStaticMaxHeaderBlocks
	EnvMaxFileBytes   = "APP_STATIC_ROUTES_MAX_FILE_BYTES"
	EnvMaxValueLen    = "APP_STATIC_ROUTES_MAX_VALUE_LEN"
)

Environment variables that tune the caps below. Every one is optional.

View Source
const (
	KindServe    = "serve"
	KindRedirect = "redirect"
	KindNotFound = "not_found"
)

Decision kinds.

View Source
const (
	SourceRedirects = spec.StaticSourceRedirects
	SourceHeaders   = spec.StaticSourceHeaders
	SourceSpec      = spec.StaticSourceSpec
)

Rule and header sources, shown next to each parsed rule.

View Source
const (
	SeverityError   = spec.StaticSeverityError
	SeverityWarning = spec.StaticSeverityWarning
)

Problem severities.

View Source
const (
	SlashOff    = spec.StaticSlashOff
	SlashAdd    = spec.StaticSlashAdd
	SlashRemove = spec.StaticSlashRemove
)

Trailing slash modes for Options.TrailingSlash.

View Source
const SidecarSuffix = spec.StaticSidecarSuffix

SidecarSuffix names the compiled rules file stored beside a published tree.

Variables

This section is empty.

Functions

func ParseHeaders

func ParseHeaders(src string, l Limits) ([]HeaderBlock, []Problem)

ParseHeaders parses Netlify _headers syntax; bad lines are reported, never applied.

func ParseRedirects

func ParseRedirects(src string, l Limits) ([]Rule, []Problem)

ParseRedirects parses Netlify _redirects syntax; bad lines are reported, never applied.

func SidecarPath

func SidecarPath(rootDir string) string

SidecarPath is where the ruleset of the tree at rootDir is stored.

func ValidateHeaderBlock

func ValidateHeaderBlock(b *HeaderBlock, l Limits) error

ValidateHeaderBlock checks a block built from app.yaml and prepares it.

func ValidateRule

func ValidateRule(r *Rule, l Limits) error

ValidateRule checks a rule and prepares its matcher. It is the single gate for file rules and app.yaml rules alike.

func WriteSidecar

func WriteSidecar(rootDir string, rs *Ruleset) error

WriteSidecar stores rs beside the tree at rootDir, replacing any earlier one.

Types

type Decision

type Decision struct {
	Kind     string     `json:"kind"`
	Status   int        `json:"status"`
	Location string     `json:"location,omitempty"`
	File     string     `json:"file,omitempty"`
	Rule     string     `json:"rule,omitempty"`
	Headers  []HeaderKV `json:"headers"`
	Remove   []string   `json:"remove_headers"`
	Trace    []string   `json:"trace"`
}

Decision is the outcome for one request. File is relative to the site root. Trace lists, in order, every step considered.

type HeaderBlock

type HeaderBlock = spec.StaticHeaderBlock

HeaderBlock applies header changes to matching paths.

type HeaderKV

type HeaderKV = spec.StaticHeaderKV

HeaderKV is one response header.

type Input

type Input = spec.StaticInput

Input is what a deploy hands to Compile.

type Limits

type Limits = spec.StaticLimits

Limits caps the size of a site's routing rules.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits are the caps used when no environment override is set.

func LimitsFromEnv

func LimitsFromEnv(lookup func(string) (string, bool)) Limits

LimitsFromEnv applies any valid positive override found through lookup.

type Options

type Options = spec.StaticServeOptions

Options are the static serving switches.

type Problem

type Problem = spec.StaticProblem

Problem is one validation finding. Line is 0 for spec rules.

type QueryMatch

type QueryMatch = spec.StaticQueryMatch

QueryMatch requires a query parameter.

type Request

type Request struct {
	Method   string
	Path     string
	RawQuery string
}

Request is the part of an HTTP request resolution looks at. Path is the decoded URL path.

type Rule

type Rule = spec.StaticRule

Rule is one redirect, rewrite or status rule.

type Ruleset

type Ruleset struct {
	spec.StaticRuleset
}

Ruleset is everything compiled for one deploy; Resolve evaluates it.

func Compile

func Compile(in Input, l Limits) (*Ruleset, error)

Compile validates every source and returns the ruleset.

func LoadSidecar

func LoadSidecar(rootDir string) (*Ruleset, error)

LoadSidecar reads the ruleset of the tree at rootDir; none yields nil without an error.

func (*Ruleset) Prepare

func (rs *Ruleset) Prepare() error

Prepare compiles the matchers of a ruleset read back from JSON.

func (*Ruleset) Resolve

func (rs *Ruleset) Resolve(fsys StatFS, req Request) Decision

Resolve applies the documented precedence: explicit rules in order (the first that matches, and, unless forced, only when no file is at the path), then files (exact, clean URL, directory index), then the SPA fallback, then the 404 page.

type StatFS

type StatFS interface {
	Stat(name string) (fs.FileInfo, error)
}

StatFS is the one file system call resolution needs; *os.Root satisfies it.

Jump to

Keyboard shortcuts

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