{"id":24302889,"url":"https://github.com/digipolitan/perfect-middleware","last_synced_at":"2026-05-29T08:31:29.847Z","repository":{"id":98675594,"uuid":"88290604","full_name":"Digipolitan/perfect-middleware","owner":"Digipolitan","description":"Perfect middleware swift allows developers to register middlewares inside a Perfect HTTPServer","archived":false,"fork":false,"pushed_at":"2019-05-08T13:29:01.000Z","size":29,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-06T14:31:37.691Z","etag":null,"topics":["middleware","perfect","perfect-server","router","swift","swift-server"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Digipolitan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2017-04-14T17:58:52.000Z","updated_at":"2019-05-08T13:28:59.000Z","dependencies_parsed_at":"2023-04-14T01:46:56.418Z","dependency_job_id":null,"html_url":"https://github.com/Digipolitan/perfect-middleware","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/Digipolitan/perfect-middleware","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Digipolitan","download_url":"https://codeload.github.com/Digipolitan/perfect-middleware/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digipolitan%2Fperfect-middleware/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33644177,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["middleware","perfect","perfect-server","router","swift","swift-server"],"created_at":"2025-01-17T00:19:59.769Z","updated_at":"2026-05-29T08:31:29.825Z","avatar_url":"https://github.com/Digipolitan.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"PerfectMiddleware\n=================================\n\n[![Swift Version](https://img.shields.io/badge/swift-5.0-orange.svg?style=flat)](https://developer.apple.com/swift/)\n[![Twitter](https://img.shields.io/badge/twitter-@Digipolitan-blue.svg?style=flat)](http://twitter.com/Digipolitan)\n\nPerfect middleware swift allows developer to register middlewares inside a Perfect HTTPServer\n\n## Installation\n\n### Swift Package Manager\n\nTo install PerfectMiddleware with SPM, add the following lines to your `Package.swift`.\n\n```swift\nlet package = Package(\n    name: \"XXX\",\n    products: [\n        .library(\n            name: \"XXX\",\n            targets: [\"XXX\"]),\n    ],\n    dependencies: [\n        .package(url: \"https://github.com/Digipolitan/perfect-middleware-swift.git\", from: \"1.1.0\")\n    ],\n    targets: [\n        .target(\n            name: \"XXX\",\n            dependencies: [\"PerfectMiddleware\"])\n    ]\n)\n```\n\n## The Basics\n\nCreate an HTTPServer and register routes in the RouterMiddleware\n\n```swift\nlet server = HTTPServer()\n\nlet router = RouterMiddleware()\n\nrouter.get(path: \"/\").bind { (context) in\n    context.response.setBody(string: \"It Works !\").completed()\n    context.next()\n}\n\nserver.use(router: router)\n\nserver.serverPort = 8887\n\ndo {\n    try server.start()\n    print(\"Server listening on port \\(server.serverPort)\")\n} catch PerfectError.networkError(let err, let msg) {\n    print(\"Network error thrown: \\(err) \\(msg)\")\n}\n```\n\nPassing data between middlewares, you can provide 2 or more middleware for the same route and shared data across each middleware using the context\n\n```swift\nrouter.get(path: \"/\")\n  .bind { (context) in\n    context[\"name\"] = \"Steve\"\n    context.next()\n  }\n  .bind { (context) in\n    guard let name = context[\"name\"] as? String else {\n        return\n    }\n    context.response.setBody(string: \"hello mr. \\(name)!\").completed()\n    context.next()\n  }\n```\n\nIt's possible to create and register Middleware subsclasses instead of closures\n\n```swift\nclass RandomMiddleware: Middleware {\n\n    func handle(context: RouteContext) throws {\n        context[\"rand\"] = arc4random()\n        context.next()\n    }\n}\n```\n\nRegister Middleware as follow :\n\n```swift\nrouter.post(path: \"/random\")\n  .bind(RandomMiddleware())\n  .bind { (context) in\n    guard let rand = context[\"rand\"] as? UInt32 else {\n        return\n    }\n    context.response.setBody(string: \"the result \\(rand)\").completed()\n    context.next()\n  }\n```\n\n## Advanced\n\n### Register middleware for all routes\n\nit's possible to register middlewares before \u0026 after all child routes of the router\n\n```swift\nrouter.use(event: .beforeAll) { (context) in\n    context[\"start_date\"] = Date()\n    context.next()\n}.use(event: .afterAll) { (context) in\n    guard let startDate = context[\"start_date\"] as? Date else {\n        return\n    }\n    let duration = Date().timeIntervalSince1970 - startDate.timeIntervalSince1970\n    print(String(duration * 1000) + \" ms\")\n    context.next()\n}\n```\n\nThis code will print the duration in second of the process for each route\n\n### 404 handler\n\n```swift\nrouter.use(event: .notFound) { (context) in\n    context.response.setBody(string: \"404\").completed()\n    context.next()\n}\n```\n\n### Error handler\n\n```swift\nrouter.use { (err, context) in\n    context.response.setBody(string: err.localizedDescription).completed()\n    context.next()\n}\n```\n\n### Register sub routers\n\nThis code print \"My name is\" by calling \"/user/name\" path in your server\n\n```swift\nlet router = RouterMiddleware()\n\nlet childRouter = RouterMiddleware()\n\nchildRouter.get(path: \"/name\")\n  .bind { (context) in\n    context.response.setBody(string: \"My name is\").completed()\n    context.next()\n  }\n\nrouter.use(path: \"/user\", child: childRouter)\n\nserver.use(router: router)\n```\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md) for more details!\n\nThis project adheres to the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).\nBy participating, you are expected to uphold this code. Please report\nunacceptable behavior to [contact@digipolitan.com](mailto:contact@digipolitan.com).\n\n## License\n\nPerfectMiddleware is licensed under the [BSD 3-Clause license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigipolitan%2Fperfect-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigipolitan%2Fperfect-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigipolitan%2Fperfect-middleware/lists"}