structpages

Documentation: https://jackielii.github.io/structpages/
Struct Pages provides a way to define routing using struct tags and methods. It integrates with Go's http.ServeMux, allowing you to quickly build web applications with minimal boilerplate.
Status: Beta - The API has settled and the library is battle-tested in production, carrying medium-to-large applications. Remaining breaking changes before v1 will be rare and called out in release notes.
Features
- Struct-based routing - Define routes using struct tags
- gsx-first - Examples and docs use gsx; any value with
Render(ctx, w) renders, so templ and html/template work too
- HTMX-friendly - With
WithTargetSelector(structpages.HTMXRenderTarget) (or HTMXv4RenderTarget), an htmx request renders just the component its HX-Target names
- Middleware - Standard Go middleware pattern
- Type-safe URLs - Generate URLs from struct references
- Dependency injection - Pass dependencies to handlers via options
Installation
go get github.com/jackielii/structpages
Quick Start
Define your page structure using struct tags, and render pages with gsx. In pages.gsx:
package main
type index struct {
product `route:"/product Product"`
team `route:"/team Team"`
}
type product struct{}
type team struct{}
// Page renders the index page. `|> url` resolves a page to its URL.
component (p index) Page() {
<html>
<body>
<h1>Welcome</h1>
<nav>
<a href={product{} |> url}>Product</a>
<a href={team{} |> url}>Team</a>
</nav>
</body>
</html>
}
component (p product) Page() {
<h1>Product</h1>
}
component (p team) Page() {
<h1>Team</h1>
}
In main.go:
package main
import (
"log"
"net/http"
"github.com/jackielii/structpages"
)
func main() {
mux := http.NewServeMux()
if _, err := structpages.Mount(mux, index{}, "/", "Home"); err != nil {
log.Fatal(err)
}
log.Println("Starting server on :8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}
The url filter comes from a gsx.toml at the module root that registers structpages' helpers:
[filters]
url = "github.com/jackielii/structpages.URLFor"
id = "github.com/jackielii/structpages.ID"
target = "github.com/jackielii/structpages.IDTarget"
key = "github.com/jackielii/structpages.Key"
keytarget = "github.com/jackielii/structpages.KeyTarget"
Generate and run with go tool gsx generate . && go run . (pin the CLI with
go get -tool github.com/gsxhq/gsx/cmd/gsx@latest). The Quick Start guide walks
through it step by step.
Documentation
The full docs live at https://jackielii.github.io/structpages/. Good starting points:
Examples
Runnable apps live in examples/; examples/README.md lists them in the order to read them.
Lint Tool