router

package
v0.54.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package ROUTER provides a sanely configured router.

Index

Constants

View Source
const (
	StatusClientClosed = 499
)
View Source
const TIMEOUT_PING = time.Millisecond * 299

Variables

This section is empty.

Functions

func CreateRateLimiter added in v0.41.5

func CreateRateLimiter(cfg *config.RateLimiter) *rate.Limiter

func Limit added in v0.41.5

func Limit(limiter *rate.Limiter, next http.Handler) http.Handler

func NewRouter

func NewRouter(cfg *config.Config, b Gatherer) http.Handler

NewRouter accepts the any struct that conforms to the Gatherer interface. So it accepts the library Backbone struct, and any downstream executable struct that wraps around a Backbone. Ideally, dependencies are smuggled into the router, and this interface allows for the easy creation of a server in both production and testing.

Types

type Backbone

type Backbone struct {
	Cache        *redis.Client
	DbHandle     *pgxpool.Pool
	Logger       *slog.Logger
	HeapSnapshot *bytes.Buffer
}

Backbone holds dependencies that can eventually be accessed by a http.Handler.

func NewBackbone

func NewBackbone(options ...Option) *Backbone

NewBackbone employs the Options pattern to selectively configure the Backbone struct. It also adds a Health record and initializes it. It also adds a buffer for receiving runtime profile data.

func (*Backbone) AddBackbone

func (b *Backbone) AddBackbone(*Backbone)

AddBackbone is an awkward method to satisfy the Gatherer interface.

func (*Backbone) GetBackbone

func (b *Backbone) GetBackbone() *Backbone

GetBackbone is an awkward, convoluted method on the Backbone struct to summon itself. This awkwardly satisfies the Gatherer interface, and using an interface allows me to pass a dependency-wrapper around both a library and a downstream executable, and for production and for testing.

func (*Backbone) GetEndpoints

func (b *Backbone) GetEndpoints() []Endpoint

GetEndpoints is a convenient method to add a list of HTTP methods and http.Handlers to a router.

func (*Backbone) GetLogger

func (b *Backbone) GetLogger() *slog.Logger

GetLogger is a convoluted method on the Backbone struct that fulfills the Gatherer interface.

func (*Backbone) PingDB

func (b *Backbone) PingDB(health *Health)

Ping evaluates the ability to contact a Postgres server

func (*Backbone) ServerError added in v0.45.5

func (b *Backbone) ServerError(w http.ResponseWriter, r *http.Request, err error)

ServerError logs an error, then produces a HTTP response appropriate for common errors.

func (*Backbone) Write

func (b *Backbone) Write(p []byte) (n int, err error)

Write is a method on the Backbone struct designed to conform to an interface, so that the Backbone can easily write runtime information to its Buffer field.

type Endpoint

type Endpoint struct {
	VerbAndPath string
	Handler     http.HandlerFunc
}

Endpoint is a custom struct that can be used to create a menu of routes. Ideally, viewing a populated Endpoint in a file is easy on the eyes during code review.

type Gatherer

type Gatherer interface {
	GetLogger() *slog.Logger
	GetBackbone() *Backbone
	AddBackbone(*Backbone)
	GetEndpoints() []Endpoint
}

Gatherer is an ugly, contrived interface that this library's configured router expects. So it is adopted by the native Backbone struct. It can also be adopted by a struct that wraps around Backbone in a downstream executable. Any Gatherer can be passed to the function that creates a new router. This enabled creating a custom test server in the library that downstream developers can easily use. Using an interface allows me to pass a dependency-wrapper around both a library and a downstream executable, and for production and testing.

type Health

type Health struct {
	Rdbms    bool
	Heap     bool
	Routines bool
}

Health offers summarized data that can be read on the /health endpoint. No matter how often an external service hammers the /health endpoint, the application will easily read a boolean to reply. The real work of evaluating the health of the application and dependencies is left to background goroutines.

func (*Health) PassFail

func (h *Health) PassFail() bool

PassFail evaluates the totality of dependencies and the application.

type Option

type Option func(*Backbone)

Option allows us to selectively add items to the Backbone struct.

func WithCache added in v0.40.5

func WithCache(client *redis.Client) Option

WithCache selectively adds a Redis client to the Backbone struct.

func WithDbHandle

func WithDbHandle(dbHandle *pgxpool.Pool) Option

WithDbHandle selectively adds a Postgres connection pool to the Backbone struct.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger selectively adds a structured logger to the Backbone struct.

Jump to

Keyboard shortcuts

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