httpserver

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 8 Imported by: 0

README

httpserver

Lifecycle-managed HTTP server built on chi that integrates with the launcher component model.

Features

  • Implements launcher.Component (OnInit / OnStart / OnStop)
  • Embeds chi.Router — full routing API exposed directly on the component
  • Graceful shutdown with configurable timeouts
  • No middleware installed by default — compose your stack with WithMiddleware

Installation

require code.nochebuena.dev/go/httpserver v0.1.0

Usage

srv := httpserver.New(logger, cfg,
    httpserver.WithMiddleware(
        httpmw.Recover(),
        httpmw.CORS([]string{"*"}),
        httpmw.RequestID(uuid.NewString),
        httpmw.RequestLogger(logger),
    ),
)

// Register routes directly on srv — it is a chi.Router.
srv.Get("/health", health.NewHandler(logger, db, cache))

srv.Route("/api/v1", func(r chi.Router) {
    r.Use(httpauth.AuthMiddleware(firebaseClient, nil))
    r.Get("/orders/{id}", httputil.Handle(validator, svc.GetOrder))
})

lc := launcher.New(logger)
lc.Append(db, cache, srv)
if err := lc.Run(); err != nil {
    log.Fatal(err)
}

Config

Field Env var Default Description
Host SERVER_HOST 0.0.0.0 Bind address
Port SERVER_PORT 8080 Listen port
ReadTimeout SERVER_READ_TIMEOUT 5s Max time to read request
WriteTimeout SERVER_WRITE_TIMEOUT 10s Max time to write response
IdleTimeout SERVER_IDLE_TIMEOUT 120s Keep-alive idle timeout

Options

Option Description
WithMiddleware(mw ...func(http.Handler) http.Handler) Applies middleware to the root router during OnInit

Lifecycle

Phase Action
OnInit Applies registered middleware to the router
OnStart Starts net/http.Server in a background goroutine
OnStop Graceful shutdown — waits up to 10s for in-flight requests

Documentation

Overview

Package httpserver provides a lifecycle-managed HTTP server built on chi.

It implements launcher.Component so it integrates directly with the launcher lifecycle (OnInit → OnStart → OnStop). It also embeds chi.Router, giving callers the full chi routing API: Get, Post, Route, Mount, Use, etc.

No middleware is installed by default. Compose your stack with WithMiddleware:

srv := httpserver.New(logger, cfg,
    httpserver.WithMiddleware(
        httpmw.Recover(),
        httpmw.CORS([]string{"*"}),
        httpmw.RequestID(uuid.NewString),
        httpmw.RequestLogger(logger),
    ),
)
srv.Get("/health", healthHandler)
srv.Route("/api/v1", func(r chi.Router) { ... })

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host         string        `env:"SERVER_HOST"          envDefault:"0.0.0.0"`
	Port         int           `env:"SERVER_PORT"          envDefault:"8080"`
	ReadTimeout  time.Duration `env:"SERVER_READ_TIMEOUT"  envDefault:"5s"`
	WriteTimeout time.Duration `env:"SERVER_WRITE_TIMEOUT" envDefault:"10s"`
	IdleTimeout  time.Duration `env:"SERVER_IDLE_TIMEOUT"  envDefault:"120s"`
}

Config holds the HTTP server configuration.

type HttpServerComponent

type HttpServerComponent interface {
	launcher.Component
	chi.Router
}

HttpServerComponent is a launcher.Component that also exposes a chi.Router. Embedding chi.Router gives callers the full routing API: Get, Post, Route, Mount, Use, etc.

func New

func New(logger Logger, cfg Config, opts ...Option) HttpServerComponent

New creates an HttpServerComponent. No middleware is installed by default; use WithMiddleware to compose the middleware stack.

type Logger

type Logger interface {
	Info(msg string, args ...any)
	Error(msg string, err error, args ...any)
}

Logger is the minimal interface httpserver needs — satisfied by logz.Logger.

type Option

type Option func(*serverOpts)

Option configures an httpserver component.

func WithMiddleware

func WithMiddleware(mw ...func(http.Handler) http.Handler) Option

WithMiddleware applies one or more middleware to the root chi router during OnInit.

Jump to

Keyboard shortcuts

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