Skip to content

jackielii/structpages

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

structpages

CI Go Reference codecov Go Report Card License: MIT

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.

Features

  • 🏗️ 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

Installation

go get github.com/jackielii/structpages

Quick Start

Define 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 title
  • POST /path - POST requests only
  • GET /path Page Title - GET requests with title "Page Title"

Documentation

Examples

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

Contributing

See CONTRIBUTING.md for development setup and guidelines.

License

MIT License - see LICENSE file for details.

About

Build Templ pages with Struct-based Routing

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •