Make Request<Body> an extractor

This commit is contained in:
David Pedersen
2021-06-09 09:42:06 +02:00
parent 90c3e5ba74
commit c91dc7ce29
10 changed files with 268 additions and 200 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
use http::{Request, StatusCode};
use http::StatusCode;
use hyper::Server;
use std::net::SocketAddr;
use tower::make::Shared;
@@ -18,11 +18,11 @@ async fn main() {
server.await.unwrap();
}
async fn handler(_req: Request<Body>) -> response::Html<&'static str> {
async fn handler() -> response::Html<&'static str> {
response::Html("<h1>Hello, World!</h1>")
}
async fn greet(_req: Request<Body>, params: extract::UrlParamsMap) -> Result<String, StatusCode> {
async fn greet(params: extract::UrlParamsMap) -> Result<String, StatusCode> {
if let Some(name) = params.get("name") {
Ok(format!("Hello {}!", name))
} else {
+4 -7
View File
@@ -7,7 +7,7 @@
//! ```
use bytes::Bytes;
use http::{Request, StatusCode};
use http::StatusCode;
use hyper::Server;
use std::{
borrow::Cow,
@@ -22,7 +22,7 @@ use tower_http::{
compression::CompressionLayer, trace::TraceLayer,
};
use tower_web::{
body::{Body, BoxBody},
body::BoxBody,
extract::{ContentLengthLimit, Extension, UrlParams},
prelude::*,
response::IntoResponse,
@@ -72,7 +72,6 @@ struct State {
}
async fn kv_get(
_req: Request<Body>,
UrlParams((key,)): UrlParams<(String,)>,
Extension(state): Extension<SharedState>,
) -> Result<Bytes, StatusCode> {
@@ -86,7 +85,6 @@ async fn kv_get(
}
async fn kv_set(
_req: Request<Body>,
UrlParams((key,)): UrlParams<(String,)>,
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
Extension(state): Extension<SharedState>,
@@ -94,7 +92,7 @@ async fn kv_set(
state.write().unwrap().db.insert(key, bytes);
}
async fn list_keys(_req: Request<Body>, Extension(state): Extension<SharedState>) -> String {
async fn list_keys(Extension(state): Extension<SharedState>) -> String {
let db = &state.read().unwrap().db;
db.keys()
@@ -104,12 +102,11 @@ async fn list_keys(_req: Request<Body>, Extension(state): Extension<SharedState>
}
fn admin_routes() -> BoxRoute<BoxBody> {
async fn delete_all_keys(_req: Request<Body>, Extension(state): Extension<SharedState>) {
async fn delete_all_keys(Extension(state): Extension<SharedState>) {
state.write().unwrap().db.clear();
}
async fn remove_key(
_req: Request<Body>,
UrlParams((key,)): UrlParams<(String,)>,
Extension(state): Extension<SharedState>,
) {