Skip to content

Commit 42ebb08

Browse files
feat(config): add dotenv for environment variables (dev-sys-do#8)
* refactor: remove unused import of E constant * feat(dependencies): add dotenv crate for environment variable management * feat(gitignore): add entry to ignore environment variable files * feat(config): add configuration struct and load from environment variables
1 parent 40c0237 commit 42ebb08

File tree

8 files changed

+47
-3
lines changed

8 files changed

+47
-3
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FERRIS_BASE_PATH=./public
2+
FERRIS_PORT=9000
3+
FERRIS_HOST=0.0.0.0

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ target
2626
/target
2727

2828
public/
29+
30+
.*env

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ async-trait = "0.1.89"
88
clap = { version = "4.5.50", features = ["derive"] }
99
tokio = { version = "1", features = ["net", "rt-multi-thread", "rt", "fs", "io-util", "sync", "macros"] }
1010
anyhow = "1.0"
11+
dotenv = "0.15.0"
1112

1213
[[bin]]
1314
name = "cli"

src/application/config.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[derive(Debug)]
2+
pub struct Config {
3+
pub ferris_base_path: String,
4+
pub ferris_port: u16,
5+
pub ferris_host: String,
6+
}
7+
8+
impl Config {
9+
pub fn from_env() -> Self {
10+
let ferris_base_path =
11+
std::env::var("FERRIS_BASE_PATH").unwrap_or_else(|_| "./public".to_string());
12+
let ferris_port = std::env::var("FERRIS_PORT")
13+
.unwrap_or_else(|_| "9000".to_string())
14+
.parse()
15+
.expect("FERRIS_PORT must be a valid u16");
16+
let ferris_host = std::env::var("FERRIS_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
17+
Config {
18+
ferris_base_path,
19+
ferris_port,
20+
ferris_host,
21+
}
22+
}
23+
}

src/application/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod config;
12
pub mod ferrisshare_state;

src/core/domain/network/services.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::f32::consts::E;
21
use std::io::Error;
32
use std::sync::Arc;
43
use std::sync::atomic::AtomicBool;

src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use dotenv::dotenv;
12
use std::sync::Arc;
23

34
use tokio::{net::TcpStream, sync::mpsc};
45

56
use ferrisshare::{
7+
application::config::Config,
68
core::domain::{
79
command::services::CommandServiceImpl,
810
network::{ports::NetworkService as _, services::NetworkServiceImpl},
@@ -12,9 +14,12 @@ use ferrisshare::{
1214

1315
#[tokio::main]
1416
async fn main() -> tokio::io::Result<()> {
17+
dotenv().ok();
18+
let cfg: Config = Config::from_env();
19+
1520
let (tx, rx) = mpsc::channel::<TcpStream>(1);
1621

17-
let storage_repo = FSStorageRepository::new("./public/".into());
22+
let storage_repo = FSStorageRepository::new(cfg.ferris_base_path);
1823

1924
let command_service = CommandServiceImpl::new(storage_repo);
2025
let network_service = NetworkServiceImpl::new(command_service);
@@ -36,7 +41,10 @@ async fn main() -> tokio::io::Result<()> {
3641

3742
if let Err(e) = ferrisshare_state
3843
.network_service
39-
.listener("127.0.0.1:9000", tx)
44+
.listener(
45+
format!("{}:{}", cfg.ferris_host, cfg.ferris_port).as_str(),
46+
tx,
47+
)
4048
.await
4149
{
4250
eprintln!("Listener error: {:?}", e);

0 commit comments

Comments
 (0)