Tuple structs are cool

This commit is contained in:
David Pedersen 2021-06-01 15:07:16 +02:00
parent ea582ab8d9
commit 08c10fe58d

View file

@ -11,7 +11,11 @@ use tower::{make::Shared, ServiceBuilder};
use tower_http::{ use tower_http::{
add_extension::AddExtensionLayer, compression::CompressionLayer, trace::TraceLayer, add_extension::AddExtensionLayer, compression::CompressionLayer, trace::TraceLayer,
}; };
use tower_web::{body::Body, extract, handler::Handler}; use tower_web::{
body::Body,
extract::{BytesMaxLength, Extension, UrlParams},
handler::Handler,
};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -48,14 +52,11 @@ struct State {
async fn get( async fn get(
_req: Request<Body>, _req: Request<Body>,
params: extract::UrlParams<(String,)>, UrlParams((key,)): UrlParams<(String,)>,
state: extract::Extension<SharedState>, Extension(state): Extension<SharedState>,
) -> Result<Bytes, StatusCode> { ) -> Result<Bytes, StatusCode> {
let state = state.0;
let db = &state.lock().unwrap().db; let db = &state.lock().unwrap().db;
let (key,) = params.0;
if let Some(value) = db.get(&key) { if let Some(value) = db.get(&key) {
Ok(value.clone()) Ok(value.clone())
} else { } else {
@ -65,15 +66,9 @@ async fn get(
async fn set( async fn set(
_req: Request<Body>, _req: Request<Body>,
params: extract::UrlParams<(String,)>, UrlParams((key,)): UrlParams<(String,)>,
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb BytesMaxLength(value): BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
state: extract::Extension<SharedState>, Extension(state): Extension<SharedState>,
) { ) {
let state = state.0; state.lock().unwrap().db.insert(key, value);
let db = &mut state.lock().unwrap().db;
let (key,) = params.0;
let value = value.0;
db.insert(key, value);
} }