use product_os_server::{ProductOSServer, StatusCode, Response, Body};
use product_os_server::{ServerConfig, Certificate};
use product_os_async_executor::TokioExecutor;
async fn secure_handler() -> Result<Response<Body>, StatusCode> {
Ok(Response::builder()
.status(StatusCode::OK)
.body(Body::from("This connection is secure!"))
.unwrap())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let mut config = ServerConfig::new();
config.network.port = 8443;
config.network.secure = true;
config.network.host = "localhost".to_string();
config.certificate = Some(Certificate {
file_kind: None, files: None,
entries: None,
names: Some(vec!["localhost".to_string()]),
serial: None,
valid_for: None,
});
println!("Creating HTTPS server...");
let mut server: ProductOSServer<(), TokioExecutor, _> =
ProductOSServer::new_with_config(config);
server.add_get("/secure", secure_handler);
println!("HTTPS Server listening on https://localhost:8443");
println!("Try:");
println!(" curl -k https://localhost:8443/secure");
println!("Note: -k flag is needed because this uses a self-signed certificate");
server.start(false).await.map_err(|e| format!("Server error: {}", e))?;
loop {
tokio::time::sleep(std::time::Duration::from_secs(3600)).await;
}
}