Error handling for env variables in service

This commit is contained in:
2023-10-02 16:01:06 -04:00
parent a3462dad10
commit af065b3844
4 changed files with 35 additions and 9 deletions

View File

@@ -24,6 +24,10 @@ services:
container_name: weather-service
env_file:
- .env
environment:
- DATABASE_HOST: db
- SERVICE_HOST: service
- SERVICE_PORT: 5000
ports:
- "${SERVICE_PORT:-5000}:5000"
build:

View File

@@ -3,7 +3,7 @@ use diesel::{r2d2::ConnectionManager, PgConnection};
use serde::{Deserialize, Serialize};
use crate::diesel_migrations::MigrationHarness;
use lazy_static::lazy_static;
use log::{error, debug, info};
use log::{error, debug, info, warn};
use r2d2;
use std::env;
@@ -16,10 +16,21 @@ lazy_static! {
static ref POOL: Pool = {
let username = env::var("DATABASE_USER").expect("Database username is not set");
let password = env::var("DATABASE_PASSWORD").expect("Database password is not set");
let host = env::var("DATABASE_HOST").expect("Database host is not set");
let host = match env::var("DATABASE_HOST") {
Ok(h) => h,
Err(_) => {
warn!("Defaulting to DATABASE_HOST localhost");
"localhost".to_string()
}
};
let name = env::var("DATABASE_NAME").expect("Database name is not set");
// let port = env::var("DATABASE_PORT").expect("Database port is not set");
let port = 5432;
let port = match env::var("DATABASE_PORT") {
Ok(p) => p,
Err(_) => {
warn!("Defaulting to DATABASE_PORT 5432");
"5432".to_string()
}
};
let url = format!("postgres://{}:{}@{}:{}/{}", username, password, host, port, name);
let manager = ConnectionManager::<PgConnection>::new(url);
Pool::builder().test_on_check_out(true).build(manager).expect("Failed to create db pool")

View File

@@ -8,7 +8,7 @@ use actix_web::{App, HttpServer, middleware::Logger};
use dotenv::dotenv;
use env_logger::Env;
use listenfd::ListenFd;
use log::debug;
use log::{debug, warn};
mod airports;
mod auth;
@@ -44,9 +44,20 @@ async fn main() -> std::io::Result<()> {
server = match listenfd.take_tcp_listener(0)? {
Some(listener) => server.listen(listener)?,
None => {
let host = std::env::var("SERVICE_HOST").expect("Please set host in .env");
// let port = std::env::var("SERVICE_PORT").expect("Please set port in .env");
let port = 5000;
let host = match std::env::var("SERVICE_HOST") {
Ok(h) => h,
Err(_) => {
warn!("Defaulting to SERVICE_HOST localhost");
"localhost".to_string()
}
};
let port = match std::env::var("SERVICE_PORT") {
Ok(p) => p,
Err(_) => {
warn!("Defaulting to SERVICE_PORT 5000");
"5000".to_string()
}
};
debug!("Binding server to {}:{}", host, port);
server.bind(format!("{}:{}", host, port))?
}

View File

@@ -6,7 +6,6 @@ import { CartesianGrid, LabelList, Line, LineChart, XAxis, YAxis } from 'rechart
export default function SkyConditions({ metar }: { metar: Metar }) {
if (metar.sky_condition && metar.sky_condition.length > 0 && metar.sky_condition[0].sky_cover != 'CLR') {
let maxHeight = 5000;
const data: any = [
{
name: 'start'
@@ -15,6 +14,7 @@ export default function SkyConditions({ metar }: { metar: Metar }) {
name: 'end'
}
];
let maxHeight = 0;
metar.sky_condition.forEach((skyCondition, index) => {
data[0][index] = skyCondition.cloud_base_ft_agl;
data[1][index] = skyCondition.cloud_base_ft_agl;