HomeDisk/core/src/main.rs

93 lines
2.4 KiB
Rust
Raw Normal View History

use std::{fs::File, path::Path};
use homedisk_database::Database;
2022-06-08 19:16:12 +00:00
use homedisk_server::serve_http;
use homedisk_types::config::Config;
use log::{info, warn};
pub const DATABASE_FILE: &str = "homedisk.db";
2022-04-16 18:19:38 +00:00
#[tokio::main]
async fn main() {
2022-06-08 19:16:12 +00:00
// init better_panic
better_panic::install();
// init logger
init_logger().expect("init logger");
2022-04-16 18:19:38 +00:00
2022-06-07 20:36:26 +00:00
// parse config
let config = Config::parse().expect("parse config");
2022-04-16 19:22:01 +00:00
2022-06-07 20:36:26 +00:00
// open database connection
let db =
// if database file doesn't exists create it
if !Path::new(DATABASE_FILE).exists() {
warn!("Database file doesn't exists.");
info!("Creating database file...");
// create an empty database file
File::create(DATABASE_FILE).expect("create a database file");
// open database file
let db = Database::open(DATABASE_FILE)
.await
.expect("open database file");
// create tables in the database
db.create_tables()
.await
.expect("create tables in the database");
db
}
// if database file exists
else {
// open database connection
let db = Database::open(DATABASE_FILE)
.await
.expect("open database file");
db
};
// change the type from Vec<String> to Vec<HeaderValue> so that the http server can correctly detect CORS hosts
let origins = config
.http
.cors
.iter()
2022-06-07 20:36:26 +00:00
.map(|e| e.parse().expect("parse CORS hosts"))
.collect();
2022-06-08 19:16:12 +00:00
// format host ip and port
let host = format!(
"{host}:{port}",
host = config.http.host,
port = config.http.port
);
2022-06-07 20:36:26 +00:00
// start http server
serve_http(host, origins, db, config)
.await
.expect("start http server");
2022-06-08 19:16:12 +00:00
}
fn init_logger() -> anyhow::Result<()> {
use log::LevelFilter;
use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger};
CombinedLogger::init(vec![
TermLogger::new(
LevelFilter::Debug,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
),
WriteLogger::new(
LevelFilter::Info,
Config::default(),
File::create("logs.log")?,
2022-06-08 19:16:12 +00:00
),
])?;
2022-06-07 20:36:26 +00:00
Ok(())
2022-04-16 18:19:38 +00:00
}