{"id":20492070,"url":"https://github.com/zotonic/router","last_synced_at":"2025-04-13T17:02:03.351Z","repository":{"id":25534309,"uuid":"28966807","full_name":"zotonic/router","owner":"zotonic","description":"In-memory trie based router for fast parallel path lookups with wildcards.","archived":false,"fork":false,"pushed_at":"2022-06-10T11:04:51.000Z","size":52,"stargazers_count":14,"open_issues_count":0,"forks_count":3,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-03-26T09:58:51.783Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zotonic.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}},"created_at":"2015-01-08T13:20:23.000Z","updated_at":"2024-02-03T09:51:08.000Z","dependencies_parsed_at":"2022-08-24T00:31:14.005Z","dependency_job_id":null,"html_url":"https://github.com/zotonic/router","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zotonic","download_url":"https://codeload.github.com/zotonic/router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224819051,"owners_count":17375208,"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":[],"created_at":"2024-11-15T17:27:33.430Z","updated_at":"2024-11-15T17:27:34.221Z","avatar_url":"https://github.com/zotonic.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status][gh badge]][gh]\n[![Hex.pm version][hexpm version]][hexpm]\n[![Hex.pm Downloads][hexpm downloads]][hexpm]\n[![Hex.pm Documentation][hexdocs documentation]][hexdocs]\n[![Erlang Versions][erlang version badge]][gh]\n[![License][license]](LICENSE)\n\nrouter\n======\n\nrouter is an in-memory trie based path router for Erlang.\nIt can be used to route paths to destinations. It has single-level,\nand multi-level wildcard levels.\n\nIt can be used for arbitrary path/hierarchical based routing, and you\ncan have multiple independent routers.\n\nUsage\n-----\n\nCreate a new router like this:\n\n```erlang\n   Router = router:new()\n```\n\nThis creates multiple ets tables which are owned by the calling process. It\nis advised to call this inside a `gen_server`.\n\nNow you can add routes like this:\n\n```erlang\n   router:add(Router, [\u003c\u003c\"a\"\u003e\u003e, \u003c\u003c\"b\"\u003e\u003e], MyDestination),\n   ...\n```\n\nThis creates a route from path ```\u003c\u003c\"a\"\u003e\u003e, \u003c\u003c\"b\"\u003e\u003e``` to the information inside \n`MyDestination`. It is important that routes are added synchronized. It is best\nto do it inside a `handle_call` or `handle_cast` of a single `gen_server`.\n\nAfter this it is possible to route request paths to destinations:\n\n```erlang\n  Routes = router:route(Router, [\u003c\u003c\"foo\"\u003e\u003e, \u003c\u003c\"bar\"\u003e\u003e]),\n  ...\n```\n\nCalls to ```route``` can be called in parallel.\n\n\nWildcards\n---------\n\nRouter has two different wildcards, multi-level and single-level wildcards.\n\nThe following wildcards are implemented:\n\n- `'+'`, matches one level on the path.\n- `{'+', test}`, matches one level on the paths and binds the path element to test.\n- `{'+', test, {module, function}}`, matches the element on the path if the call\n  to `module:function(PathElement)` returns true.\n\nThere is one multi-level wildcard `'#'` which matches all elements on the path.\n\nExample single-level wildcard route:\n\n```erlang\n   router:add(Router, [\u003c\u003c\"rsc\"\u003e\u003e, {'+', id}], {controller_rsc, [{foo, \u003c\u003c\"bar\"\u003e\u003e]})\n```\n\nWhen you now call `router:route(Router,[\u003c\u003c\"rsc\"\u003e\u003e, \u003c\u003c\"12312\"\u003e\u003e])`, you get a list\nwith route tuples with all matching destinations and the list of bound variables in a\nproplist. In this case: `[{route, {controller_rsc, [{foo, \u003c\u003c\"bar\"\u003e\u003e}]}, [{id, \u003c\u003c\"12312\"\u003e\u003e}]`.\n\nYou get a list of matching destinations, and all elements which are bound in a proplist\nas result.\n\nExample multi-level wildcard route:\n\n```Erlang\n\n    router:add(Router, [\u003c\u003c\"truck\"\u003e\u003e, \u003c\u003c\"00001\"\u003e\u003e, '#'], self())\n```\n\nThis will match all routes to `[\u003c\u003c\"truck\"\u003e\u003e, \u003c\u003c\"00001\"\u003e\u003e]` and all its sub paths. For \nexample: `[\u003c\u003c\"truck\"\u003e\u003e, \u003c\u003c\"00001\"\u003e\u003e, \u003c\u003c\"temperature\"\u003e\u003e]` will match and also \n`[\u003c\u003c\"truck\"\u003e\u003e, \u003c\u003c\"00001\"\u003e\u003e, \u003c\u003c\"location\"\u003e\u003e]`.\n\n\n\n\u003c!-- Badges --\u003e\n[hexpm]: https://hex.pm/packages/router\n[hexpm version]: https://img.shields.io/hexpm/v/router.svg?style=flat-curcle \"Hex version\"\n[hexpm downloads]: https://img.shields.io/hexpm/dt/router.svg?style=flat-curcle\n[hexdocs documentation]: https://img.shields.io/badge/hex-docs-purple.svg?style=flat-curcle\n[hexdocs]: https://hexdocs.pm/router\n[gh]: https://github.com/zotonic/router/actions/workflows/test.yaml\n[gh badge]: https://github.com/zotonic/router/workflows/Test/badge.svg\n[erlang version badge]: https://img.shields.io/badge/Supported%20Erlang%2FOTP-22%20to%2025-blue.svg?style=flat-curcle\n[license]: https://img.shields.io/badge/License-MIT-blue.svg \"MIT\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzotonic%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzotonic%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzotonic%2Frouter/lists"}