torrust_index/config/v2/
net.rs

1use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2
3use serde::{Deserialize, Serialize};
4use url::Url;
5
6use crate::config::Tsl;
7
8/// The the base URL for the API.
9///
10/// NOTICE: that `port` and por in `base_url` does not necessarily match because
11/// the application migth be running behind a proxy. The local socket could be
12/// bound to, for example, port 80 but the application could be exposed publicly
13/// via port 443, which is a very common setup.
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
15pub struct Network {
16    /// The base URL for the API. For example: `http://localhost`.
17    /// If not set, the base URL will be inferred from the request.
18    #[serde(default = "Network::default_base_url")]
19    pub base_url: Option<Url>,
20
21    /// The address the tracker will bind to.
22    /// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
23    /// listen to all interfaces, use `0.0.0.0`. If you want the operating
24    /// system to choose a random port, use port `0`.
25    #[serde(default = "Network::default_bind_address")]
26    pub bind_address: SocketAddr,
27
28    /// TSL configuration.
29    #[serde(default = "Network::default_tsl")]
30    pub tsl: Option<Tsl>,
31}
32
33impl Default for Network {
34    fn default() -> Self {
35        Self {
36            bind_address: Self::default_bind_address(),
37            base_url: Self::default_base_url(),
38            tsl: Self::default_tsl(),
39        }
40    }
41}
42
43impl Network {
44    fn default_bind_address() -> SocketAddr {
45        SocketAddr::new(Self::default_ip(), Self::default_port())
46    }
47
48    fn default_ip() -> IpAddr {
49        IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))
50    }
51
52    fn default_port() -> u16 {
53        3001
54    }
55
56    fn default_base_url() -> Option<Url> {
57        None
58    }
59
60    fn default_tsl() -> Option<Tsl> {
61        None
62    }
63}