Skip to content

happysnaker/go-http-middleware-kit

Repository files navigation

go-http-middleware-kit

Go Version License Stars Project Page Support Async Review

Reusable net/http middleware for backend services that want request IDs, real IP extraction, structured logs, panic recovery, and timeouts without pulling in a full framework.

This repo is intentionally small, readable, and production-minded:

What is included

  • RequestID middleware with context + response-header propagation
  • RealIP middleware for X-Forwarded-For / X-Real-IP
  • RequestLogger middleware built on log/slog
  • Recovery middleware with panic logging and stack traces
  • Timeout middleware backed by http.TimeoutHandler
  • Chain / Wrap helpers for clean composition

Quick start

go get github.com/happysnaker/go-http-middleware-kit
package main

import (
	"log/slog"
	"net/http"
	"os"
	"time"

	httpmw "github.com/happysnaker/go-http-middleware-kit"
)

func main() {
	logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

	mux := http.NewServeMux()
	mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("hello"))
	})

	handler := httpmw.Wrap(
		mux,
		httpmw.RealIP(httpmw.RealIPOptions{SetRemoteAddr: true}),
		httpmw.RequestID(httpmw.RequestIDOptions{TrustIncoming: true}),
		httpmw.Recovery(httpmw.RecoveryOptions{Logger: logger}),
		httpmw.RequestLogger(httpmw.RequestLoggerOptions{Logger: logger, IncludeUserAgent: true}),
		httpmw.Timeout(httpmw.TimeoutOptions{Duration: 5 * time.Second}),
	)

	_ = http.ListenAndServe(":8080", handler)
}

Middleware overview

Middleware Purpose
RequestID attach / generate request ids and expose them through context
RealIP resolve client IP from trusted proxy headers
RequestLogger emit structured request completion logs via slog
Recovery catch panics, log stack traces, and return HTTP 500
Timeout cap slow handlers with a timeout response

Why this repo exists

Many backend teams want the same small layer of HTTP middleware, but:

  • a lot of examples are too toy-like to reuse
  • some libraries pull in much more framework than needed
  • starter repos often hide the middleware details instead of making them easy to read

go-http-middleware-kit aims for the middle ground: enough to be practical, small enough to understand in one sitting.

Project layout

chain.go            middleware composition helpers
request_id.go       request-id propagation and generation
real_ip.go          trusted proxy IP extraction
logger.go           slog request logging
recovery.go         panic recovery middleware
timeout.go          request timeout wrapper
response_writer.go  response recorder for logs / metrics-like data
example/main.go     minimal integration example

Good fit for

  • small internal APIs
  • service starter templates
  • backend interview demos
  • teams that want lightweight net/http middleware without a framework migration

Related repos

Support

If this repo saves you time, consider:

If this middleware kit saved you copy-paste time in a real service, small direct support is especially appreciated:

Fastest path if this repo helped: open the support page, scan WeChat / Alipay, and leave a short note like go-http-middleware-kit or request-id middleware. Tying the tip to one concrete repo tends to convert much better than a generic donation.

License

MIT

About

Reusable net/http middleware kit for request IDs, structured logging, panic recovery, timeouts, and production-minded service basics.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages