Skip to main content

coil_config/
app.rs

1use std::net::SocketAddr;
2
3use ipnet::IpNet;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7pub struct AppConfig {
8    pub name: String,
9    pub environment: Environment,
10}
11
12#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
13#[serde(rename_all = "snake_case")]
14pub enum Environment {
15    Development,
16    Staging,
17    Production,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
21pub struct ServerConfig {
22    pub bind: String,
23    #[serde(default)]
24    pub trusted_proxies: Vec<String>,
25    #[serde(default)]
26    pub max_body_bytes: Option<usize>,
27}
28
29impl ServerConfig {
30    pub fn trusts_forwarded_headers(&self, remote_addr: Option<&SocketAddr>) -> bool {
31        let Some(remote_addr) = remote_addr else {
32            return false;
33        };
34
35        self.trusted_proxies.iter().any(|trusted_proxy| {
36            trusted_proxy
37                .parse::<IpNet>()
38                .map(|network| network.contains(&remote_addr.ip()))
39                .unwrap_or(false)
40        })
41    }
42}