NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

About
LoggingHealth ChecksProfilingMulti-Server SupportStatic FilesRate LimitingPanic RecoveryCustom 404 HandlerCustom 404 PageError Handling
RouterFeatures

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:8001

The 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:8001

This 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.

Profiling

Enable Go pprof profiling endpoints for runtime performance analysis, memory inspection, and debugging.

Static Files

Serve CSS, JavaScript, images, and favicon files from a local directory using a URL prefix.

On this page

Starting Multiple ListenersAccessing Active ListenersLogging IntegrationCommon Deployment PatternsNotes