{"id":13367438,"url":"https://github.com/Go-playground/pure","last_synced_at":"2025-03-12T18:32:57.384Z","repository":{"id":56234094,"uuid":"69056502","full_name":"go-playground/pure","owner":"go-playground","description":":non-potable_water: Is a lightweight  HTTP router that sticks to the std \"net/http\" implementation","archived":false,"fork":false,"pushed_at":"2023-07-13T17:05:28.000Z","size":110,"stargazers_count":149,"open_issues_count":0,"forks_count":14,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-09-30T09:35:09.731Z","etag":null,"topics":["context","http-router","native-http"],"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/go-playground.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-09-23T19:57:58.000Z","updated_at":"2024-09-27T11:48:46.000Z","dependencies_parsed_at":"2024-01-08T16:08:51.569Z","dependency_job_id":"85225219-b8de-4a41-8222-21f3e91439e2","html_url":"https://github.com/go-playground/pure","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fpure","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fpure/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fpure/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fpure/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-playground","download_url":"https://codeload.github.com/go-playground/pure/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221309994,"owners_count":16795840,"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":["context","http-router","native-http"],"created_at":"2024-07-30T00:01:49.095Z","updated_at":"2024-10-24T11:31:24.359Z","avatar_url":"https://github.com/go-playground.png","language":"Go","readme":"package pure\n============\n\u003cimg align=\"right\" src=\"https://raw.githubusercontent.com/go-playground/pure/master/logo.png\"\u003e![Project status](https://img.shields.io/badge/version-5.3.0-green.svg)\n[![Build Status](https://travis-ci.org/go-playground/pure.svg?branch=master)](https://travis-ci.org/go-playground/pure)\n[![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure)\n[![GoDoc](https://godoc.org/github.com/go-playground/pure?status.svg)](https://pkg.go.dev/github.com/go-playground/pure)\n![License](https://img.shields.io/dub/l/vibe-d.svg)\n[![Gitter](https://badges.gitter.im/go-playground/pure.svg)](https://gitter.im/go-playground/pure?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n\nPure is a fast radix-tree based HTTP router that sticks to the native implementations of Go's \"net/http\" package;\nin essence, keeping the handler implementations 'pure' by using Go 1.7's \"context\" package.\n\nThis makes heavy usage of `github.com/go-playground/pkg/v5` for HTTP abstractions.\n\nWhy Another HTTP Router?\n------------------------\nI initially created [lars](https://github.com/go-playground/lars), which I still maintain, that wraps the native implementation, think of this package as a Go pure implementation of [lars](https://github.com/go-playground/lars)\n\nKey \u0026 Unique Features \n--------------\n- [x] It sticks to Go's native implementations while providing helper functions for convenience\n- [x] **Fast \u0026 Efficient** - pure uses a custom version of [httprouter](https://github.com/julienschmidt/httprouter)'s radix tree, so incredibly fast and efficient.\n\nInstallation\n-----------\n\nUse go get \n\n```shell\ngo get -u github.com/go-playground/pure/v5\n```\n\nUsage\n------\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/go-playground/pure/v5\"\n\tmw \"github.com/go-playground/pure/v5/_examples/middleware/logging-recovery\"\n)\n\nfunc main() {\n\n\tp := pure.New()\n\tp.Use(mw.LoggingAndRecovery(true))\n\n\tp.Get(\"/\", helloWorld)\n\n\thttp.ListenAndServe(\":3007\", p.Serve())\n}\n\nfunc helloWorld(w http.ResponseWriter, r *http.Request) {\n\tw.Write([]byte(\"Hello World\"))\n}\n```\n\nRequestVars\n-----------\nThis is an interface that is used to pass request scoped variables and functions using `context.Context`.\nIt is implemented in this way because retrieving values from `context` isn't the fastest, and so using this \nthe router can store multiple pieces of information while reducing lookup time to a single stored `RequestVars`.\n\nCurrently only the URL/SEO params are stored on the `RequestVars` but if/when more is added they can merely be added\nto the `RequestVars` and there will be no additional lookup time.\n\nURL Params\n----------\n\n```go\np := p.New()\n\n// the matching param will be stored in the context's params with name \"id\"\np.Get(\"/user/:id\", UserHandler)\n\n// extract params like so\nrv := pure.RequestVars(r) // done this way so only have to extract from context once, read above\nrv.URLParam(paramname)\n\n// serve css, js etc.. pure.RequestVars(r).URLParam(pure.WildcardParam) will return the remaining path if \n// you need to use it in a custom handler...\np.Get(\"/static/*\", http.StripPrefix(\"/static/\", http.FileServer(http.Dir(\"static\"))).ServeHTTP)\n\n...\n```\n\n**Note:** Since this router has only explicit matches, you can not register static routes and parameters for the same path segment. For example you can not register the patterns /user/new and /user/:user for the same request method at the same time. The routing of different request methods is independent from each other. I was initially against this, however it nearly cost me in a large web application where the dynamic param value say :type actually could have matched another static route and that's just too dangerous and so it is not allowed.\n\nGroups\n-----\n```go\n\np.Use(LoggingAndRecovery, Gzip...)\n...\np.Post(\"/users/add\", ...)\n\n// creates a group for /user/:userid + inherits all middleware registered previously by p\nuser := p.Group(\"/user/:userid\")\nuser.Get(\"\", ...)\nuser.Post(\"\", ...)\nuser.Delete(\"/delete\", ...)\n\ncontactInfo := user.Group(\"/contact-info/:cid\")\ncontactinfo.Delete(\"/delete\", ...)\n\n// creates a group for /others, inherits all middleware registered previously by p + adds \n// OtherHandler to middleware\nothers := p.GroupWithMore(\"/others\", OtherHandler)\n\n// creates a group for /admin WITH NO MIDDLEWARE... more can be added using admin.Use()\nadmin := p.GroupWithNone(\"/admin\")\nadmin.Use(SomeAdminSecurityMiddleware)\n...\n```\n\nDecoding Body\n-------------\ncurrently JSON, XML, FORM, Multipart Form and url.Values are support out of the box; there are also \nindividual functions for each as well when you know the Content-Type.\n```go\n\t// second argument denotes yes or no I would like URL query parameter fields\n\t// to be included. i.e. 'id' and 'id2' in route '/user/:id?id2=val' should it be included.\n\tif err := pure.Decode(r, true, maxBytes, \u0026user); err != nil {\n\t\tlog.Println(err)\n\t}\n```\n\nMisc\n-----\n```go\n\n// set custom 404 ( not Found ) handler\np.Register404(404Handler, middleware_like_logging)\n\n// Redirect to or from ending slash if route not found, default is true\np.SetRedirectTrailingSlash(true)\n\n// Handle 405 ( Method Not allowed ), default is false\np.RegisterMethodNotAllowed(middleware)\n\n// automatically handle OPTION requests; manually configured\n// OPTION handlers take precedence. default false\np.RegisterAutomaticOPTIONS(middleware)\n\n```\n\nMiddleware\n-----------\nThere are some pre-defined middlewares within the middleware folder; NOTE: that the middleware inside will\ncomply with the following rule(s):\n\n* Are completely reusable by the community without modification\n\nOther middleware will be listed under the _examples/middleware/... folder for a quick copy/paste modify. As an example a LoddingAndRecovery middleware is very application dependent and therefore will be listed under the _examples/middleware/...\n\nBenchmarks\n-----------\nRun on i5-7600 16 GB DDR4-2400 using Go version go1.12.5 darwin/amd64\n\nNOTICE: pure uses a custom version of [httprouter](https://github.com/julienschmidt/httprouter)'s radix tree, benchmarks can be found [here](https://github.com/deankarn/go-http-routing-benchmark/tree/pure-and-lars) the slowdown is with the use of the `context` package, as you can see when no SEO params are defined, and therefore no need to store anything in the context, it is faster than even lars.\n\n```go\ngo test -bench=. -benchmem=true ./...\n#GithubAPI Routes: 203\n   Pure: 37096 Bytes\n\n#GPlusAPI Routes: 13\n   Pure: 2792 Bytes\n\n#ParseAPI Routes: 26\n   Pure: 5040 Bytes\n\n#Static Routes: 157\n   HttpServeMux: 14992 Bytes\n   Pure: 21096 Bytes\n\n\ngoos: darwin\ngoarch: arm64\nBenchmarkPure_Param             11965519               100.4 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_Param5             8756385               138.6 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_Param20            4335284               276.5 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_ParamWrite         9980685               120.0 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_GithubStatic      47743062                24.77 ns/op            0 B/op          0 allocs/op\nBenchmarkPure_GithubParam        8514968               139.8 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_GithubAll            42250             28333 ns/op           42753 B/op        167 allocs/op\nBenchmarkPure_GPlusStatic       87363000                13.39 ns/op            0 B/op          0 allocs/op\nBenchmarkPure_GPlusParam        10398274               113.0 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_GPlus2Params       9235220               128.7 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_GPlusAll            792037              1526 ns/op            2816 B/op         11 allocs/op\nBenchmarkPure_ParseStatic       79194198                14.96 ns/op            0 B/op          0 allocs/op\nBenchmarkPure_ParseParam        11391336               104.5 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_Parse2Params      10103078               116.2 ns/op           256 B/op          1 allocs/op\nBenchmarkPure_ParseAll            498306              2417 ns/op            4096 B/op         16 allocs/op\nBenchmarkPure_StaticAll           219930              5225 ns/op               0 B/op          0 allocs/op\n```\n\nLicenses\n--------\n- [MIT License](https://raw.githubusercontent.com/go-playground/pure/master/LICENSE) (MIT), Copyright (c) 2016 Dean Karn\n- [BSD License](https://raw.githubusercontent.com/julienschmidt/httprouter/master/LICENSE), Copyright (c) 2013 Julien Schmidt. All rights reserved.\n","funding_links":[],"categories":["XML"],"sub_categories":["路由"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGo-playground%2Fpure","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGo-playground%2Fpure","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGo-playground%2Fpure/lists"}