1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

Update db/basic, db/sqlx_todo and db/r2d2 to v4 (#506)

This commit is contained in:
Luca Palmieri
2022-01-29 16:25:46 +00:00
committed by GitHub
parent 6e3ae0fd19
commit 8b5de155bd
11 changed files with 225 additions and 165 deletions

View File

@ -2,16 +2,16 @@
name = "async_db"
version = "2.0.0"
authors = ["Darin Gordon <dkcdkg@gmail.com>"]
edition = "2018"
edition = "2021"
[dependencies]
actix-web = "3"
env_logger = "0.8"
actix-web = "4.0.0-beta.21"
env_logger = "0.9.0"
failure = "0.1.7"
futures = "0.3.1"
num_cpus = "1.13"
r2d2 = "0.8.2"
r2d2_sqlite = "0.14"
rusqlite = "0.21"
r2d2_sqlite = "0.18.0"
rusqlite = "0.25.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -1,7 +1,6 @@
use actix_web::{web, Error as AWError};
use actix_web::{error::InternalError, http::StatusCode, web};
use failure::Error;
use futures::{Future, TryFutureExt};
use rusqlite::{Statement, NO_PARAMS};
use rusqlite::Statement;
use serde::{Deserialize, Serialize};
use std::{thread::sleep, time::Duration};
@ -23,10 +22,10 @@ pub enum Queries {
GetTopTenColdestMonths,
}
pub fn execute(
pub async fn execute(
pool: &Pool,
query: Queries,
) -> impl Future<Output = Result<Vec<WeatherAgg>, AWError>> {
) -> Result<Vec<WeatherAgg>, InternalError<Error>> {
let pool = pool.clone();
web::block(move || {
// simulate an expensive query, see comments at top of main.rs
@ -40,7 +39,9 @@ pub fn execute(
};
result.map_err(Error::from)
})
.map_err(AWError::from)
.await
.unwrap()
.map_err(|e| InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))
}
fn get_hottest_years(conn: Connection) -> WeatherAggResult {
@ -73,7 +74,7 @@ fn get_coldest_years(conn: Connection) -> WeatherAggResult {
fn get_rows_as_annual_agg(mut statement: Statement) -> WeatherAggResult {
statement
.query_map(NO_PARAMS, |row| {
.query_map([], |row| {
Ok(WeatherAgg::AnnualAgg {
year: row.get(0)?,
total: row.get(1)?,
@ -112,7 +113,7 @@ fn get_coldest_months(conn: Connection) -> WeatherAggResult {
fn get_rows_as_month_agg(mut statement: Statement) -> WeatherAggResult {
statement
.query_map(NO_PARAMS, |row| {
.query_map([], |row| {
Ok(WeatherAgg::MonthAgg {
year: row.get(0)?,
month: row.get(1)?,

View File

@ -61,7 +61,7 @@ async fn main() -> io::Result<()> {
HttpServer::new(move || {
App::new()
// store db pool as Data object
.data(pool.clone())
.app_data(web::Data::new(pool.clone()))
.wrap(middleware::Logger::default())
.service(
web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)),