Scripts2026年7月1日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Negroni Middleware
直接安装命令
npx -y tokrepo@latest install ff469060-7568-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产