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::{
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]
async fn main() {
@ -48,14 +52,11 @@ struct State {
async fn get(
_req: Request<Body>,
params: extract::UrlParams<(String,)>,
state: extract::Extension<SharedState>,
UrlParams((key,)): UrlParams<(String,)>,
Extension(state): Extension<SharedState>,
) -> Result<Bytes, StatusCode> {
let state = state.0;
let db = &state.lock().unwrap().db;
let (key,) = params.0;
if let Some(value) = db.get(&key) {
Ok(value.clone())
} else {
@ -65,15 +66,9 @@ async fn get(
async fn set(
_req: Request<Body>,
params: extract::UrlParams<(String,)>,
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
state: extract::Extension<SharedState>,
UrlParams((key,)): UrlParams<(String,)>,
BytesMaxLength(value): BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
Extension(state): Extension<SharedState>,
) {
let state = state.0;
let db = &mut state.lock().unwrap().db;
let (key,) = params.0;
let value = value.0;
db.insert(key, value);
state.lock().unwrap().db.insert(key, value);
}