| 10 | |
| 11 | #[tokio::main] |
| 12 | async fn main() { |
| 13 | let log_filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "practical_rust_book=info,warp=error".to_owned()); |
| 14 | |
| 15 | let store = store::Store::new(); |
| 16 | let store_filter = warp::any().map(move || store.clone()); |
| 17 | |
| 18 | tracing_subscriber::fmt() |
| 19 | // Use the filter we built above to determine which traces to record. |
| 20 | .with_env_filter(log_filter) |
| 21 | // Record an event when each span closes. This can be used to time our |
| 22 | // routes' durations! |
| 23 | .with_span_events(FmtSpan::CLOSE) |
| 24 | .init(); |
| 25 | |
| 26 | let cors = warp::cors() |
| 27 | .allow_any_origin() |
| 28 | .allow_header("content-type") |
| 29 | .allow_methods(&[Method::PUT, Method::DELETE, Method::GET, Method::POST]); |
| 30 | |
| 31 | let get_questions = warp::get() |
| 32 | .and(warp::path("questions")) |
| 33 | .and(warp::path::end()) |
| 34 | .and(warp::query()) |
| 35 | .and(store_filter.clone()) |
| 36 | .and_then(routes::question::get_questions) |
| 37 | .with(warp::trace(|info| { |
| 38 | tracing::info_span!( |
| 39 | "get_questions request", |
| 40 | method = %info.method(), |
| 41 | path = %info.path(), |
| 42 | id = %uuid::Uuid::new_v4(), |
| 43 | )}) |
| 44 | ); |
| 45 | |
| 46 | let update_question = warp::put() |
| 47 | .and(warp::path("questions")) |
| 48 | .and(warp::path::param::<String>()) |
| 49 | .and(warp::path::end()) |
| 50 | .and(store_filter.clone()) |
| 51 | .and(warp::body::json()) |
| 52 | .and_then(routes::question::update_question); |
| 53 | |
| 54 | let delete_question = warp::delete() |
| 55 | .and(warp::path("questions")) |
| 56 | .and(warp::path::param::<String>()) |
| 57 | .and(warp::path::end()) |
| 58 | .and(store_filter.clone()) |
| 59 | .and_then(routes::question::delete_question); |
| 60 | |
| 61 | let add_question = warp::post() |
| 62 | .and(warp::path("questions")) |
| 63 | .and(warp::path::end()) |
| 64 | .and(store_filter.clone()) |
| 65 | .and(warp::body::json()) |
| 66 | .and_then(routes::question::add_question); |
| 67 | |
| 68 | let add_answer = warp::post() |
| 69 | .and(warp::path("comments")) |