server

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package server provides a production-ready HTTP server with graceful shutdown, signal handling, and lifecycle hooks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Server)

Option configures a Server.

func WithAddr

func WithAddr(addr string) Option

WithAddr sets the listen address (default ":8080").

func WithIdleTimeout

func WithIdleTimeout(d time.Duration) Option

WithIdleTimeout sets the idle timeout (default 120s).

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the structured logger (default slog.Default()).

func WithReadTimeout

func WithReadTimeout(d time.Duration) Option

WithReadTimeout sets the read timeout (default 15s).

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

WithShutdownTimeout sets the graceful shutdown timeout (default 30s).

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) Option

WithWriteTimeout sets the write timeout (default 60s).

type Server

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

Server wraps http.Server with graceful shutdown and lifecycle hooks.

func New

func New(handler http.Handler, opts ...Option) *Server

New creates a new Server with the given handler and options.

func (*Server) OnShutdown

func (s *Server) OnShutdown(fn func(ctx context.Context) error)

OnShutdown registers a hook that runs after HTTP connections are drained. Errors are logged but all hooks still execute.

func (*Server) OnStart

func (s *Server) OnStart(fn func() error)

OnStart registers a hook that runs before the server starts listening. If any hook returns an error, Start aborts and returns the error.

func (*Server) Shutdown

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

Shutdown triggers a graceful shutdown of the server.

func (*Server) Start

func (s *Server) Start() error

Start runs the server and blocks until a shutdown signal (SIGINT/SIGTERM) is received or Shutdown is called programmatically.

Jump to

Keyboard shortcuts

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