Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 1, 2026·3 min de lecture

Negroni — Idiomatic HTTP Middleware Library for Go

Negroni is a minimal, idiomatic Go library for composing HTTP middleware stacks, providing a thin layer between the net/http standard library and your application handlers.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Negroni Middleware
Commande d'installation directe
npx -y tokrepo@latest install ff469060-7568-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

Negroni is a small, non-intrusive middleware library for Go that embraces the standard net/http interfaces. Rather than replacing the standard library, it wraps it with a composable middleware stack so developers can layer logging, recovery, and custom handlers without learning a new API.

What Negroni Does

  • Composes middleware functions into a sequential processing pipeline
  • Wraps any net/http handler or ServeMux without requiring a custom router
  • Ships with Classic() defaults: request logger, panic recovery, and static file server
  • Allows per-route middleware via integration with third-party routers like Gorilla Mux
  • Provides a Handler interface compatible with standard http.Handler for easy adoption

Architecture Overview

Negroni maintains an ordered slice of middleware handlers. Each middleware receives a ResponseWriter, Request, and a next function. Calling next passes control to the subsequent middleware or the final handler. This design is intentionally simple: no reflection, no dependency injection, and no hidden magic — just function composition around net/http types.

Self-Hosting & Configuration

  • Requires Go 1.17 or later
  • Install with go get github.com/urfave/negroni
  • Create an instance with negroni.New() or negroni.Classic() for defaults
  • Add custom middleware with n.Use() or n.UseFunc() for simple functions
  • Bind to a port with n.Run(":8080") or attach to your own http.Server

Key Features

  • Zero-magic design that follows Go idioms and net/http conventions
  • Classic() preset bundles production-ready logging and recovery
  • Works with any router (Gorilla Mux, Chi, httprouter, or the standard ServeMux)
  • Middleware ordering is explicit and predictable
  • Tiny codebase under 300 lines makes auditing and understanding straightforward

Comparison with Similar Tools

  • Gin — full router with middleware; Negroni is middleware-only and router-agnostic
  • Chi — composable router with inline middleware; Negroni separates middleware from routing
  • Alice — similar middleware chaining; Negroni adds Classic() defaults and Run()
  • Echo — batteries-included framework; Negroni is deliberately minimal
  • Martini — by the same author but uses reflection; Negroni avoids reflection entirely

FAQ

Q: Does Negroni include a router? A: No. Negroni handles middleware composition only. Pair it with Gorilla Mux, Chi, or the standard ServeMux for routing.

Q: What is the Classic() preset? A: Classic() creates a Negroni instance pre-loaded with Logger, Recovery, and Static middleware — suitable for most web applications out of the box.

Q: Can I use Negroni with Go modules? A: Yes. The github.com/urfave/negroni path is fully compatible with Go modules.

Q: How does Negroni compare to writing raw middleware with net/http? A: Negroni provides the same pattern but adds a structured stack, default handlers, and the convenience of Run(). It does not replace net/http — it wraps it.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires