router

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintRouteTable

func PrintRouteTable(routes []Route)

PrintRouteTable prints Zyra's startup route table to stdout, e.g.:

[Zyra Router] Registered 3 routes:
  GET  /              -> pages/index.tsx
  GET  /about         -> pages/about.tsx
  GET  /blog/:slug    -> pages/blog/[slug].tsx

func RegisterPageRoutes

func RegisterPageRoutes(mux *http.ServeMux, routes []Route, handler PageHandler)

RegisterPageRoutes registers a single catch-all handler on mux that dispatches every incoming request to handler based on the first route in routes matching the request path (see MatchRoute), and prints Zyra's startup route table via PrintRouteTable. Requests that match no route receive a 404.

Types

type PageHandler

type PageHandler func(w http.ResponseWriter, r *http.Request, route *Route, params map[string]string)

PageHandler is invoked for an incoming request that matched a registered page Route, with the params extracted from the URL.

type Route

type Route struct {
	// Pattern is the URL pattern for this route, e.g. "/blog/:slug" or
	// "/*" for a catch-all.
	Pattern string
	// FilePath is the path to the .tsx/.jsx source file that
	// implements this route.
	FilePath string
	// Params lists the dynamic parameter names declared by the route,
	// in the order they appear in the file path. For a catch-all
	// route, this holds the single wildcard parameter name.
	Params []string
	// IsCatchAll is true for catch-all routes such as
	// pages/[...all].tsx.
	IsCatchAll bool
}

Route describes a single file-based page route discovered by ScanPages.

func MatchRoute

func MatchRoute(routes []Route, urlPath string) (*Route, map[string]string, bool)

MatchRoute finds the first route in routes whose pattern matches urlPath, returning the matched route, its extracted parameters, and whether a match was found. Since ScanPages sorts static routes before dynamic routes before catch-alls, exact (static) matches take priority over dynamic ones, and dynamic ones over catch-alls, as long as routes is sorted that way (as ScanPages guarantees).

func ScanPages

func ScanPages(pagesDir string) ([]Route, error)

ScanPages recursively walks pagesDir and converts every .tsx/.jsx file it finds into a Route using Zyra's file-based routing convention:

pages/index.tsx          -> /
pages/about.tsx          -> /about
pages/blog/index.tsx     -> /blog
pages/blog/[slug].tsx    -> /blog/:slug
pages/[id]/edit.tsx      -> /:id/edit
pages/[...all].tsx       -> /* (catch-all)

The returned slice is sorted so static routes come first, dynamic routes next, and catch-all routes last, which is what gives static and dynamic routes priority over catch-alls in MatchRoute. If pagesDir does not exist, ScanPages returns an empty slice and no error.

Jump to

Keyboard shortcuts

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