Documentation
¶
Overview ¶
Package server provides HTTP server adapters and lifecycle wiring for go-service.
This package contains helpers used to run an internal HTTP server consistently within an Fx/Dig application. It bridges between:
- a configured `*net/http.Server` (from github.com/alexfalkowski/go-service/v2/net/http),
- a bind address (via `net.Listen`), and
- the transport-agnostic server lifecycle manager in github.com/alexfalkowski/go-service/v2/net/server.
Listener and address semantics ¶
NewServer accepts either the go-service network address convention "<network>://<address>" (for example "tcp://:8080") or a raw listen address such as ":8080". Raw addresses use the "tcp" network. It creates a listener using `net.Listen` with a background context; the resulting listener is held by the returned Server and is used for [Serve]/`ServeTLS`.
TLS semantics ¶
If `cfg.TLS` is non-nil, the Server assigns it to the underlying http.Server.TLSConfig and serves TLS using `ServeTLS` with empty cert/key paths (because certificates are expected to be provided via TLSConfig). If `cfg.TLS` is nil, the Server serves plain HTTP.
Error normalization ¶
Server.Serve wraps the underlying serve error using github.com/alexfalkowski/go-service/v2/net/http/errors.ServerError so callers can treat expected shutdown errors consistently.
Service wiring ¶
NewService constructs a managed `*net/server.Service` that starts the HTTP server asynchronously, logs lifecycle events, and triggers application shutdown if the server terminates unexpectedly.
Start with NewService and NewServer.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server adapts a *http.Server to the github.com/alexfalkowski/go-service/v2/net/server.Server interface used by go-service.
It holds:
- the underlying HTTP server,
- an optional TLS config, and
- the listener created during construction.
func NewServer ¶
NewServer constructs an HTTP Server adapter that owns a listener and serves HTTP (optionally with TLS).
Address parsing: The cfg.Address is expected to use the go-service network address convention "<network>://<address>" (for example "tcp://:8080"). It is split using net.SplitNetworkAddress and then passed to net.Listen.
Listener lifecycle: NewServer creates and stores the listener immediately. The listener is used by Serve/ServeTLS. If listener creation fails, NewServer returns nil and the listener error.
TLS behavior: If cfg.TLS is non-nil, Serve will set the underlying http.Server.TLSConfig and call ServeTLS with empty cert/key file paths (because certificate material is expected to be present in TLSConfig). If cfg.TLS is nil, Serve will call Serve and serve plain HTTP.
Address formats: cfg.Address may use either the go-service "<network>://<address>" convention or a raw listen address such as ":8080". Raw addresses default to the "tcp" network.
func (*Server) Serve ¶
Serve starts serving requests on the configured listener.
Serve normalizes expected shutdown errors via github.com/alexfalkowski/go-service/v2/net/http/errors.ServerError so callers can treat graceful termination consistently.
func (*Server) Shutdown ¶
Shutdown gracefully stops the underlying server, then force-closes active connections if ctx ends first.
It first stops accepting new connections and waits for active requests to finish. If ctx is canceled or reaches its deadline first, Shutdown calls http.Server.Close to unblock the remaining requests and returns ctx.Err().
type Service ¶
Service is an alias for server.Service.
func NewService ¶
func NewService(name string, http *http.Server, cfg *config.Config, logger *logger.Logger, sh di.Shutdowner) (*Service, error)
NewService constructs a managed Service for an HTTP server.
It adapts the provided *http.Server into this package's Server (see NewServer), then wraps it with the transport-agnostic lifecycle manager in net/server.
The returned service will:
- start serving asynchronously (when [Service.Start] is called by upstream wiring),
- log start/stop events when a logger is provided, and
- trigger application shutdown via the provided di.Shutdowner if serving terminates unexpectedly.
Errors returned are from listener creation performed by NewServer.