Skip to main content

micromegas/
config.rs

1//! CLI args shared by every long-running server binary.
2
3use anyhow::{Result, bail};
4use micromegas_tracing::prelude::warn;
5use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
6use std::time::Duration;
7
8pub const DEFAULT_INGESTION_LISTEN_ADDR: SocketAddr =
9    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081);
10pub const DEFAULT_FLIGHTSQL_LISTEN_ADDR: SocketAddr =
11    SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 50051);
12pub const DEFAULT_WEB_LISTEN_ADDR: SocketAddr =
13    SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 3000);
14
15/// CLI args shared by every long-running server binary.
16/// Flatten into a binary's clap struct with `#[command(flatten)]`.
17#[derive(clap::Args, Debug, Clone)]
18pub struct CommonServerArgs {
19    /// Seconds to wait for in-flight work to complete after SIGTERM.
20    #[arg(
21        long,
22        default_value = "25",
23        env = "MICROMEGAS_SHUTDOWN_GRACE_PERIOD_SECONDS"
24    )]
25    pub shutdown_grace_period_seconds: u64,
26}
27
28impl CommonServerArgs {
29    pub fn grace(&self) -> Duration {
30        Duration::from_secs(self.shutdown_grace_period_seconds)
31    }
32}
33
34/// HTTP ingestion listen address. Flatten into a binary's clap struct.
35#[derive(clap::Args, Debug, Clone)]
36pub struct IngestionListenArgs {
37    /// HTTP ingestion listen address
38    #[arg(
39        long,
40        env = "MICROMEGAS_INGESTION_LISTEN_ADDR",
41        default_value_t = DEFAULT_INGESTION_LISTEN_ADDR,
42        alias = "listen-endpoint-http"
43    )]
44    pub ingestion_listen_addr: SocketAddr,
45}
46
47/// FlightSQL listen address. Flatten into a binary's clap struct.
48#[derive(clap::Args, Debug, Clone)]
49pub struct FlightSqlListenArgs {
50    /// FlightSQL listen address
51    #[arg(
52        long,
53        env = "MICROMEGAS_FLIGHTSQL_LISTEN_ADDR",
54        default_value_t = DEFAULT_FLIGHTSQL_LISTEN_ADDR
55    )]
56    pub flightsql_listen_addr: SocketAddr,
57}
58
59/// Web server listen address. Call [`WebListenArgs::resolve`] to get the address.
60#[derive(clap::Args, Debug, Clone)]
61pub struct WebListenArgs {
62    /// Web server listen address (default 0.0.0.0:3000)
63    #[arg(long, env = "MICROMEGAS_WEB_LISTEN_ADDR")]
64    pub web_listen_addr: Option<SocketAddr>,
65
66    /// Deprecated: use --web-listen-addr
67    #[arg(short, long, env = "MICROMEGAS_PORT", hide = true)]
68    pub port: Option<u16>,
69}
70
71impl WebListenArgs {
72    pub fn resolve(&self) -> Result<SocketAddr> {
73        match (self.web_listen_addr, self.port) {
74            (Some(_), Some(_)) => bail!(
75                "both --web-listen-addr/MICROMEGAS_WEB_LISTEN_ADDR and the deprecated \
76                 --port/MICROMEGAS_PORT are set; remove --port/MICROMEGAS_PORT"
77            ),
78            (Some(addr), None) => Ok(addr),
79            (None, Some(port)) => {
80                warn!(
81                    "--port/MICROMEGAS_PORT is deprecated, use --web-listen-addr/MICROMEGAS_WEB_LISTEN_ADDR"
82                );
83                Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port))
84            }
85            (None, None) => Ok(DEFAULT_WEB_LISTEN_ADDR),
86        }
87    }
88}
89
90/// Address a same-host client dials to reach `listen`: an unspecified IP
91/// (0.0.0.0 / ::) maps to the loopback of the same family.
92pub fn local_connect_addr(listen: SocketAddr) -> SocketAddr {
93    let ip = match listen.ip() {
94        IpAddr::V4(ip) if ip.is_unspecified() => IpAddr::V4(Ipv4Addr::LOCALHOST),
95        IpAddr::V6(ip) if ip.is_unspecified() => IpAddr::V6(Ipv6Addr::LOCALHOST),
96        ip => ip,
97    };
98    SocketAddr::new(ip, listen.port())
99}