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: Alpha - This package is in early development and may have breaking changes in the future. Currently used in a medium-sized project, but not yet battle-tested in production.
- 🏗️ Struct-based routing - Define routes using struct tags
- 🎨 Templ support - Built-in integration with Templ
- ⚡ HTMX-friendly - Automatic partial rendering support
- 🔧 Middleware - Standard Go middleware pattern
- 🎯 Type-safe URLs - Generate URLs from struct references
- 📦 Dependency injection - Pass dependencies to handlers via options
go get github.com/jackielii/structpagesDefine your page structure using struct tags:
package main
import (
"log"
"net/http"
"github.com/jackielii/structpages"
)
type index struct {
product `route:"/product Product"`
team `route:"/team Team"`
contact `route:"/contact Contact"`
}
// Implement the Page method using Templ
templ (index) Page() {
<html>
<body>
<h1>Welcome</h1>
<nav>
<a href="/product">Product</a>
<a href="/team">Team</a>
<a href="/contact">Contact</a>
</nav>
</body>
</html>
}
func main() {
mux := http.NewServeMux()
_, err := structpages.Mount(mux, index{}, "/", "Home")
if err != nil {
log.Fatal(err)
}
log.Println("Starting server on :8080")
http.ListenAndServe(":8080", mux)
}Route definitions use the format [method] path [Title]:
/path- All methods, no titlePOST /path- POST requests onlyGET /path Page Title- GET requests with title "Page Title"
- API Reference - Complete API documentation for Mount, options, and methods
- Routing Patterns - Route definitions, path parameters, nested routes
- Middleware - Using middleware with your pages
- HTMX Integration - Partial rendering and HTMX support
- URLFor - Type-safe URL generation
- Templ Patterns - Working with Templ templates
- Advanced Features - Dependency injection, error handling, and more
Check out the examples directory for complete working applications:
- Simple - Basic routing and page rendering
- HTMX - HTMX integration with partial updates
- Todo - Full todo application with database
See CONTRIBUTING.md for development setup and guidelines.
MIT License - see LICENSE file for details.