1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 04:52:58 +01:00
actix-net/examples/ssl.rs

62 lines
1.6 KiB
Rust
Raw Normal View History

2018-08-19 19:47:04 +02:00
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
2018-12-11 17:20:19 +01:00
use actix_codec::{AsyncRead, AsyncWrite};
2018-12-10 06:51:35 +01:00
use actix_rt::System;
2018-12-11 17:20:19 +01:00
use actix_server::{ssl, Server};
2018-12-10 06:51:35 +01:00
use actix_service::NewService;
2018-12-11 17:20:19 +01:00
use futures::{future, Future};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
2018-08-19 19:47:04 +02:00
#[derive(Debug)]
struct ServiceState {
num: Arc<AtomicUsize>,
}
fn service<T: AsyncRead + AsyncWrite>(
2018-10-30 04:29:47 +01:00
st: &mut ServiceState,
_: T,
2018-09-08 23:50:16 +02:00
) -> impl Future<Item = (), Error = ()> {
2018-08-19 19:47:04 +02:00
let num = st.num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
}
fn main() {
2018-12-10 06:51:35 +01:00
let sys = System::new("test");
2018-08-19 19:47:04 +02: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 23:50:16 +02:00
let openssl = ssl::OpensslAcceptor::new(builder.build());
2018-08-19 19:47:04 +02:00
2018-08-23 22:39:13 +02:00
// server start mutiple workers, it runs supplied `Fn` in each worker.
2018-12-10 06:51:35 +01:00
Server::build()
2018-09-18 05:19:48 +02:00
.bind("test-ssl", "0.0.0.0:8443", move || {
2018-08-24 00:42:34 +02:00
let num = num.clone();
// configure service
openssl
.clone()
2018-09-08 23:50:16 +02:00
.map_err(|e| println!("Openssl error: {}", e))
2018-09-19 17:04:31 +02:00
.and_then(move |_| {
let num = num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
})
2018-12-06 23:04:42 +01:00
})
.unwrap()
2018-08-24 00:42:34 +02:00
.start();
2018-08-19 19:47:04 +02:00
sys.run();
}