httpserver

package module
v0.3.21 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 8 Imported by: 0

README

Documentation

Overview

Package httpserver provides a small HTTP server with context-driven graceful-shutdown lifecycle management.

It owns the start/stop machinery — listen, context-cancellation shutdown, and the start/shutdown error contract — and nothing else: the caller supplies the http.Handler and wires the cancellation (typically via signal.NotifyContext), passing the resulting context to Serve. It holds no CLI or orchestration logic and is reusable by any service that needs to serve HTTP and stop cleanly.

Index

Constants

View Source
const (
	// ErrServerStart indicates the server could not start listening.
	ErrServerStart errs.Const = "failed to start server"
	// ErrServerShutdown indicates the server did not shut down within the deadline.
	ErrServerShutdown errs.Const = "failed to shutdown server"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Host

type Host string

Host is the address the server binds to.

type Port

type Port int

Port is the TCP port the server listens on.

type Server

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

Server wraps an *http.Server with lifecycle management. It is a value type: the non-copyable server machinery lives behind the http pointer field, so copies share the same underlying server.

func New

func New(logger *slog.Logger, host Host, port Port, handler http.Handler) Server

New builds a Server bound to host:port serving the supplied handler.

func (Server) Serve

func (s Server) Serve(ctx context.Context, timeout time.Duration) error

Serve starts the server and blocks until ctx is cancelled or startup fails, then shuts down gracefully within timeout. Request contexts derive from ctx (via http.Server.BaseContext), so they observe the same lifecycle cancellation. When ctx is cancelled, a pending startup failure is preferred over reporting a clean shutdown, so a real error is never masked.

Jump to

Keyboard shortcuts

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