error handling improvements
This commit is contained in:
parent
226d3cde4f
commit
efbfe3e0fe
66
src/main.rs
66
src/main.rs
@ -16,6 +16,8 @@ use rand::Rng;
|
|||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::r2d2::{self, ConnectionManager};
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
use listenfd::ListenFd;
|
use listenfd::ListenFd;
|
||||||
|
use std::io;
|
||||||
|
use std::io::ErrorKind;
|
||||||
use types::DbPool;
|
use types::DbPool;
|
||||||
|
|
||||||
mod actions;
|
mod actions;
|
||||||
@ -41,27 +43,55 @@ pub struct AppConfig {
|
|||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "debug");
|
std::env::set_var("RUST_LOG", "debug");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().map_err(|err| {
|
||||||
|
io::Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("Failed to set up env: {:?}", err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
// let _basic_auth_middleware =
|
// let _basic_auth_middleware =
|
||||||
// HttpAuthentication::basic(utils::auth::validator);
|
// HttpAuthentication::basic(utils::auth::validator);
|
||||||
|
|
||||||
// set up database connection pool
|
// set up database connection pool
|
||||||
let connspec =
|
let connspec = std::env::var("DATABASE_URL").map_err(|err| {
|
||||||
std::env::var("DATABASE_URL").expect("DATABASE_URL NOT FOUND");
|
io::Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("Database url is not set: {:?}", err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
||||||
let pool = r2d2::Pool::builder()
|
let pool = r2d2::Pool::builder().build(manager).map_err(|err| {
|
||||||
.build(manager)
|
io::Error::new(
|
||||||
.expect("Failed to create pool.");
|
ErrorKind::Other,
|
||||||
|
format!("Failed to create pool: {:?}", err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
diesel_migrations::run_pending_migrations(&pool.get().unwrap())
|
{
|
||||||
.expect("Error running migrations");
|
let conn = &pool.get().map_err(|err| {
|
||||||
|
io::Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("Failed to get connection: {:?}", err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
diesel_migrations::run_pending_migrations(conn).map_err(|err| {
|
||||||
|
io::Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("Error running migrations: {:?}", err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
let hash_cost = std::env::var("HASH_COST")
|
let hash_cost = std::env::var("HASH_COST")
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
.and_then(|x| x.parse::<u32>().map_err(|e| e.to_string()))
|
.and_then(|x| x.parse::<u32>().map_err(|e| e.to_string()))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|err| {
|
||||||
info!("Error parsing hash cost env variable, or it is not set. Using default cost of 8");
|
info!(
|
||||||
|
"Error getting hash cost: {:?}. Using default cost of 8",
|
||||||
|
err
|
||||||
|
);
|
||||||
8
|
8
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -74,8 +104,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
// user_service: &user_service,
|
// user_service: &user_service,
|
||||||
// };
|
// };
|
||||||
|
|
||||||
let addr = std::env::var("BIND_ADDRESS").expect("BIND ADDRESS NOT FOUND");
|
let addr =
|
||||||
info!("Starting server {}", addr);
|
std::env::var("BIND_ADDRESS").unwrap_or("127.0.0.1:7800".to_owned());
|
||||||
|
info!("Starting server at {}", addr);
|
||||||
let private_key = rand::thread_rng().gen::<[u8; 32]>();
|
let private_key = rand::thread_rng().gen::<[u8; 32]>();
|
||||||
let app = move || {
|
let app = move || {
|
||||||
App::new()
|
App::new()
|
||||||
@ -103,11 +134,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
};
|
};
|
||||||
// HttpServer::new(app).bind(addr)?.run().await
|
// HttpServer::new(app).bind(addr)?.run().await
|
||||||
let mut listenfd = ListenFd::from_env();
|
let mut listenfd = ListenFd::from_env();
|
||||||
let mut server = HttpServer::new(app);
|
let server = HttpServer::new(app);
|
||||||
server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
|
let server = match listenfd.take_tcp_listener(0)? {
|
||||||
server.listen(l)?
|
Some(l) => server.listen(l),
|
||||||
} else {
|
None => server.bind(addr),
|
||||||
server.bind(addr)?
|
}?;
|
||||||
};
|
|
||||||
server.run().await
|
server.run().await
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user