Skip to content

Commit e539a8d

Browse files
committed
refactor: Simplify configuration loading and update server port handling
- Replace manual file reading in the load function with std::fs::read_to_string for improved clarity. - Update the main function to dynamically set the server port from the environment variable, defaulting to 8000 if not specified.
1 parent 3f7dff9 commit e539a8d

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/config.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use clap::Parser;
22
use serde::{Deserialize, Serialize};
33
use std::collections::HashMap;
44
use std::fmt;
5-
use std::fs::File;
6-
use std::io::Read;
75

86
#[derive(Deserialize, Debug, Clone)]
97
pub struct Config {
@@ -73,11 +71,7 @@ impl Args {
7371
}
7472

7573
fn load(file_path: &str) -> Result<Config, Box<dyn std::error::Error>> {
76-
let mut f = File::open(file_path)?;
77-
78-
let mut contents = String::new();
79-
f.read_to_string(&mut contents)?;
80-
74+
let contents = std::fs::read_to_string(file_path)?;
8175
let config: Config = serde_yaml::from_str(&contents)?;
8276

8377
Ok(config)

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2121
#[tokio::main]
2222
async fn main() {
2323
// initialize tracing
24-
config::Args::parse();
2524
let args = config::Args::parse();
2625
let config = args.load_config().expect("load config error");
2726

@@ -74,8 +73,15 @@ async fn main() {
7473
),
7574
);
7675

77-
// run our app with hyper, listening globally on port 3000
78-
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
76+
let port = std::env::var("PORT")
77+
.ok()
78+
.and_then(|p| p.parse().ok())
79+
.unwrap_or(8000);
80+
81+
// run our app with hyper, listening globally on port 8000
82+
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port)))
83+
.await
84+
.unwrap();
7985
axum::serve(
8086
listener,
8187
app.into_make_service_with_connect_info::<SocketAddr>(),

0 commit comments

Comments
 (0)