Skip to main content

nexus_common/config/
api.rs

1use super::file::ConfigLoader;
2use super::{default_stack, DaemonConfig, StackConfig};
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use std::{fmt::Debug, net::SocketAddr};
6
7pub const NAME: &str = "nexus.api";
8
9pub const DEFAULT_HOST: [u8; 4] = [127, 0, 0, 1];
10pub const DEFAULT_PORT: u16 = 8080;
11
12/// Configuration settings for the Nexus Watcher service
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct ApiConfig {
15    pub name: String,
16    pub public_addr: SocketAddr,
17    #[serde(default = "default_stack")]
18    pub stack: StackConfig,
19}
20
21impl Default for ApiConfig {
22    fn default() -> Self {
23        Self {
24            name: String::from(NAME),
25            public_addr: SocketAddr::from((DEFAULT_HOST, DEFAULT_PORT)),
26            stack: StackConfig::default(),
27        }
28    }
29}
30
31/// Converts a [`DaemonConfig`] into an [`ApiConfig`], extracting only the API-related settings
32/// and the shared application stack
33impl From<DaemonConfig> for ApiConfig {
34    fn from(daemon_config: DaemonConfig) -> Self {
35        ApiConfig {
36            name: daemon_config.api.name,
37            public_addr: daemon_config.api.public_addr,
38            stack: daemon_config.stack,
39        }
40    }
41}
42
43#[async_trait]
44impl ConfigLoader<ApiConfig> for ApiConfig {}