mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-22 15:17:18 +01:00
Tuple structs are cool
This commit is contained in:
parent
ea582ab8d9
commit
08c10fe58d
1 changed files with 11 additions and 16 deletions
|
@ -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);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue