If a component earlier in the launcher sequence fails during OnInit or OnStart,
the launcher calls OnStop on every already-registered component for cleanup.
httpserver.OnStop previously called s.srv.Shutdown(ctx) unconditionally; because
s.srv is only assigned inside OnStart, any shutdown triggered before OnStart ran
caused a nil pointer panic.
Add an early return in OnStop: `if s.srv == nil { return nil }`.
3.1 KiB
3.1 KiB
Changelog
All notable changes to this module will be documented in this file.
The format is based on Keep a Changelog, and this module adheres to Semantic Versioning.
0.9.1 - 2026-03-21
Fixed
OnStopnow returnsnilimmediately whens.srv == nil, preventing a nil pointer panic when the launcher calls cleanup on a server component whoseOnStartwas never reached (e.g. because an earlier component failed during startup).
0.9.0 - 2026-03-18
Added
Loggerinterface — duck-typed; requiresInfo(msg string, args ...any)andError(msg string, err error, args ...any); satisfied directly bylogz.Loggerand by any two-method struct, with no import oflogzrequiredConfigstruct — holds HTTP server configuration with env-tag support:Host(SERVER_HOST, default0.0.0.0),Port(SERVER_PORT, default8080),ReadTimeout(SERVER_READ_TIMEOUT, default5s),WriteTimeout(SERVER_WRITE_TIMEOUT, default10s),IdleTimeout(SERVER_IDLE_TIMEOUT, default120s)Optionfunctional option type for configuring the server at construction timeWithMiddleware(mw ...func(http.Handler) http.Handler) Option— accumulates middleware applied to the root chi router duringOnInit; order is preserved and caller-controlled; multiple calls toWithMiddlewareappend to the listHttpServerComponentinterface — embeds bothlauncher.Componentandchi.Router, giving callers the complete chi routing API (Get,Post,Route,Mount,Use,With,Group, etc.) on the same value that participates in the launcher lifecycleNew(logger Logger, cfg Config, opts ...Option) HttpServerComponent— constructs the server component backed bychi.NewRouter(); no middleware is installed by defaultOnInitlifecycle method — applies all middleware registered viaWithMiddlewareto the root router; no-op if none were providedOnStartlifecycle method — constructs anhttp.Serverwith the configured timeouts and starts it in a background goroutine; logs the bind address on start and logs fatal errors ifListenAndServeexits unexpectedlyOnStoplifecycle method — callshttp.Server.Shutdownwith a 10-second context timeout, giving in-flight requests up to 10 seconds to complete before the method returns
Design Notes
HttpServerComponentembedschi.Routerdirectly in the interface rather than delegating through wrapper methods, so callers register routes and manage the lifecycle on the same value with no extra indirection- No middleware is installed by default; the full middleware stack is composed explicitly via
WithMiddlewareat construction time, keeping the stack visible and ordering unambiguous in the application source - chi was chosen as the underlying router because it uses stdlib
http.Handlerthroughout, making it fully compatible withhttpmwmiddleware andhttputilhandler adapters without any wrapper code at the boundary