{"id":13424600,"url":"https://github.com/omartijn/cpprouter","last_synced_at":"2025-03-15T18:35:30.912Z","repository":{"id":44723941,"uuid":"254868502","full_name":"omartijn/cpprouter","owner":"omartijn","description":"A modern, header-only request router for C++","archived":false,"fork":false,"pushed_at":"2022-01-28T14:36:22.000Z","size":198,"stargazers_count":22,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-26T23:55:30.539Z","etag":null,"topics":["cmake","http","routing-engine"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/omartijn.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":"2020-04-11T12:59:53.000Z","updated_at":"2024-06-14T09:34:44.000Z","dependencies_parsed_at":"2022-09-11T20:12:04.492Z","dependency_job_id":null,"html_url":"https://github.com/omartijn/cpprouter","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omartijn%2Fcpprouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omartijn%2Fcpprouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omartijn%2Fcpprouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omartijn%2Fcpprouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omartijn","download_url":"https://codeload.github.com/omartijn/cpprouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243775898,"owners_count":20346281,"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":["cmake","http","routing-engine"],"created_at":"2024-07-31T00:00:56.847Z","updated_at":"2025-03-15T18:35:25.900Z","avatar_url":"https://github.com/omartijn.png","language":"C++","readme":"# cpprouter\nA modern, header-only request router for C++\n\n![](https://github.com/omartijn/cpprouter/workflows/ubuntu-latest/badge.svg)\n![](https://github.com/omartijn/cpprouter/workflows/macos-latest/badge.svg)\n![](https://github.com/omartijn/cpprouter/workflows/windows-latest/badge.svg)\n\n## Routing requests\n\nThis library is designed to route requests to an associated callback. Requests are matched\nusing a _pattern_, which may contain embedded regular expressions. The data matched with the\nregular expression is then parsed and provided to the callback.\n\n### URL patterns\n\nThe router matches incoming requests against list of URL patterns. A URL pattern consists\nof a combination of literal data and _slugs_, matched using a regular expression. Slugs are\n opened with a `{` and closed with a `}`.\n\nThe following example pattern\n\n`/test/{\\d+}/{\\w+}`\n\nwould match targets like `/test/123/hello` and `/test/999/bye`, but not `/test/xyz/abc` (because\n\\d only matches digits).\n\n### Callbacks\n\nThe library does not impose a particular callback signature on the user, instead letting the user\nchoose the signature that suits their needs.\n\nThe list of patterns and callbacks is stored in a _routing table_, which is available in the\n`router::table` class. This class is templated on the signature of the used callback functions.\n\nSay, your callback functions follow the `int(std::string\u0026\u0026 body)` signature, you would create\nan instance of `router::table\u003cint(std::string\u0026\u0026)\u003e`. On this instance you can register your URL\npatterns and their associated callbacks using the `add()` member function:\n\n```\nrouter.add\u003c\u0026callback\u003e(\"pattern\");\nrouter.add\u003c\u0026class::callback\u003e(\"pattern\", instance_pointer);\n```\n\nThe first variant is used for registering a _free function_, while the second variant is used\nfor registering a _member function_ as the callback. Callbacks may be registered on _any_ class,\nas long as they have the correct signature.\n\n### Working with slug data\n\nIf the URL patterns contain _slugs_, you are probably interested in the data they hold. There are\nthree ways to get this data.\n\n- Accept the data as separate parameters to the callback\n- Accept a tuple with the slug data in the callback\n- Accept a custom _data transfer object_ in the callback\n\nThe first two options are the easiest. Let's assume our previous examples of a callback definition\nof `int(std::string\u0026\u0026)` and a pattern of `/test/{\\d+}/{\\w+}`. From the pattern, it's clear that the\nfirst slug should contain _numeric data_, and the second slug contains a string. We could therefore\ndefine and register our callback like this:\n\n```\nint callback(std::string\u0026\u0026 body, int numeric, std::string\u0026\u0026 word);\nint callback(std::string\u0026\u0026 body, std::tuple\u003cint, std::string\u003e\u0026\u0026 slugs);\n```\n\nWhen a matching request is routed, the slugs are automatically parsed and converted to the right\ntype before the callback is invoked.\n\nIt is also possible to make a dedicated struct, a so-called _data transfer object_, to receive\nthe slug data.\n\nThe _data transfer object_ needs to be annotated so that cpprouter knows how to parse the slug\ndata, this is done by defining a `using` alias on the class.\n\n```\nclass slug_dto {\n    int numeric;\n    std::string word;\n\n    using dto = router::dto\u003cslug_dto\u003e\n        ::bind\u003c\u0026slug_dto::numeric\u003e\n        ::bind\u003c\u0026slug_dto::word\u003e;\n};\n```\n\nAssuming the same callback signature as before, the router and callback could be declared as follows:\n\n`int callback(std::string\u0026\u0026 body, slug_dto\u0026\u0026 slugs);`\n\n### Bringing it all together\n\nSee the examples (TODO)\n","funding_links":[],"categories":["Rest protocol"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomartijn%2Fcpprouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomartijn%2Fcpprouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomartijn%2Fcpprouter/lists"}