Skip to content

Commit

Permalink
feat: server cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
matty-rose committed Oct 7, 2022
1 parent 7f1e24d commit b34e50d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 4 deletions.
115 changes: 115 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ path = "src/main.rs"
[dependencies]
anyhow = "1.0.65"
axum = "0.5.16"
clap = { version = "4.0.10", features = ["derive"] }
hyper = { version = "0.14.20", features = ["full"] }
serde = { version = "1.0.145", features = ["serde_derive"] }
tokio = { version = "1.21.2", features = ["full"] }
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Frontman

## TODO
- [ ] Read/parse proxy config from file?
- [x] Read/parse proxy config from file?
- [x] Server CLI args (config file path, listening port etc)
- [ ] Load balance requests to 1 or more origin servers (of the same backend), supporting IP/DNS
- [ ] Anyhow for result?
- [ ] proper error responses
Expand Down
11 changes: 11 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use clap::Parser;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct ServerArgs {
#[arg(short, long, default_value_t = String::from("frontman.toml"))]
pub config_path: String,

#[arg(short, long, default_value_t = 3000)]
pub port: u16,
}
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@
use std::net::SocketAddr;

use axum::{routing::any, Router};
use clap::Parser;
use tower_http::trace::TraceLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

mod args;
mod config;
mod proxy;

#[tokio::main]
async fn main() {
let args = args::ServerArgs::parse();

tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| "frontman=debug,tower_http=debug".into()),
))
.with(tracing_subscriber::fmt::layer())
.init();

let config_file =
std::fs::read_to_string("frontman.toml").expect("Should have been able to read the file");
let config_file = match std::fs::read_to_string(&args.config_path) {
Ok(file) => file,
Err(error) => panic!(
"Couldn't read config file at {}: {:?}",
args.config_path, error
),
};
let config: config::Config = toml::from_str(&config_file).unwrap();

tracing::debug!("Starting server with configuration: {:?}", config);
Expand All @@ -34,7 +43,7 @@ async fn main() {
.route("/*path", any(handler))
.layer(TraceLayer::new_for_http());

let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let addr = SocketAddr::from(([0, 0, 0, 0], args.port));
tracing::debug!("Server listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
Expand Down

0 comments on commit b34e50d

Please sign in to comment.