{"id":18494827,"url":"https://github.com/gbrlsnchs/mux","last_synced_at":"2025-05-14T02:33:06.445Z","repository":{"id":57552569,"uuid":"142954948","full_name":"gbrlsnchs/mux","owner":"gbrlsnchs","description":"Fast zero-allocation HTTP routing for Go :zap:","archived":false,"fork":false,"pushed_at":"2018-10-24T18:14:01.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T13:54:07.923Z","etag":null,"topics":["go","golang","handler","http","http-multiplexer","middleware","mux","radix-tree","router"],"latest_commit_sha":null,"homepage":"","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/gbrlsnchs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-31T03:01:17.000Z","updated_at":"2024-07-09T09:50:56.000Z","dependencies_parsed_at":"2022-09-26T18:50:42.801Z","dependency_job_id":null,"html_url":"https://github.com/gbrlsnchs/mux","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbrlsnchs%2Fmux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbrlsnchs%2Fmux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbrlsnchs%2Fmux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbrlsnchs%2Fmux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gbrlsnchs","download_url":"https://codeload.github.com/gbrlsnchs/mux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239210276,"owners_count":19600508,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["go","golang","handler","http","http-multiplexer","middleware","mux","radix-tree","router"],"created_at":"2024-11-06T13:22:21.246Z","updated_at":"2025-02-16T23:26:36.834Z","avatar_url":"https://github.com/gbrlsnchs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mux (HTTP multiplexer for Go)\n[![Build Status](https://travis-ci.org/gbrlsnchs/mux.svg?branch=master)](https://travis-ci.org/gbrlsnchs/mux)\n[![Sourcegraph](https://sourcegraph.com/github.com/gbrlsnchs/mux/-/badge.svg)](https://sourcegraph.com/github.com/gbrlsnchs/mux?badge)\n[![GoDoc](https://godoc.org/github.com/gbrlsnchs/mux?status.svg)](https://godoc.org/github.com/gbrlsnchs/mux)\n[![Minimal Version](https://img.shields.io/badge/minimal%20version-go1.10%2B-5272b4.svg)](https://golang.org/doc/go1.10)\n\n## About\nThis package is a fast HTTP multiplexer.\n\nIt uses a radix tree to match URLs. When matching simple routes, it's a zero allocation search.\nIt's fast, simple and supports middlewares in an elegant way.\n\n## Usage\nFull documentation [here].\n\n### Installing\n#### Go 1.10\n`vgo get -u github.com/gbrlsnchs/mux`\n#### Go 1.11 or after\n`go get -u github.com/gbrlsnchs/mux`\n\n### Importing\n```go\nimport (\n\t// ...\n\n\t\"github.com/gbrlsnchs/mux\"\n)\n```\n\n### Setting a handler (or handler function)\n#### First, set a context key\n```go\ntype key uint8\n\nconst ctxKey key = 0\n```\n\n#### Then, create a new router and set an endpoint handler\n```go\nrt := mux.NewRouter(\"/api\", ctxKey)\nrt.HandleFunc(http.MethodGet, \"/ping\", func(w http.ResponseWriter, _ *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n\tw.Write([]byte(\"pong\"))\n})\n```\n\n### Setting a common middleware for every endpoint\n#### First, define a middleware\n```go\nfunc loggingFunc(next http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tlog.Printf(\"%s %s\", r.Method, r.URL.Path)\n\t\tnext.ServeHTTP(w, r)\n\t})\n}\n```\n\n#### Then, create a router and use the middleware in all requests\n```go\nrt := mux.NewRouter(\"/api\")\nrt.Use(loggingFunc)\nrt.Handle(http.MethodGet, \"/ping\", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n\tw.Write([]byte(\"pong\"))\n}))\n```\n\n### Setting isolated middlewares\n#### First, define a handler and some middlewares\n```go\nfunc handler(w http.ResponseWriter, _ *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n})\n\nfunc authFunc(next http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tif mux.Params(r.Context(), ctxKey)[\"secret\"] != \"my_secret\" {\n\t\t\tw.WriteHeader(http.StatusUnauthorized)\n\t\t\treturn\n\t\t}\n\t\tnext.ServeHTTP(w, r)\n\t})\n}\n\nfunc permissionFunc(next http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tif !userIsAdmin(r) { // hypothetical function\n\t\t\tw.WriteHeader(http.StatusForbidden)\n\t\t\treturn\n\t\t}\n\t\tnext.ServeHTTP(w, r)\n\t})\n}\n```\n\n#### Then, create a middleware chain and add it to the router\n```go\nrt := mux.NewRouter(\"/\", ctxKey)\nguard := mux.NewChain(authFunc, permissionFunc)\n\nrt.Handle(http.MethodPost, \"/unprotected\", handler)\nrt.Handle(http.MethodPost, \"/protected/:secret\", guard(handler))\n```\n\n## Contributing\n### How to help\n- For bugs and opinions, please [open an issue](https://github.com/gbrlsnchs/mux/issues/new)\n- For pushing changes, please [open a pull request](https://github.com/gbrlsnchs/mux/compare)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbrlsnchs%2Fmux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgbrlsnchs%2Fmux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbrlsnchs%2Fmux/lists"}