{"id":13492969,"url":"https://github.com/justinas/alice","last_synced_at":"2025-05-13T16:11:50.516Z","repository":{"id":17377159,"uuid":"20149101","full_name":"justinas/alice","owner":"justinas","description":"Painless middleware chaining for Go","archived":false,"fork":false,"pushed_at":"2024-06-06T09:50:16.000Z","size":34,"stargazers_count":3211,"open_issues_count":11,"forks_count":155,"subscribers_count":51,"default_branch":"master","last_synced_at":"2025-04-24T01:51:15.365Z","etag":null,"topics":["go","handler","middleware","web"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/justinas/alice","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/justinas.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-05-25T07:27:41.000Z","updated_at":"2025-04-23T15:35:15.000Z","dependencies_parsed_at":"2024-04-06T19:22:31.915Z","dependency_job_id":"81fad12f-54ca-4cc9-9dc3-fc188d226959","html_url":"https://github.com/justinas/alice","commit_stats":{"total_commits":58,"total_committers":16,"mean_commits":3.625,"dds":0.5172413793103448,"last_synced_commit":"619712d23b64958db44b03f008edbbf6acf61519"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinas%2Falice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinas%2Falice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinas%2Falice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinas%2Falice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justinas","download_url":"https://codeload.github.com/justinas/alice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253980055,"owners_count":21994042,"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","handler","middleware","web"],"created_at":"2024-07-31T19:01:10.919Z","updated_at":"2025-05-13T16:11:50.449Z","avatar_url":"https://github.com/justinas.png","language":"Go","readme":"# Alice \n\n[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](http://godoc.org/github.com/justinas/alice)\n[![Build Status](https://travis-ci.org/justinas/alice.svg?branch=master)](https://travis-ci.org/justinas/alice)\n[![Coverage](http://gocover.io/_badge/github.com/justinas/alice)](http://gocover.io/github.com/justinas/alice)\n\nAlice provides a convenient way to chain \nyour HTTP middleware functions and the app handler.\n\nIn short, it transforms\n\n```go\nMiddleware1(Middleware2(Middleware3(App)))\n```\n\nto\n\n```go\nalice.New(Middleware1, Middleware2, Middleware3).Then(App)\n```\n\n### Why?\n\nNone of the other middleware chaining solutions\nbehaves exactly like Alice.\nAlice is as minimal as it gets:\nin essence, it's just a for loop that does the wrapping for you.\n\nCheck out [this blog post](http://justinas.org/alice-painless-middleware-chaining-for-go/)\nfor explanation how Alice is different from other chaining solutions.\n\n### Usage\n\nYour middleware constructors should have the form of\n\n```go\nfunc (http.Handler) http.Handler\n```\n\nSome middleware provide this out of the box.\nFor ones that don't, it's trivial to write one yourself.\n\n```go\nfunc myStripPrefix(h http.Handler) http.Handler {\n    return http.StripPrefix(\"/old\", h)\n}\n```\n\nThis complete example shows the full power of Alice.\n\n```go\npackage main\n\nimport (\n    \"net/http\"\n    \"time\"\n\n    \"github.com/throttled/throttled\"\n    \"github.com/justinas/alice\"\n    \"github.com/justinas/nosurf\"\n)\n\nfunc timeoutHandler(h http.Handler) http.Handler {\n    return http.TimeoutHandler(h, 1*time.Second, \"timed out\")\n}\n\nfunc myApp(w http.ResponseWriter, r *http.Request) {\n    w.Write([]byte(\"Hello world!\"))\n}\n\nfunc main() {\n    th := throttled.Interval(throttled.PerSec(10), 1, \u0026throttled.VaryBy{Path: true}, 50)\n    myHandler := http.HandlerFunc(myApp)\n\n    chain := alice.New(th.Throttle, timeoutHandler, nosurf.NewPure).Then(myHandler)\n    http.ListenAndServe(\":8000\", chain)\n}\n```\n\nHere, the request will pass [throttled](https://github.com/PuerkitoBio/throttled) first,\nthen an http.TimeoutHandler we've set up,\nthen [nosurf](https://github.com/justinas/nosurf)\nand will finally reach our handler.\n\nNote that Alice makes **no guarantees** for\nhow one or another piece of  middleware will behave.\nOnce it passes the execution to the outer layer of middleware,\nit has no saying in whether middleware will execute the inner handlers.\nThis is intentional behavior.\n\nAlice works with Go 1.0 and higher.\n\n### Contributing\n\n0. Find an issue that bugs you / open a new one.\n1. Discuss.\n2. Branch off, commit, test.\n3. Make a pull request / attach the commits to the issue.\n","funding_links":[],"categories":["Misc","Go","Web Frameworks","XML","中间件","Libraries for creating HTTP middlewares","Web框架","Middlewares","Repositories","中间件### 中间件"],"sub_categories":["Middlewares","中间件","创建http中间件的代码库","Microsoft Word","Fail injection","中間件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinas%2Falice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustinas%2Falice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinas%2Falice/lists"}