Skip to main content

rust_integration_services/http/server/
http_server_config.rs

1use std::path::Path;
2
3use rustls::{ServerConfig};
4
5use crate::http::crypto::Crypto;
6
7pub struct HttpServerConfig {
8    pub ip: String,
9    pub port: u16,
10    pub tls_config: Option<ServerConfig>,
11}
12
13impl HttpServerConfig {
14    pub fn new(ip: impl Into<String>, port: u16) -> Self {
15        HttpServerConfig {
16            ip: ip.into(),
17            port,
18            tls_config: None,
19        }
20    }
21
22    /// Enables TLS for incoming connections using the provided server certificate and private key in `.pem` format and
23    /// configures the TLS context and sets supported ALPN protocols to allow HTTP/2 and HTTP/1.1.
24    pub fn tls(mut self, tls_server_cert_path: impl AsRef<Path>, tls_server_key_path: impl AsRef<Path>) -> Self {
25        let certs = Crypto::pem_load_certs(tls_server_cert_path).expect("Failed to load server cert.");
26        let key = Crypto::pem_load_private_key(tls_server_key_path).expect("Failed to load server key.");
27        Crypto::install_crypto_provider().expect("Failed to install crypto provider.");
28
29        let mut tls_config = ServerConfig::builder()
30            .with_no_client_auth()
31            .with_single_cert(certs, key)
32            .expect("Failed to create tls server config.");
33
34        tls_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
35
36        self.tls_config = Some(tls_config);
37        self
38    }
39}