Skip to main content

opentalk_roomserver_common/settings/runtime_settings/
http.rs

1// SPDX-License-Identifier: EUPL-1.2
2// SPDX-FileCopyrightText: OpenTalk Team <mail@opentalk.eu>
3
4use std::net::{Ipv4Addr, Ipv6Addr, TcpListener};
5
6use opentalk_service_auth::service::ApiKeys;
7
8use crate::settings::settings_file;
9
10/// Settings for the HTTP server
11#[derive(Debug, Clone)]
12pub struct Http {
13    /// The IP address that the HTTP server should bind to
14    pub address: String,
15
16    /// The port that the HTTP server should use
17    pub port: u16,
18
19    /// The URL that is reachable by internal services
20    pub service_url: Option<url::Url>,
21
22    /// The publicly reachable URL of this server
23    pub public_url: url::Url,
24
25    /// The API keys for service endpoints
26    pub api_keys: ApiKeys,
27
28    // Enable the OpenAPI endpoint under `/v1/openapi.json` and the corresponding
29    // swagger endpoint under `/swagger`.
30    pub enable_openapi: bool,
31}
32
33impl From<settings_file::http::Http> for Http {
34    fn from(value: settings_file::http::Http) -> Self {
35        let address = match value.address {
36            Some(address) => address,
37            None => {
38                if is_ipv6_available() {
39                    Ipv6Addr::UNSPECIFIED.to_string()
40                } else {
41                    Ipv4Addr::UNSPECIFIED.to_string()
42                }
43            }
44        };
45
46        Self {
47            address,
48            port: value.port,
49            service_url: value.service_url,
50            public_url: value.public_url,
51            api_keys: value.api_keys,
52            enable_openapi: value.enable_openapi,
53        }
54    }
55}
56
57fn is_ipv6_available() -> bool {
58    TcpListener::bind((Ipv6Addr::UNSPECIFIED, 0)).is_ok()
59}