Multi-Server Support
Run multiple HTTP listeners simultaneously using a single router instance for internal APIs, public endpoints, admin panels, or multi-port deployments.
The router package can serve multiple HTTP listeners simultaneously using a single router instance.
This allows the same routing tree, middleware pipeline, and application state to be shared across multiple ports or network interfaces.
Typical use cases include:
- public and internal APIs
- admin panels
- metrics endpoints
- multi-port deployments
- IPv4 and IPv6 listeners
- development and debugging interfaces
Starting Multiple Listeners
package main
import (
"fmt"
"log/slog"
"net/http"
"os"
"github.com/netlifeguru/logger"
"github.com/netlifeguru/router"
)
func main() {
r := router.New()
closer, err := logger.Init(logger.Config{
Dir: "./logs",
TerminalOutput: true,
DisableColors: false,
MinLevel: slog.LevelInfo,
ConsoleMinLevel: slog.LevelDebug,
MaxFileSize: 100 * 1024 * 1024,
MaxLogFiles: 10,
})
defer closer.Close()
if err != nil {
slog.Error("failed to initialize logger", "error", err)
os.Exit(1)
}
r.HandleFunc("/", "GET POST", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
serverHost := req.Host
response := fmt.Sprintf(`
<h1>Hello World</h1>
<p>Successfully connected!</p>
<p>Served by listener: <strong style="color: green;">%s</strong></p>
`, serverHost)
w.Write([]byte(response))
})
listeners := router.Listeners{
{Addr: "localhost:8000"},
{Addr: "localhost:8001"},
}
if err := r.MultiListenAndServe(listeners); err != nil {
slog.Error("failed to start server", "error", err)
os.Exit(1)
}
}
The router starts all configured listeners concurrently while sharing the same application routes and middleware.
Accessing Active Listeners
Open multiple endpoints in the browser:
http://localhost:8000
http://localhost:8001The response dynamically shows which listener handled the request.
Example:
<h1>Hello World</h1>
<p>Successfully connected!</p>
<p>Served by listener: <strong style="color: green;">localhost:8001</strong></p>Logging Integration
When request logging is enabled, each active listener is logged during startup.
Example terminal output:
2026-05-12 21:43:02 [INFO] Starting server
2026-05-12 21:43:02 [INFO] System resources cpu_cores=12
2026-05-12 21:43:02 [INFO] web server started server=NetLifeGuru version=v0.0.1 listen_addr=localhost:8000
2026-05-12 21:43:02 [INFO] web server started server=NetLifeGuru version=v0.0.1 listen_addr=localhost:8001This makes it easy to verify which interfaces and ports are currently active.
Common Deployment Patterns
Multi-server setups are useful when applications expose different traffic types on separate listeners.
Examples:
- public API on
:443 - internal admin API on
127.0.0.1:9000 - metrics endpoint on
:9090 - profiling server on
127.0.0.1:6060
Example:
listeners := router.Listeners{
{Addr: ":443"},
{Addr: "127.0.0.1:9000"},
{Addr: ":9090"},
}All listeners share:
- the same router instance
- middleware stack
- route tree
- application state
Notes
Each listener runs in its own HTTP server internally.
The router manages listener startup and graceful shutdown automatically while keeping routing behavior consistent across all active listeners.