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-04-21 09:36:31 -07:00
|
|
|
Note: The use of sleep(Duration::from_secs(2)); in db.rs is to make performance
|
2019-04-15 10:02:57 -05:00
|
|
|
improvement with parallelism more obvious.
|
2019-03-06 11:51:05 -08:00
|
|
|
*/
|
|
|
|
use std::io;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2019-03-07 14:50:29 -08:00
|
|
|
use actix_web::{middleware, web, App, Error as AWError, HttpResponse, HttpServer};
|
2019-12-07 23:59:24 +06:00
|
|
|
use futures::future::join_all;
|
|
|
|
use r2d2_sqlite::{self, SqliteConnectionManager};
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
mod db;
|
2019-12-07 23:59:24 +06:00
|
|
|
use db::{Pool, Queries};
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
/// Version 1: Calls 4 queries in sequential order, as an asynchronous handler
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn asyncio_weather(db: web::Data<Pool>) -> Result<HttpResponse, AWError> {
|
|
|
|
let result = vec![
|
|
|
|
db::execute(&db, Queries::GetTopTenHottestYears).await?,
|
|
|
|
db::execute(&db, Queries::GetTopTenColdestYears).await?,
|
|
|
|
db::execute(&db, Queries::GetTopTenHottestMonths).await?,
|
|
|
|
db::execute(&db, Queries::GetTopTenColdestMonths).await?,
|
|
|
|
];
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
Ok(HttpResponse::Ok().json(result))
|
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-12-07 23:59:24 +06:00
|
|
|
async fn parallel_weather(db: web::Data<Pool>) -> Result<HttpResponse, AWError> {
|
2018-05-26 17:05:12 -04:00
|
|
|
let fut_result = vec![
|
2019-12-07 23:59:24 +06:00
|
|
|
Box::pin(db::execute(&db, Queries::GetTopTenHottestYears)),
|
|
|
|
Box::pin(db::execute(&db, Queries::GetTopTenColdestYears)),
|
|
|
|
Box::pin(db::execute(&db, Queries::GetTopTenHottestMonths)),
|
|
|
|
Box::pin(db::execute(&db, Queries::GetTopTenColdestMonths)),
|
2018-07-16 12:36:53 +06:00
|
|
|
];
|
2019-12-07 23:59:24 +06:00
|
|
|
let result: Result<Vec<_>, _> = join_all(fut_result).await.into_iter().collect();
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
Ok(HttpResponse::Ok().json(result.map_err(AWError::from)?))
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> io::Result<()> {
|
2019-03-06 11:51:05 -08:00
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
2018-05-26 17:05:12 -04:00
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
// 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()
|
2019-12-07 23:59:24 +06:00
|
|
|
// store db pool as Data object
|
2019-03-26 04:29:00 +01:00
|
|
|
.data(pool.clone())
|
|
|
|
.wrap(middleware::Logger::default())
|
2019-03-06 15:51:56 -08:00
|
|
|
.service(
|
2019-12-07 23:59:24 +06:00
|
|
|
web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)),
|
2019-03-06 15:51:56 -08:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/parallel_weather")
|
2019-12-07 23:59:24 +06:00
|
|
|
.route(web::get().to(parallel_weather)),
|
2019-03-06 15:51:56 -08:00
|
|
|
)
|
2019-03-06 11:51:05 -08:00
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")?
|
2019-12-07 23:59:24 +06:00
|
|
|
.start()
|
|
|
|
.await
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|