use std::net::{Ipv4Addr, Ipv6Addr, TcpListener};
use opentalk_service_auth::service::ApiKeys;
use crate::settings::settings_file;
#[derive(Debug, Clone)]
pub struct Http {
pub address: String,
pub port: u16,
pub service_url: Option<url::Url>,
pub public_url: url::Url,
pub api_keys: ApiKeys,
pub enable_openapi: bool,
}
impl From<settings_file::http::Http> for Http {
fn from(value: settings_file::http::Http) -> Self {
let address = match value.address {
Some(address) => address,
None => {
if is_ipv6_available() {
Ipv6Addr::UNSPECIFIED.to_string()
} else {
Ipv4Addr::UNSPECIFIED.to_string()
}
}
};
Self {
address,
port: value.port,
service_url: value.service_url,
public_url: value.public_url,
api_keys: value.api_keys,
enable_openapi: value.enable_openapi,
}
}
}
fn is_ipv6_available() -> bool {
TcpListener::bind((Ipv6Addr::UNSPECIFIED, 0)).is_ok()
}