66 lines
1.6 KiB
Rust
66 lines
1.6 KiB
Rust
use structopt::StructOpt;
|
|
use std::{
|
|
boxed::Box,
|
|
error::Error,
|
|
};
|
|
use tokio::{
|
|
sync::RwLock,
|
|
fs::File,
|
|
io::AsyncReadExt,
|
|
};
|
|
use crate::{
|
|
cli::Options,
|
|
config::Config,
|
|
};
|
|
|
|
mod cli;
|
|
mod config;
|
|
#[cfg(feature = "sqlite")]
|
|
mod sqlite;
|
|
|
|
const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite";
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn Error>> {
|
|
let clap_matches = Options::clap().get_matches();
|
|
let options = Options::from_clap(&clap_matches);
|
|
|
|
if options.generate_config {
|
|
println!("{}", toml::to_string_pretty(&Config::default())?);
|
|
return Ok(());
|
|
}
|
|
|
|
let mut config = get_config(&options).await?;
|
|
|
|
if clap_matches.is_present("database-path") {
|
|
config.database_path = options.database_path;
|
|
}
|
|
|
|
#[cfg(debug_assertions)]
|
|
println!("{:?}", config);
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
let mut sqlite_connection = RwLock::new(sqlite::connect(&config.database_path).await?);
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
sqlite::load_template(sqlite_connection.get_mut()).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn get_config(options: &Options) -> std::io::Result<Config> {
|
|
if options.no_config {
|
|
Ok(Config::default())
|
|
} else {
|
|
let mut config_file = File::open(&options.config).await?;
|
|
let mut config_string = String::new();
|
|
config_file.read_to_string(&mut config_string).await?;
|
|
|
|
toml::from_str(&config_string)
|
|
.map_err(|err| std::io::Error::new(
|
|
std::io::ErrorKind::InvalidData,
|
|
err
|
|
)
|
|
)
|
|
}
|
|
} |