Server

NewServer, the transport options, and the Start and Stop lifecycle.

Server wraps a Router in a net/http.Server and exposes a Name, Start, Stop lifecycle a service registry can drive.

NewServer

func NewServer(cfg Config, router *Router, logger Logger, opts ...Option) *Server

Wires a server around the router. Pass a github.com/toaweme/log logger directly, or a null logger to discard output. Options tune the underlying *http.Server. Reach for HTTP to set fields no option covers.

Config

type Config struct {
	Host	string
	Port	int
}

Holds only the listen address. Everything else is set with an option or by mutating the raw server.

Lifecycle

func (s *Server) Start() error

Logs the routes, then serves until Stop is called. It blocks and returns nil on a clean shutdown.

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

Gracefully shuts the server down, draining in-flight requests until ctx's deadline passes.

func (s *Server) Name() string

Identifies the service in a registry.

func (s *Server) HTTP() *http.Server

Returns the underlying *http.Server for fields no option covers, such as TLS config or connection hooks. Mutate it before calling Start.

Options

Each option mutates the underlying *http.Server during construction. Options run after the defaults, so they override.

func WithReadHeaderTimeout(d time.Duration) Option

Sets how long the server waits for request headers. Pass 0 to disable it, which re-exposes the server to slow-header connections.

func WithReadTimeout(d time.Duration) Option

Sets the maximum duration for reading an entire request.

func WithWriteTimeout(d time.Duration) Option

Sets the maximum duration before timing out response writes.

func WithIdleTimeout(d time.Duration) Option

Sets how long a keep-alive connection may stay idle.