server

package
v2.679.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 10 Imported by: 0

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:

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

func NewServer(server *http.Server, cfg *config.Config) (*Server, error)

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

func (s *Server) Serve() error

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

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully stops the underlying server.

The provided context controls shutdown deadlines/cancellation.

func (*Server) String

func (s *Server) String() string

String returns the listener address as a string (used for logging/attribution).

type Service

type Service = server.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.

Jump to

Keyboard shortcuts

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