Documentation
¶
Overview ¶
Package ROUTER provides a sanely configured router.
Index ¶
- Constants
- func CreateRateLimiter(cfg *config.RateLimiter) *rate.Limiter
- func Limit(limiter *rate.Limiter, next http.Handler) http.Handler
- func NewRouter(cfg *config.Config, b Gatherer) http.Handler
- type Backbone
- func (b *Backbone) AddBackbone(*Backbone)
- func (b *Backbone) GetBackbone() *Backbone
- func (b *Backbone) GetEndpoints() []Endpoint
- func (b *Backbone) GetLogger() *slog.Logger
- func (b *Backbone) PingDB(health *Health)
- func (b *Backbone) ServerError(w http.ResponseWriter, r *http.Request, err error)
- func (b *Backbone) Write(p []byte) (n int, err error)
- type Endpoint
- type Gatherer
- type Health
- type Option
Constants ¶
const (
StatusClientClosed = 499
)
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 NewRouter ¶
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 ¶
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 ¶
AddBackbone is an awkward method to satisfy the Gatherer interface.
func (*Backbone) GetBackbone ¶
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 ¶
GetEndpoints is a convenient method to add a list of HTTP methods and http.Handlers to a router.
func (*Backbone) GetLogger ¶
GetLogger is a convoluted method on the Backbone struct that fulfills the Gatherer interface.
func (*Backbone) ServerError ¶ added in v0.45.5
ServerError logs an error, then produces a HTTP response appropriate for common errors.
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 ¶
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.
type Option ¶
type Option func(*Backbone)
Option allows us to selectively add items to the Backbone struct.
func WithDbHandle ¶
WithDbHandle selectively adds a Postgres connection pool to the Backbone struct.
func WithLogger ¶
WithLogger selectively adds a structured logger to the Backbone struct.