commit ae9340276ff080a54556e4452e0c8d4d67aed2e3 Author: Erik Hollensbe Date: Fri Jan 14 22:27:21 2022 -0800 initial commit; with baseline deps added Signed-off-by: Erik Hollensbe diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7bc9c38 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ratpack" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +hyper = { version = "*", features = [ "http1", "http2", "server", "runtime", "tcp", "stream" ] } +http = "*" diff --git a/http-api.md b/http-api.md new file mode 100644 index 0000000..455090a --- /dev/null +++ b/http-api.md @@ -0,0 +1,58 @@ +## http API idea + +```rust +async fn root(req: Request) -> Result { + Ok(Response::builder() + .status(StatusCode::OK) + .body("hello, world!") + .build()?) +} + +async fn with_param(req: Request, params: Params) -> Result { + let param = params.get("param")?; + Ok(Response::builder() + .status(StatusCode::OK) + .body(&format!("hello, {}!", param)) + .build()?) +} + +async fn with_param_post(req: Request, params: Params) -> Result { + let param = params.get("param")?; + let body = req.body().await?; // should also support iter() probably for large requests + Ok(Response::builder() + .status(StatusCode::OK) + .body(&format!("hello, {}!\n{}\n", param, body)) + .build()?) +} + +#[derive(Clone, Debug)] +struct ToggleMiddleware { + state: AtomicBool, +} + +#[async_trait] +impl Middleware for ToggleMiddleware { + async fn perform( + &mut self, + req: Request, + params: Params, + next: Next, + ) -> Result { + self.state.swap(self.state.get(), Ordering::Relaxed); + let response = next.call(req).await?; + let headers = response.headers_mut(); + headers.insert("state", &format!("{:?}", self.state.get())); + Ok(response) + } +} + +#[tokio::main] +async fn main() { + let mut app = Server::new(); + app.with(ToggleMiddleware::new()); + app.get("/", root); + app.get("/:param", with_param); + app.post("/:param", with_param_post); + app.listen("127.0.0.1:3000"); +} +``` diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1b4a90c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,8 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +}