Skip to content

Commit

Permalink
Add configuration via .env.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksbgbg committed Feb 3, 2024
1 parent c807b3b commit ab3c6ca
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend-rs/.env.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[config.app]
host = [0, 0, 0, 0]
port = 8601
95 changes: 95 additions & 0 deletions backend-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ edition = "2021"

[dependencies]
axum = "0.7.4"
serde = { version = "1.0.196", features = ["derive"] }
tokio = { version = "1.36.0", features = ["rt-multi-thread"] }
toml-env = "1.1.1"
tower-http = { version = "0.5.1", features = ["trace"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
22 changes: 22 additions & 0 deletions backend-rs/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};
use toml_env::Args;

#[derive(Serialize, Deserialize)]
pub struct Config {
pub app: App,
}

#[derive(Serialize, Deserialize)]
pub struct App {
pub host: [u8; 4],
pub port: u16,
}

pub fn load() -> Config {
toml_env::initialize(Args {
config_variable_name: "config",
..Default::default()
})
.expect("could not initialize toml_env")
.expect("could not load config")
}
6 changes: 5 additions & 1 deletion backend-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod config;

use axum::{routing, Router};
use std::net::SocketAddr;
use tokio::net::TcpListener;
Expand All @@ -6,6 +8,8 @@ use tracing::{info, Level};

#[tokio::main]
async fn main() {
let config = config::load();

tracing_subscriber::fmt()
.with_target(false)
.compact()
Expand All @@ -19,7 +23,7 @@ async fn main() {
.on_response(DefaultOnResponse::new().level(Level::INFO)),
);

let listener = TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], 8601)))
let listener = TcpListener::bind(SocketAddr::from((config.app.host, config.app.port)))
.await
.expect("could not bind");

Expand Down

0 comments on commit ab3c6ca

Please sign in to comment.