main is now setup to handle pipeline environments and regular .env filled environments

This commit is contained in:
shockrah 2020-07-31 21:49:07 -07:00
parent ab981f68d0
commit c8055e07cc

View File

@ -110,18 +110,42 @@ async fn shutdown_signal() {
.expect("Failed to capture ctrl-c signal");
}
#[tokio::main]
async fn main() {
dotenv().ok();
println!("Servering on localhost:8888");
let addr = SocketAddr::from(([127,0,0,1], 8888));
async fn main() -> Result<(), u16>{
const NO_ERR: u16 = 0;
const CONFIG_ERR: u16 = 1;
const SHUTDOWN_ERR: u16 = 2;
let service = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(main_responder))
});
let mut main_ret: u16 = 0;
// setting up environment variables
let d_result = dotenv();
if let Err(_d) = d_result {
// we may be on a pipeline/prod environment so .env may not be there
if let Err(_e) = env::var("DATABASE_URL") {
main_ret |= CONFIG_ERR;
}
}
if main_ret == NO_ERR {
println!("Servering on localhost:8888");
let addr = SocketAddr::from(([127,0,0,1], 8888));
let service = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(main_responder))
});
let server = Server::bind(&addr).serve(service);
let graceful_shutdown = server.with_graceful_shutdown(shutdown_signal());
let server = Server::bind(&addr).serve(service);
let graceful_shutdown = server.with_graceful_shutdown(shutdown_signal());
if let Err(e) = graceful_shutdown.await {
eprintln!("Server shutdown error: {}", e);
if let Err(e) = graceful_shutdown.await {
main_ret |= SHUTDOWN_ERR;
eprintln!("Server shutdown error: {}", e);
}
}
if main_ret != 0 {
// dumb as heck loggin method here
if main_ret & CONFIG_ERR != 0 {println!("ERROR: Config was not setup properly => Missing {{DATABASE_URL}}");}
if main_ret & SHUTDOWN_ERR != 0 {println!("ERROR: Couldn't shutdown gracefully");}
Err(main_ret)
}
else {
Ok(())
}
}