use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use argh::FromArgs;
use certificate_manager::certificate::CertificateAuthority;
use rustls_pki_types::{pem::PemObject, PrivateKeyDer};
use std::io;
#[derive(FromArgs)]
struct StartServer {
#[argh(
option,
short = 'c',
default = "\"./examples/ca_certs/example.com.pem\".to_string()"
)]
cert_file: String,
#[argh(
option,
short = 'k',
default = "\"./examples/ca_certs/key.pem\".to_string()"
)]
key_file: String,
#[argh(option, short = 'p')]
passphrase: Option<String>,
}
async fn index() -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().content_type("text/html").body(
"<html><head><title>Welcome</title></head><body>Welcome to the Actix server!</body></html>",
))
}
#[actix_web::main]
async fn main() -> io::Result<()> {
let args: StartServer = argh::from_env();
let ca =
CertificateAuthority::load_from_pem_files(args.cert_file, args.key_file, args.passphrase)
.unwrap();
let key = PrivateKeyDer::from_pem_slice(ca.key_pair.serialize_pem().as_bytes()).unwrap();
let tls_config = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(vec![ca.cert.der().clone()], key)
.unwrap();
println!("Server is starting at https://127.0.0.1:4443");
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.service(web::resource("/").to(index))
})
.bind_rustls_0_23(("127.0.0.1", 4443), tls_config)?
.run()
.await?;
Ok(())
}