{"id":16221575,"url":"https://github.com/static-web-server/hyper-middleware","last_synced_at":"2025-03-19T11:31:06.251Z","repository":{"id":38077073,"uuid":"498288472","full_name":"static-web-server/hyper-middleware","owner":"static-web-server","description":"A compact HTTP middleware and handler system for Hyper 0.14 [WIP]","archived":false,"fork":false,"pushed_at":"2024-02-05T23:01:34.000Z","size":57,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-28T18:23:19.618Z","etag":null,"topics":["http-middleware","http-server","hyper-rs","middleware","rust-lang"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/static-web-server.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","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},"funding":{"github":"joseluisq","custom":"paypal.me/joseluisqs"}},"created_at":"2022-05-31T10:27:08.000Z","updated_at":"2025-02-08T10:02:58.000Z","dependencies_parsed_at":"2024-10-27T20:42:36.037Z","dependency_job_id":null,"html_url":"https://github.com/static-web-server/hyper-middleware","commit_stats":{"total_commits":22,"total_committers":1,"mean_commits":22.0,"dds":0.0,"last_synced_commit":"e35d5874900bb07304a7576ee5cf4bc2e0a43202"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-web-server%2Fhyper-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-web-server%2Fhyper-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-web-server%2Fhyper-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-web-server%2Fhyper-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/static-web-server","download_url":"https://codeload.github.com/static-web-server/hyper-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243986103,"owners_count":20379266,"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":["http-middleware","http-server","hyper-rs","middleware","rust-lang"],"created_at":"2024-10-10T12:08:56.813Z","updated_at":"2025-03-19T11:31:05.923Z","avatar_url":"https://github.com/static-web-server.png","language":"Rust","readme":"# Hyper Middleware\n\n[![hyper-middleware crate](https://img.shields.io/crates/v/hyper-middleware.svg)](https://crates.io/crates/hyper-middleware)\n[![Released API docs](https://docs.rs/hyper-middleware/badge.svg)](https://docs.rs/hyper-middleware)\n[![hyper-middleware crate license](https://img.shields.io/crates/l/hyper-middleware)](./LICENSE-MIT)\n\n\u003e A compact HTTP middleware and handler system for [Hyper](https://github.com/hyperium/hyper) `0.14.x`.\u003cbr\u003e\n\u003e **NOTE:** This crate is still under active development.\n\n## Features\n\n- Compact Middleware and Handler System inspired by [The Iron Framework](https://github.com/iron/iron).\n- Simple [Hyper Service](https://docs.rs/hyper/latest/hyper/service/trait.Service.html) with convenient __Remote Address__ access.\n- Convenient `Error` and `Result` types powered by [anyhow](https://github.com/dtolnay/anyhow).\n- `Async` support via [async-trait](https://github.com/dtolnay/async-trait).\n- Macros to facilitate HTTP response errors or error casting.\n\n## Example\n\n[examples/server.rs](examples/server.rs)\n\n```rust\nuse hyper::{header, Server, StatusCode};\nuse hyper_middleware::{\n    async_trait, AfterMiddleware, BeforeMiddleware, Body, Error, Handler, Middlewares, Request,\n    Response, Result, Service,\n};\nuse std::{net::SocketAddr, path::PathBuf};\n\nstruct Config {\n    pub root: PathBuf,\n}\n\nstruct Application {\n    opts: Config,\n}\n\n#[async_trait]\nimpl Handler for Application {\n    async fn handle(\u0026self, req: \u0026mut Request) -\u003e Result\u003cResponse\u003e {\n        // Access the Hyper incoming Request\n        println!(\"Application::handle() - URI Path: {}\", req.uri().path());\n\n        // Access the custom app options\n        println!(\n            \"Application::handle() - Config Root: {}\",\n            self.opts.root.display()\n        );\n\n        // Access the Remote Address\n        let remote_addr = req.extensions().get::\u003cSocketAddr\u003e().unwrap();\n        println!(\"Application::handle() - Remote Addr: {}\", remote_addr);\n\n        // Create a Hyper Response and send it back to the middlewares chain\n        Ok(Response::new(Body::from(\"¡Hola!\")))\n    }\n}\n\nstruct FirstMiddleware {}\n\n#[async_trait]\nimpl BeforeMiddleware for FirstMiddleware {\n    async fn before(\u0026self, req: \u0026mut Request) -\u003e Result {\n        println!(\"FirstMiddleware::before()\");\n\n        // Access the Hyper incoming Request\n        println!(\"FirstMiddleware::before() - URI Path: {}\", req.uri().path());\n\n        Ok(())\n    }\n\n    async fn catch(\u0026self, _: \u0026mut Request, err: Error) -\u003e Result {\n        Err(err)\n    }\n}\n\nstruct SecondMiddleware {}\n\n#[async_trait]\nimpl AfterMiddleware for SecondMiddleware {\n    async fn after(\u0026self, _: \u0026mut Request, mut res: Response) -\u003e Result\u003cResponse\u003e {\n        println!(\"SecondMiddleware::after()\");\n\n        // Mutate the Hyper Response at convenience\n        // and send it back to other middlewares on the chain\n        res.headers_mut().insert(\n            header::CONTENT_TYPE,\n            \"text/html; charset=utf-8\".parse().unwrap(),\n        );\n\n        Ok(res)\n    }\n\n    async fn catch(\u0026self, _: \u0026mut Request, err: Error) -\u003e Result\u003cResponse\u003e {\n        Ok(Response::builder()\n            .status(StatusCode::NOT_FOUND)\n            .body(Body::from(err.to_string()))\n            .unwrap())\n    }\n}\n\n#[tokio::main(flavor = \"multi_thread\")]\nasync fn main() -\u003e Result {\n    // 0. Define some app options (example only)\n    let opts = Config {\n        root: std::env::current_dir().unwrap(),\n    };\n\n    // 1. Create a custom middleware chain and plug in some custom middlewares\n    let mut middlewares = Middlewares::new(Application { opts });\n    middlewares.link_before(FirstMiddleware {});\n    middlewares.link_after(SecondMiddleware {});\n\n    // 2. Create a Hyper service and set the current handler with its middlewares\n    let service = Service::new(middlewares);\n\n    // 3. Finally just run server using the service already created\n    let addr = ([127, 0, 0, 1], 8787).into();\n    let server = Server::bind(\u0026addr).serve(service);\n\n    println!(\"Listening on http://{}\", addr);\n\n    server.await?;\n\n    Ok(())\n}\n```\n\nTo run the example just type:\n\n```sh\ncargo run --example server\n```\n\n## Contributions\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in current work by you, as defined in the Apache-2.0 license, shall be dual licensed as described below, without any additional terms or conditions.\n\nFeel free to send some [Pull request](https://github.com/static-web-server/hyper-middleware/pulls) or file an [issue](https://github.com/static-web-server/hyper-middleware/issues).\n\n## License\n\nThis work is primarily distributed under the terms of both the [MIT license](LICENSE-MIT) and the [Apache License (Version 2.0)](LICENSE-APACHE).\n\n© 2022-present [Jose Quintana](https://joseluisq.net)\n","funding_links":["https://github.com/sponsors/joseluisq","paypal.me/joseluisqs"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatic-web-server%2Fhyper-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstatic-web-server%2Fhyper-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatic-web-server%2Fhyper-middleware/lists"}