2021-05-05 14:30:11 +00:00
|
|
|
use actix_demo::{AppConfig, AppData, EnvConfig};
|
|
|
|
use diesel::{r2d2::ConnectionManager, SqliteConnection};
|
|
|
|
use env_logger::Env;
|
|
|
|
use io::ErrorKind;
|
2021-05-02 11:17:23 +00:00
|
|
|
use std::io;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
#[actix_web::main]
|
2021-05-05 14:30:11 +00:00
|
|
|
async fn main() -> io::Result<()> {
|
|
|
|
let _ = dotenv::dotenv().map_err(|err| {
|
2021-05-02 11:17:23 +00:00
|
|
|
io::Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("Failed to set up env: {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-05-05 14:30:11 +00:00
|
|
|
let _ = env_logger::try_init_from_env(
|
|
|
|
Env::default().filter("ACTIX_DEMO_RUST_LOG"),
|
|
|
|
)
|
|
|
|
.map_err(|err| {
|
2021-05-02 11:17:23 +00:00
|
|
|
io::Error::new(
|
|
|
|
ErrorKind::Other,
|
2021-05-05 14:30:11 +00:00
|
|
|
format!("Failed to set up env logger: {:?}", err),
|
2021-05-02 11:17:23 +00:00
|
|
|
)
|
|
|
|
})?;
|
2021-05-05 14:30:11 +00:00
|
|
|
|
|
|
|
let env_config = envy::prefixed("ACTIX_DEMO_")
|
|
|
|
.from_env::<EnvConfig>()
|
|
|
|
.map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("Failed to parse config: {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let connspec = &env_config.database_url;
|
2020-05-06 13:25:54 +00:00
|
|
|
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
2021-05-02 11:17:23 +00:00
|
|
|
let pool = r2d2::Pool::builder().build(manager).map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("Failed to create pool: {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-05-05 14:30:11 +00:00
|
|
|
let _ = {
|
2021-05-02 11:17:23 +00:00
|
|
|
let conn = &pool.get().map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("Failed to get connection: {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
2021-05-05 14:30:11 +00:00
|
|
|
let _ =
|
|
|
|
diesel_migrations::run_pending_migrations(conn).map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("Error running migrations: {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
};
|
2020-08-12 07:05:26 +00:00
|
|
|
|
2021-05-05 14:30:11 +00:00
|
|
|
let app_data = AppData {
|
|
|
|
config: AppConfig {
|
|
|
|
hash_cost: env_config.hash_cost,
|
|
|
|
},
|
2021-04-21 05:41:48 +00:00
|
|
|
pool: pool.clone(),
|
|
|
|
};
|
|
|
|
|
2021-05-05 14:30:11 +00:00
|
|
|
actix_demo::run(format!("{}:7800", env_config.http_host), app_data).await
|
2020-05-06 13:25:54 +00:00
|
|
|
}
|