2018-05-26 17:05:12 -04:00
|
|
|
/* Actix-Web Asynchronous Database Example
|
|
|
|
|
|
|
|
This project illustrates two examples:
|
|
|
|
|
|
|
|
1. An asynchronous handler that executes 4 queries in *sequential order*,
|
|
|
|
collecting the results and returning them as a single serialized json object
|
|
|
|
|
|
|
|
2. An asynchronous handler that executes 4 queries in *parallel*,
|
|
|
|
collecting the results and returning them as a single serialized json object
|
|
|
|
|
2019-03-06 11:51:05 -08:00
|
|
|
*/
|
|
|
|
use std::io;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2019-03-06 11:51:05 -08:00
|
|
|
use actix_web::{web, App, Error as AWError, HttpResponse, HttpServer, State};
|
2018-07-16 12:36:53 +06:00
|
|
|
use futures::future::{join_all, ok as fut_ok, Future};
|
2019-03-06 11:51:05 -08:00
|
|
|
use r2d2_sqlite;
|
2018-05-26 17:05:12 -04:00
|
|
|
use r2d2_sqlite::SqliteConnectionManager;
|
|
|
|
|
|
|
|
mod db;
|
2019-03-06 11:51:05 -08:00
|
|
|
use db::{Pool, Queries, WeatherAgg};
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
/// Version 1: Calls 4 queries in sequential order, as an asynchronous handler
|
2019-03-06 11:51:05 -08:00
|
|
|
fn asyncio_weather(
|
|
|
|
db: State<Pool>,
|
|
|
|
) -> impl Future<Item = HttpResponse, Error = AWError> {
|
2018-05-26 17:05:12 -04:00
|
|
|
let mut result: Vec<Vec<WeatherAgg>> = vec![];
|
|
|
|
|
2019-03-06 11:51:05 -08:00
|
|
|
db::execute(&db, Queries::GetTopTenHottestYears)
|
2018-07-16 12:36:53 +06:00
|
|
|
.from_err()
|
2018-05-26 17:05:12 -04:00
|
|
|
.and_then(move |res| {
|
2019-03-06 11:51:05 -08:00
|
|
|
result.push(res);
|
|
|
|
db::execute(&db, Queries::GetTopTenColdestYears)
|
2018-07-16 12:36:53 +06:00
|
|
|
.from_err()
|
2018-05-26 17:05:12 -04:00
|
|
|
.and_then(move |res| {
|
2019-03-06 11:51:05 -08:00
|
|
|
result.push(res);
|
|
|
|
db::execute(&db, Queries::GetTopTenHottestMonths)
|
2018-07-16 12:36:53 +06:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |res| {
|
2019-03-06 11:51:05 -08:00
|
|
|
result.push(res);
|
|
|
|
db::execute(&db, Queries::GetTopTenColdestMonths)
|
2018-07-16 12:36:53 +06:00
|
|
|
.from_err()
|
|
|
|
.and_then(move |res| {
|
2019-03-06 11:51:05 -08:00
|
|
|
result.push(res);
|
2018-07-16 12:36:53 +06:00
|
|
|
fut_ok(result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-05-26 17:05:12 -04:00
|
|
|
})
|
2018-07-16 12:36:53 +06:00
|
|
|
.and_then(|res| Ok(HttpResponse::Ok().json(res)))
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Version 2: Calls 4 queries in parallel, as an asynchronous handler
|
|
|
|
/// Returning Error types turn into None values in the response
|
2019-03-06 11:51:05 -08:00
|
|
|
fn parallel_weather(
|
|
|
|
db: State<Pool>,
|
|
|
|
) -> impl Future<Item = HttpResponse, Error = AWError> {
|
2018-05-26 17:05:12 -04:00
|
|
|
let fut_result = vec![
|
2019-03-06 11:51:05 -08:00
|
|
|
Box::new(db::execute(&db, Queries::GetTopTenHottestYears)),
|
|
|
|
Box::new(db::execute(&db, Queries::GetTopTenColdestYears)),
|
|
|
|
Box::new(db::execute(&db, Queries::GetTopTenHottestMonths)),
|
|
|
|
Box::new(db::execute(&db, Queries::GetTopTenColdestMonths)),
|
2018-07-16 12:36:53 +06:00
|
|
|
];
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
join_all(fut_result)
|
|
|
|
.map_err(AWError::from)
|
2019-03-06 11:51:05 -08:00
|
|
|
.map(|result| HttpResponse::Ok().json(result))
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
2019-03-06 11:51:05 -08:00
|
|
|
fn main() -> io::Result<()> {
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
2018-05-26 17:05:12 -04:00
|
|
|
env_logger::init();
|
2019-03-06 11:51:05 -08:00
|
|
|
let sys = actix_rt::System::new("parallel_db_example");
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
// Start N db executor actors (N = number of cores avail)
|
|
|
|
let manager = SqliteConnectionManager::file("weather.db");
|
|
|
|
let pool = Pool::new(manager).unwrap();
|
|
|
|
|
|
|
|
// Start http server
|
2019-03-06 11:51:05 -08:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.state(pool.clone())
|
2018-05-26 17:05:12 -04:00
|
|
|
// enable logger
|
2019-03-06 11:51:05 -08:00
|
|
|
// .middleware(middleware::Logger::default())
|
2019-03-06 15:51:56 -08:00
|
|
|
.service(
|
|
|
|
web::resource("/asyncio_weather")
|
|
|
|
.route(web::get().to_async(asyncio_weather)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/parallel_weather")
|
|
|
|
.route(web::get().to_async(parallel_weather)),
|
|
|
|
)
|
2019-03-06 11:51:05 -08:00
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.start();
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
println!("Started http server: 127.0.0.1:8080");
|
2019-03-06 11:51:05 -08:00
|
|
|
sys.run()
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|