router

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package router provides domain-based reverse proxy, automatic TLS via Let's Encrypt, request logging, and server lifecycle management.

router.Proxy("app.example.com", "myapp:5000")
router.Listen(router.WithLogger(), security.New(), application.New(views))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowDomain

func AllowDomain(host string) error

AllowDomain validates DNS and adds a domain to the TLS whitelist. Returns an error if the domain doesn't resolve.

func AllowDomainUnchecked

func AllowDomainUnchecked(host string)

AllowDomainUnchecked adds a domain without DNS validation. Use for system domains loaded from config/env at startup.

func ClientIP

func ClientIP(r *http.Request) string

ClientIP extracts the client IP from a request, respecting X-Forwarded-For.

func DisallowDomain

func DisallowDomain(host string)

DisallowDomain removes a domain from the TLS whitelist.

func Handler

func Handler(fallback http.Handler) http.Handler

Handler returns an http.Handler that does domain-based proxy routing. Requests matching a Proxy() entry are reverse-proxied to the target. All other requests fall through to the provided handler.

func Listen

func Listen(middleware ...func(http.Handler) http.Handler)

Listen is the declarative entry point. It composes the middleware chain and starts the server. The last middleware is typically the application. Blocks until shutdown signal (SIGINT/SIGTERM).

router.Listen(
    router.WithLogger(),
    security.New(security.WithNonce(), security.WithHeaders()),
    application.New(views, ...),
)

func Proxies

func Proxies() map[string]string

Proxies returns a copy of the current proxy table.

func Proxy

func Proxy(host, target string)

Proxy maps a domain to a backend target (e.g. "myapp:5000"). Requests with this Host header are reverse-proxied to the target.

func RemoveProxy

func RemoveProxy(host string)

RemoveProxy removes a domain proxy.

func ServerIP

func ServerIP() string

ServerIP returns this server's public IP (cached after first call).

func SetCertDir

func SetCertDir(dir string)

SetCertDir sets the directory for autocert certificate storage. Must be called before Listen.

func WithLogger

func WithLogger() func(http.Handler) http.Handler

WithLogger returns middleware that logs all HTTP requests. Production (ENV=production): structured JSON. Development: human-readable.

Types

This section is empty.