From e844d8d3e2b25bbb6db71f083ceb4da017729b20 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 21 Oct 2021 00:25:35 +0300 Subject: [PATCH] chore: clean web example --- examples/web/src/lib.rs | 2 +- examples/web/src/main.rs | 5 ++++- examples/web/src/rest/server.rs | 31 ++++++++++++++----------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/web/src/lib.rs b/examples/web/src/lib.rs index 9ed05f1..e1b413d 100644 --- a/examples/web/src/lib.rs +++ b/examples/web/src/lib.rs @@ -13,5 +13,5 @@ pub mod config; pub mod error; mod app; -mod db; +pub mod db; pub mod rest; diff --git a/examples/web/src/main.rs b/examples/web/src/main.rs index 94fbb2f..b9bc792 100644 --- a/examples/web/src/main.rs +++ b/examples/web/src/main.rs @@ -1,10 +1,13 @@ use web_example::config::load_env_config; +use web_example::db::persistence::create_postgres_pool; use web_example::error::StdResult; use web_example::rest::server::start_server; #[tokio::main] async fn main() -> StdResult<()> { load_env_config(); - start_server().await?; + + let pool = create_postgres_pool().await; + start_server(pool).await?; Ok(()) } diff --git a/examples/web/src/rest/server.rs b/examples/web/src/rest/server.rs index 6c1ffd4..dfc35b7 100644 --- a/examples/web/src/rest/server.rs +++ b/examples/web/src/rest/server.rs @@ -1,6 +1,6 @@ use super::server_utils; use crate::config; -use crate::db::persistence::create_postgres_pool; +use crate::db::persistence::PostgresPool; use crate::error::SyncStdError; use crate::rest::context::{RestGlobalContext, RestReqContext}; use crate::rest::prelude::*; @@ -10,16 +10,7 @@ use hyper::service::{make_service_fn, service_fn}; use hyper::Server; use std::sync::Arc; -/// Waits for the Ctrl+C signal for graceful shutdown backend -async fn shutdown_signal() { - tokio::signal::ctrl_c() - .await - .expect("failed to install CTRL+C signal handler"); -} - -pub async fn start_server() -> StdResult<()> { - let pool = create_postgres_pool().await; - // let persistence = ood_persistence::bb8_postgres::new(&pool); +pub async fn start_server(pool: PostgresPool) -> StdResult<()> { let context = Arc::new(RestGlobalContext { pool }); let new_service = make_service_fn(move |_| { @@ -42,11 +33,11 @@ pub async fn start_server() -> StdResult<()> { Ok(()) } -fn split_request_uri_path(uri_path: &str) -> Vec<&str> { - uri_path - .split('/') - .filter(|part| !part.is_empty()) - .collect() +/// Waits for the Ctrl+C signal for graceful shutdown backend +async fn shutdown_signal() { + tokio::signal::ctrl_c() + .await + .expect("failed to install CTRL+C signal handler"); } async fn process_request( @@ -70,7 +61,6 @@ async fn process_request( StatusCode::INTERNAL_SERVER_ERROR, RestResponseData::::error(REST_INTERNAL_SERVER_ERROR), ) - // TODO(pleshevskiy): investigate why `Send` is not implemented .unwrap(), Ok(res) => res, }; @@ -93,3 +83,10 @@ async fn process_request( Ok(res) } + +fn split_request_uri_path(uri_path: &str) -> Vec<&str> { + uri_path + .split('/') + .filter(|part| !part.is_empty()) + .collect() +}