{"id":36790287,"url":"https://github.com/csmith/middleware","last_synced_at":"2026-01-12T13:25:02.171Z","repository":{"id":315121007,"uuid":"1058024073","full_name":"csmith/middleware","owner":"csmith","description":"Go HTTP middleware library","archived":false,"fork":false,"pushed_at":"2025-12-29T17:15:57.000Z","size":58,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-01-01T22:19:59.968Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/csmith.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-16T14:17:39.000Z","updated_at":"2025-12-29T17:15:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"4269bb2d-95f1-4512-b9db-4ef9590d7f24","html_url":"https://github.com/csmith/middleware","commit_stats":null,"previous_names":["csmith/middleware"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/csmith/middleware","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csmith%2Fmiddleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csmith%2Fmiddleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csmith%2Fmiddleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csmith%2Fmiddleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/csmith","download_url":"https://codeload.github.com/csmith/middleware/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csmith%2Fmiddleware/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28338997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T12:22:26.515Z","status":"ssl_error","status_checked_at":"2026-01-12T12:22:10.856Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-12T13:25:00.718Z","updated_at":"2026-01-12T13:25:02.143Z","avatar_url":"https://github.com/csmith.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go HTTP middleware\n\nContains a collection of generally useful middlewares for use with Go's built-in\n`net/http` server.\n\n## Middleware\n\n### Cache Control\n\nAutomatically sets a `Cache-Control` header with a max-age based on the\n`Content-Type` header. By default, static assets like images, videos, and\ndownloads get a max-age of 1 year, while text assets like HTML and CSS get a\nmax-age of 1 hour.\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\t\"time\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\t// With default cache times\n\thttp.ListenAndServe(\":8080\", middleware.CacheControl()(mux))\n\n\t// With custom cache times\n\thttp.ListenAndServe(\":8080\", middleware.CacheControl(middleware.WithCacheTimes(map[string]time.Duration{\n\t\t\"application/json\": time.Duration(0),\n\t\t\"image/*\":          time.Hour * 24,\n\t\t\"text/css\":         time.Hour * 12,\n\t}))(mux))\n}\n```\n\n### Compress\n\nAutomatically compresses the response body if the client accepts gzip encoding.\nSupports configurable compression levels and handles Accept-Encoding headers\nwith quality values.\n\n```go\npackage main\n\nimport (\n\t\"compress/gzip\"\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\t// With default compression level\n\thttp.ListenAndServe(\":8080\", middleware.Compress()(mux))\n\n\t// With custom compression level\n\thttp.ListenAndServe(\":8080\", middleware.Compress(middleware.WithGzipLevel(gzip.BestSpeed))(mux))\n\t\n\t// With additional custom logic for disabling compression on certain requests \n\thttp.ListenAndServe(\":8080\", middleware.Compress(middleware.WithCompressionCheck(func(r *http.Request) bool {\n\t\treturn r.URL.Path != \"/special\"\n\t}))(mux))\n}\n```\n\n### Chain\n\nAllows you to chain other middleware together, without directly chaining the\nfunction calls.\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\tnotFoundHandler := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {\n        // ...\n\t})\n\n\tchain := middleware.Chain(middleware.WithMiddleware(\n\t\t// Innermost\n\t\tmiddleware.CacheControl(),\n\t\tmiddleware.ErrorHandler(middleware.WithErrorHandler(http.StatusNotFound, notFoundHandler)),\n\t\tmiddleware.TextLog(middleware.WithTextLogFormat(middleware.TextLogFormatCombined)),\n\t\tmiddleware.Recover(),\n\t\t// Outermost\n    ))\n\thttp.ListenAndServe(\":8080\", chain(mux))\n}\n```\n\n### Cross Origin Protection\n\nDefends against CSRF attacks by denying unsafe requests that originated from a\ndifferent origin. GET, HEAD and OPTIONS requests are always allowed. Any other\nrequest has its `Sec-Fetch-Site` header verified. If present, it must either be\n`same-origin` or `none` for the request to proceed.\n\nDenied requests are responded to with a 403 response with no body. Chain this\nmiddleware with Error Handler to customise this.\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\thttp.ListenAndServe(\":8080\", middleware.CrossOriginProtection()(mux))\n}\n```\n\n### Error Handler\n\nHandles HTTP status codes by invoking custom handlers. When a registered status\ncode is returned by the next handler, its response is dropped and replaced with\nthe custom handler's response.\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\tnotFoundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tw.WriteHeader(http.StatusNotFound)\n\t\tw.Write([]byte(\"Custom 404 page\"))\n\t})\n\n\tserverErrorHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tw.WriteHeader(http.StatusInternalServerError)\n\t\tw.Write([]byte(\"Custom 500 page\"))\n\t})\n\n\thandler := middleware.ErrorHandler(\n\t\t// Add one more handlers for specific status codes\n\t\tmiddleware.WithErrorHandler(http.StatusNotFound, notFoundHandler),\n\t\tmiddleware.WithErrorHandler(http.StatusInternalServerError, serverErrorHandler),\n\t\t// If you want to preserve headers set by the original handler\n\t\tmiddleware.WithClearHeadersOnError(false),\n\t)(mux)\n\n\thttp.ListenAndServe(\":8080\", handler)\n}\n```\n\n### Headers\n\nAdds headers to a response as late as possible. This may be useful when chained\nwith other middleware such as ErrorHandler that change headers.\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\thandler := middleware.Headers(\n\t\tmiddleware.WithHeader(\"X-Frame-Options\", \"DENY\"),\n\t\tmiddleware.WithHeader(\"X-Content-Type-Options\", \"nosniff\"),\n\t\tmiddleware.WithHeader(\"Cache-Control\", \"no-cache\"),\n\t\tmiddleware.WithHeader(\"Cache-Control\", \"no-store\"), // Multiple values for same key\n\t)(mux)\n\n\thttp.ListenAndServe(\":8080\", handler)\n}\n```\n\n### Real Address\n\nGets the real address of the client by parsing `X-Forwarded-For` headers from\ntrusted proxies. By default only private IP ranges are trusted. Sets the\n`RemoteAddr` field of the request to the first untrusted hop (or the last hop\nif they're all trusted).\n\n```go\npackage main\n\nimport (\n\t\"net\"\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\t// With default options\n\thttp.ListenAndServe(\":8080\", middleware.RealAddress()(mux))\n\n\t// With custom trusted proxies\n\tvar trustedProxies []net.IPNet // Populate appropriately\n\thttp.ListenAndServe(\":8080\", middleware.RealAddress(middleware.WithTrustedProxies(trustedProxies))(mux))\n}\n```\n\n### Recover\n\nRecovers from downstream panics by logging them and returning a 500 error to\nthe client.\n\n```go\npackage main\n\nimport (\n\t\"log/slog\"\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\t// With default options\n\thttp.ListenAndServe(\":8080\", middleware.Recover()(mux))\n\n\t// With custom logger\n\thttp.ListenAndServe(\":8080\", middleware.Recover(middleware.WithPanicLogger(func(r *http.Request, err any) {\n\t\tslog.Error(\"Panic serving request\", \"err\", err, \"url\", r.URL)\n\t}))(mux))\n}\n```\n\n### Text Log\n\nLogs details of each request in either Common Log Format or Combined Log Format.\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\t\"os\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\t// With default options (Common Log Format to stdout)\n\thttp.ListenAndServe(\":8080\", middleware.TextLog()(mux))\n\n\t// With Combined Log Format\n\thttp.ListenAndServe(\":8080\", middleware.TextLog(middleware.WithTextLogFormat(middleware.TextLogFormatCombined))(mux))\n\n\t// With custom sink\n\tfile, _ := os.OpenFile(\"access.log\", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)\n\thttp.ListenAndServe(\":8080\", middleware.TextLog(middleware.WithTextLogSink(func(line string) {\n\t\tfile.WriteString(line + \"\\n\")\n\t}))(mux))\n}\n```\n\n### Strip Trailing Slashes\n\nRemoves trailing slashes from request URLs\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\t// Requests to /foo/ will be served as /foo\n\thttp.ListenAndServe(\":8080\", middleware.StripTrailingSlashes()(mux))\n}\n```\n\n### Redirect Trailing Slashes\n\nRedirects URLs without trailing slashes to their equivalent with a trailing slash\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/csmith/middleware\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\t// With default redirect code 308 Permanent Redirect\n\thttp.ListenAndServe(\":8080\", middleware.RedirectTrailingSlashes()(mux))\n\n\t// With custom redirect code 307 Temporary Redirect)\n\thttp.ListenAndServe(\":8080\", middleware.RedirectTrailingSlashes(\n\t\tmiddleware.WithRedirectCode(http.StatusTemporaryRedirect),\n\t)(mux))\n}\n```\n\n## Issues/Contributing/etc\n\nBug reports, feature requests, and pull requests are all welcome.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsmith%2Fmiddleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsmith%2Fmiddleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsmith%2Fmiddleware/lists"}