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
Route HandlersParameterized RoutesWildcard RoutesRoute GroupsMiddlewareGroup MiddlewareMounting Standard HandlersRequest Context
RouterCore Concepts

Middleware

Use built-in and custom middleware for logging, request IDs, real client IP detection, CORS, compression, content validation, caching, and request normalization.

Middleware allows you to run logic before or after a route handler.

It is commonly used for:

  • request logging
  • request IDs
  • real client IP detection
  • CORS
  • compression
  • cache headers
  • content validation
  • rate limiting
  • custom application logic

Middleware uses the standard router middleware signature:

type Middleware func(router.HandlerFunc) router.HandlerFunc

Registering Middleware

Register global middleware with r.Use.

r.Use(router.Logger())
r.Use(router.RequestID())
r.Use(router.RealIP())

Global middleware applies to all routes registered on the router.


Built-in Middleware Overview

Middleware / HelperTypeDescription
UseRegistrationRegisters global middleware on the router
WithRegistrationCreates a scoped middleware group for selected routes
Group.UseRegistrationRegisters middleware only for a route group
UseDefaultsPresetRegisters GetHead, RequestID, RealIP, and NoCache
LoggerMiddlewareLogs request method, host, path, query, request ID, and duration
RequestIDMiddlewareAdds a request ID to the request context and response header
RequestIDWithGeneratorMiddlewareAdds a request ID using a custom ID generator
RequestIDFromContextHelperReads the request ID from context.Context
RealIPMiddlewareResolves the real client IP and stores it in X-Real-IP
SetTrustedProxiesHelperConfigures trusted proxy CIDR ranges for real IP detection
ClientIPHelperResolves the client IP from trusted proxy headers or remote address
CORSMiddlewareHandles Cross-Origin Resource Sharing and preflight requests
NoCacheMiddlewareAdds headers that prevent browser and proxy caching
CompressMiddlewareApplies gzip compression for selected content types
DefaultCompressMiddlewareApplies gzip compression for common text and JSON content types
AllowContentTypeMiddlewareRestricts requests based on the Content-Type header
ContentCharsetMiddlewareValidates accepted request character sets
GetHeadMiddlewareTreats HEAD requests as GET requests for handlers
CleanPathMiddlewareNormalizes the request path before passing it to the handler
RateLimitMiddlewareLimits request frequency per client and route

Example

package main

import (
	"compress/gzip"
	"log/slog"
	"net/http"
	"os"

	"github.com/netlifeguru/router"
)

func main() {
	r := router.New()

	r.Use(router.Logger())
	r.Use(router.RequestID())

	if err := router.SetTrustedProxies([]string{"10.0.0.0/8"}); err != nil {
		slog.Error("failed to set trusted proxies", "error", err)
	}

	r.Use(router.RealIP())
	r.Use(router.GetHead())
	r.Use(router.CleanPath())

	r.Use(router.CORS(router.CORSOptions{
		AllowedOrigins:   []string{"https://*", "http://*"},
		AllowedMethods:   []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowedHeaders:   []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
		ExposedHeaders:   []string{"Link"},
		AllowCredentials: false,
		MaxAge:           300,
	}))

	r.Use(router.NoCache())

	r.Use(router.Compress(
		gzip.DefaultCompression,
		"text/html",
		"text/plain",
		"text/css",
		"application/javascript",
		"text/javascript",
		"application/json",
	))

	r.Use(router.AllowContentType("application/json", "text/xml"))
	r.Use(router.ContentCharset("UTF-8", "Latin-1", ""))

	r.Use(func(next router.HandlerFunc) router.HandlerFunc {
		return func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
			next(w, req, ctx)
		}
	})

	r.GET("/", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(http.StatusCreated)
		w.Write([]byte(`Hello World`))
	})

	if err := r.ListenAndServe(":8000"); err != nil {
		slog.Error("failed to start server", "error", err)
		os.Exit(1)
	}
}

Default Middleware

Use UseDefaults to register a small default middleware set.

r.UseDefaults()

This registers:

  • GetHead
  • RequestID
  • RealIP
  • NoCache

Use this when you want sensible defaults without configuring each middleware manually.


Custom Middleware

Custom middleware can wrap any route handler.

r.Use(func(next router.HandlerFunc) router.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
		// before handler

		next(w, req, ctx)

		// after handler
	}
})

This is useful for authentication, authorization, tracing, request-scoped dependencies, custom headers, and application-specific behavior.


Notes

Middleware order matters.

Middleware is executed in the order it is registered, while handlers are wrapped internally so each middleware can run logic before and after the next handler.

For route-group specific middleware, use route groups and Group.Use.

Route Groups

Organize related routes under shared URL prefixes for APIs, versioning, admin panels, and modular applications.

Group Middleware

Apply middleware only to selected route groups such as protected APIs, admin panels, or internal services.

On this page

Registering MiddlewareBuilt-in Middleware OverviewExampleDefault MiddlewareCustom MiddlewareNotes