[feat] introduced server and cli crates with foundational HTTP server and CLI implementation, including routing, health check, and configuration setup

This commit is contained in:
2025-08-20 09:58:21 +02:00
parent 16167d18ff
commit d37daf02f6
12 changed files with 457 additions and 1275 deletions

View File

@@ -0,0 +1,15 @@
[package]
name = "cli"
version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = { workspace = true }
tokio = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
toml = { workspace = true }
dotenv = { workspace = true }
api = { path = "../api" }
server = { path = "../server" }

View File

@@ -0,0 +1,70 @@
use anyhow::Result;
use api::config::Cli;
use dotenv::dotenv;
use std::{env, net::SocketAddr, str::FromStr};
use tokio::signal;
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
let args: Vec<String> = env::args().collect();
match args.get(1).map(|s| s.as_str()) {
Some("serve") => serve(args).await,
Some("print-config") => print_config(),
_ => {
print_help();
Ok(())
}
}
}
fn print_help() {
eprintln!(
"Usage:
cli serve [--addr 0.0.0.0:8080]
cli print-config
Environment:
These may influence runtime behavior.
Notes:
- 'serve' runs the HTTP server.
- 'print-config' prints the default CLI configuration in JSON."
);
}
async fn serve(args: Vec<String>) -> Result<()> {
// naive flag parse: look for "--addr host:port"
let mut addr: SocketAddr = SocketAddr::from_str("127.0.0.1:8080")?;
let mut i = 2;
while i + 1 < args.len() {
if args[i] == "--addr" {
addr = SocketAddr::from_str(&args[i + 1])?;
i += 2;
} else {
i += 1;
}
}
let server_task = tokio::spawn(async move { server::start_server(addr).await });
// graceful shutdown via Ctrl+C
tokio::select! {
res = server_task => {
res??;
}
_ = signal::ctrl_c() => {
eprintln!("Shutting down...");
}
}
Ok(())
}
fn print_config() -> Result<()> {
let cfg = Cli::default();
let json = serde_json::to_string_pretty(&cfg)?;
println!("{json}");
Ok(())
}