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

Group Middleware

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

Middleware can be attached directly to a route group.

This allows specific middleware to affect only a selected part of the application instead of all routes globally.

Group middleware is useful for:

  • authentication
  • authorization
  • API versioning
  • admin-only routes
  • rate limiting
  • request validation
  • internal services

Registering Middleware on a Group

Use Group.Use(...) to attach middleware to a route group.

api.Use(SimpleAuth)

Only routes inside that group are affected.


Example

package main

import (
	"net/http"

	"github.com/netlifeguru/router"
)

func SimpleAuth(next router.HandlerFunc) router.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request, ctx *router.Context) {
		apiKey := r.Header.Get("X-API-KEY")

		if apiKey != "secret-key" {
			http.Error(w, "Unauthorized: Invalid API Key", http.StatusUnauthorized)
			return
		}

		next(w, r, ctx)
	}
}

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

	api := r.Group("/api")

	api.Use(SimpleAuth)

	api.GET("/data", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
		w.Write([]byte("Secret data accessed successfully!"))
	})

	r.ListenAndServe(":8000")
}

Testing the Endpoint

Request without the API key:

curl http://localhost:8000/api/data

Response:

Unauthorized: Invalid API Key

Request with the correct API key:

curl -H "X-API-KEY: secret-key" http://localhost:8000/api/data

Response:

Secret data accessed successfully!

Middleware Scope

Middleware registered on a group affects only routes registered inside that group.

Example:

public := r.Group("/public")
api := r.Group("/api")

api.Use(SimpleAuth)

Result:

RouteMiddleware
/public/*none
/api/*SimpleAuth

This allows public and protected routes to coexist cleanly inside the same application.


Nested Groups

Middleware inheritance is hierarchical.

Example:

api := r.Group("/api")
api.Use(SimpleAuth)

admin := api.Group("/admin")

Routes inside /api/admin automatically inherit the middleware from /api.


Notes

Group middleware is executed after global middleware registered through r.Use(...).

This allows applications to combine:

  • global middleware
  • group-specific middleware
  • route-specific behavior

in a predictable middleware chain.

Middleware

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

Mounting Standard Handlers

Mount native net/http handlers, third-party services, and wildcard routes directly into the router without using router.Context.

On this page

Registering Middleware on a GroupExampleTesting the EndpointMiddleware ScopeNested GroupsNotes