Use more async API from tokio

Fixes issue #4
This commit is contained in:
Elias Schriefer 2021-07-08 16:30:55 +02:00
parent cfed9b149c
commit fa9b77c335

View File

@ -1,8 +1,6 @@
use structopt::StructOpt; use structopt::StructOpt;
use std::{ use std::{
path::PathBuf, path::PathBuf,
fs::File,
io::prelude::*,
boxed::Box, boxed::Box,
error::Error, error::Error,
}; };
@ -20,6 +18,8 @@ use sqlx::{
}; };
use tokio::{ use tokio::{
sync::RwLock, sync::RwLock,
fs::File,
io::AsyncReadExt,
}; };
const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite"; const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite";
@ -67,7 +67,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
return Ok(()); return Ok(());
} }
let mut config = get_config(&options)?; let mut config = get_config(&options).await?;
if clap_matches.is_present("database-path") { if clap_matches.is_present("database-path") {
config.database_path = options.database_path; config.database_path = options.database_path;
@ -106,13 +106,13 @@ async fn load_sqlite_template(sqlite_handle: &mut SqliteConnection) -> sqlx::Res
)).execute(sqlite_handle).await )).execute(sqlite_handle).await
} }
fn get_config(options: &Options) -> std::io::Result<Config> { async fn get_config(options: &Options) -> std::io::Result<Config> {
if options.no_config { if options.no_config {
Ok(Config::default()) Ok(Config::default())
} else { } else {
let mut config_file = File::open(&options.config)?; let mut config_file = File::open(&options.config).await?;
let mut config_string = String::new(); let mut config_string = String::new();
config_file.read_to_string(&mut config_string)?; config_file.read_to_string(&mut config_string).await?;
toml::from_str(&config_string) toml::from_str(&config_string)
.map_err(|err| std::io::Error::new( .map_err(|err| std::io::Error::new(