server package provides utilities for managing multiple servers (HTTP, gRPC) with graceful shutdown and multiplexing capabilities.
Sub-packages
Mux
Multiplexed server management for HTTP and gRPC
SPA
Single-page application serving utilities
Installation
Mux Package
Themux package allows you to run multiple protocol servers (HTTP, gRPC) concurrently with coordinated lifecycle management and graceful shutdown.
Features
- Multi-Protocol Support: Run HTTP and gRPC servers simultaneously
- Graceful Shutdown: Coordinated shutdown with configurable grace period
- Context-Based Control: Use context cancellation for shutdown
- Error Handling: Proper error propagation from all servers
Core Function
Quick Start
Options
WithHTTPTarget
WithGRPCTarget
WithGracePeriod
Complete Example: HTTP + gRPC Gateway
Graceful Shutdown Behavior
When the context is cancelled (e.g., by SIGTERM):- All servers stop accepting new connections
- Existing connections are allowed to complete within the grace period
- After the grace period, servers are forcefully stopped
- Any errors during shutdown are logged
SPA Package
Thespa package provides utilities for serving Single-Page Applications with proper fallback handling.
Features
- SPA Routing: Serves index.html for all non-file routes
- Static Asset Caching: Proper cache headers for static files
- Development Support: Auto-reload in development mode
SPAHandler
Complete Example: API + SPA
Advanced Patterns
Multiple HTTP Servers
HTTP + gRPC + Metrics
Best Practices
Use Signal Handling
Use Signal Handling
Always use signal handling for graceful shutdown:
Configure Timeouts
Configure Timeouts
Set appropriate timeouts for HTTP servers:
Separate Admin Endpoints
Separate Admin Endpoints
Run admin/metrics endpoints on a separate port:
Log Server Status
Log Server Status
Log when servers start and stop: