octoproxy_client/
lib.rs

1mod backends;
2mod balance;
3pub mod command;
4mod config;
5mod connector;
6mod metric;
7mod proxy;
8
9use std::{path::PathBuf, sync::Arc};
10
11use anyhow::{Context, Result};
12use clap::Parser;
13use config::Config;
14use octoproxy_lib::build_rt;
15use tracing_subscriber::EnvFilter;
16
17use crate::{metric::listening_metric, proxy::listening_proxy};
18
19/// Client commandline args
20#[derive(Debug, Parser)]
21#[command(author, version, about, long_about = None)]
22pub struct Cmd {
23    /// http proxy server listening port
24    #[arg(short = 'l', long = "listen")]
25    pub(crate) listen_address: Option<String>,
26
27    /// config
28    #[arg(short = 'c', long = "config", default_value = "config.toml")]
29    pub(crate) config: PathBuf,
30}
31
32impl Cmd {
33    pub fn run(self) -> Result<()> {
34        let rt = build_rt();
35        rt.block_on(self.run_main())
36    }
37
38    /// client run function, distribute the listener service, and help selecting backend,
39    /// and spawn the each transmission action
40    async fn run_main(self) -> Result<()> {
41        let config = Config::new(self).context("Could not init config")?;
42
43        let env = EnvFilter::builder()
44            .with_default_directive(config.log_level.into())
45            .from_env_lossy();
46
47        tracing_subscriber::fmt().with_env_filter(env).init();
48        let config = Arc::new(config);
49
50        tokio::try_join!(listening_proxy(config.clone()), listening_metric(config))?;
51        Ok(())
52    }
53}