-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathmain.rs
43 lines (38 loc) · 1.32 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use sqlpage::{
app_config,
webserver::{self, Database},
AppState,
};
#[actix_web::main]
async fn main() {
init_logging();
if let Err(e) = start().await {
log::error!("{:?}", e);
std::process::exit(1);
}
}
async fn start() -> anyhow::Result<()> {
let app_config = app_config::load_from_cli()?;
let db = Database::init(&app_config).await?;
webserver::database::migrations::apply(&app_config, &db).await?;
let state = AppState::init_with_db(&app_config, db).await?;
log::debug!("Starting server...");
webserver::http::run_server(&app_config, state).await?;
log::info!("Server stopped gracefully. Goodbye!");
Ok(())
}
fn init_logging() {
let load_env = dotenvy::dotenv();
let env =
env_logger::Env::new().default_filter_or("sqlpage=info,actix_web::middleware::logger=info");
let mut logging = env_logger::Builder::from_env(env);
logging.format_timestamp_millis();
logging.init();
match load_env {
Ok(path) => log::info!("Loaded environment variables from {path:?}"),
Err(dotenvy::Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => log::debug!(
"No .env file found, using only environment variables and configuration files"
),
Err(e) => log::error!("Error loading .env file: {}", e),
}
}