httpsrv

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package httpsrv provides an HTTP server integrated with BAT application lifecycle. It wraps net/http.Server with graceful shutdown support and context decoration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(rs []Route, mdw ...Middleware) http.Handler

Build composes a slice of Routes into an http.Handler, optionally wrapped with middleware.

func Mount

func Mount(mux Mux, prefix string, sub http.Handler)

Mount registers a sub-handler onto the mux at the given prefix, stripping the prefix from requests.

Types

type Config

type Config struct {
	Addr                         string
	Listener                     func() (net.Listener, error)
	DisableGeneralOptionsHandler bool
	ReadTimeout                  string
	ReadHeaderTimeout            string
	WriteTimeout                 string
	IdleTimeout                  string
	MaxHeaderBytes               int
	// contains filtered or unexported fields
}

func (Config) BuildServer

func (cfg Config) BuildServer(h http.Handler) (*http.Server, error)

BuildServer creates an http.Server from this config with the given handler.

func (Config) New

func (cfg Config) New(h http.Handler, cd []bat.CtxDecorator) (*Service, error)

New creates a Service from a pre-built http.Handler. Use this when you provide your own handler — a third-party router, a manually constructed mux, or the result of Build.

func (Config) NewFromRoutes

func (cfg Config) NewFromRoutes(rs []Route, mdws []Middleware, cd []bat.CtxDecorator) (*Service, error)

NewFromRoutes collects Routes and Middlewares injected by the DI container, assembles them via Build, and creates the Service. Pass it directly to bat.Provide: bat.Provide(cfg.NewFromRoutes)

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware is a standard HTTP middleware. Register multiple middlewares for DI-assembled servers via bat.SupplyGroup[Middleware].

type Mux

type Mux = interface {
	Handle(string, http.Handler)
}

type Route

type Route interface {
	Register(mux Mux)
}

Route is implemented by anything that can register itself onto a Mux. Use Endpoint for a single pattern, Group to compose multiple Routes.

func Endpoint

func Endpoint(p string, h http.Handler, mdw ...Middleware) Route

func Group

func Group(rs ...Route) Route

Group returns a Route that registers all given routes on the same Mux.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service wraps an http.Server with BAT lifecycle integration.

func New

func New(hs *http.Server, cd []bat.CtxDecorator) *Service

New creates a Service from a pre-built http.Server and optional context decorators.

func (*Service) Init

func (s *Service) Init(bg bat.BackgroundContext, app bat.PhaseScheduler)

Init registers the service into the BAT lifecycle. It wires Serve at PhaseStart, Shutdown at PhaseShutdown, and Stop at PhaseStop.

func (*Service) Serve

func (s *Service) Serve(bg context.Context) error

Serve starts the HTTP server. It blocks until the server stops. If no listener or address is configured, it listens on a random localhost port.

func (*Service) Shutdown

func (s *Service) Shutdown(ctx context.Context)

Shutdown gracefully drains the server, waiting for in-flight requests to complete.

func (*Service) Stop

func (s *Service) Stop(ctx context.Context) error

Stop forcibly closes the server's listeners without waiting for in-flight requests.

Directories

Path Synopsis
Package batwing provides a development dashboard for BAT applications.
Package batwing provides a development dashboard for BAT applications.

Jump to

Keyboard shortcuts

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