1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-13 06:05:25 +01:00

52 lines
1.3 KiB
Rust
Raw Permalink Normal View History

2019-03-06 23:33:35 -08:00
use std::io;
2018-08-19 10:47:04 -07:00
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
2018-12-09 21:51:35 -08:00
use actix_rt::System;
2018-12-11 08:20:19 -08:00
use actix_server::{ssl, Server};
2018-12-09 21:51:35 -08:00
use actix_service::NewService;
2019-03-06 23:33:35 -08:00
use futures::future;
2018-12-11 08:20:19 -08:00
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
2018-08-19 10:47:04 -07:00
#[derive(Debug)]
struct ServiceState {
num: Arc<AtomicUsize>,
}
2019-03-06 23:33:35 -08:00
fn main() -> io::Result<()> {
2018-12-09 21:51:35 -08:00
let sys = System::new("test");
2018-08-19 10:47:04 -07:00
// load ssl keys
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder
.set_private_key_file("./examples/key.pem", SslFiletype::PEM)
.unwrap();
builder
.set_certificate_chain_file("./examples/cert.pem")
.unwrap();
let num = Arc::new(AtomicUsize::new(0));
2018-09-08 14:50:16 -07:00
let openssl = ssl::OpensslAcceptor::new(builder.build());
2018-08-19 10:47:04 -07:00
2018-08-23 13:39:13 -07:00
// server start mutiple workers, it runs supplied `Fn` in each worker.
2018-12-09 21:51:35 -08:00
Server::build()
2018-09-17 20:19:48 -07:00
.bind("test-ssl", "0.0.0.0:8443", move || {
2018-08-23 15:42:34 -07:00
let num = num.clone();
// configure service
openssl
.clone()
2018-09-08 14:50:16 -07:00
.map_err(|e| println!("Openssl error: {}", e))
2018-09-19 08:04:31 -07:00
.and_then(move |_| {
let num = num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
})
2019-03-06 23:33:35 -08:00
})?
2018-08-23 15:42:34 -07:00
.start();
2018-08-19 10:47:04 -07:00
2019-03-06 23:33:35 -08:00
sys.run()
2018-08-19 10:47:04 -07:00
}