mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-01 20:07:39 +03:00
Some checks failed
Checks / Check (push) Failing after 4s
Checks / Clippy (push) Failing after 3s
Checks / Rustfmt (push) Failing after 3s
Coverage / coverage (push) Failing after 2s
CI (Linux) / 1.75.0 - x86_64-unknown-linux-gnu (push) Failing after 2s
CI (Linux) / nightly - x86_64-unknown-linux-gnu (push) Failing after 2s
CI (Linux) / stable - x86_64-unknown-linux-gnu (push) Failing after 2s
CI (Windows) / nightly - x86_64-pc-windows-msvc (push) Has been cancelled
CI (Windows) / stable - x86_64-pc-windows-msvc (push) Has been cancelled
CI (OSX) / nightly - aarch64-apple-darwin (push) Has been cancelled
CI (OSX) / stable - aarch64-apple-darwin (push) Has been cancelled
66 lines
2.3 KiB
Rust
66 lines
2.3 KiB
Rust
use std::{fs::File, io, io::BufReader, sync::Arc};
|
|
|
|
use ntex::service::{chain_factory, fn_service};
|
|
use ntex::{codec, io::Io, server, util::Either};
|
|
use ntex_tls::rustls::TlsAcceptor;
|
|
use tls_rust::ServerConfig;
|
|
|
|
#[ntex::main]
|
|
async fn main() -> io::Result<()> {
|
|
std::env::set_var("RUST_LOG", "trace");
|
|
env_logger::init();
|
|
|
|
println!("Started openssl echp server: 127.0.0.1:8443");
|
|
|
|
// load ssl keys
|
|
let cert_file = &mut BufReader::new(File::open("../examples/cert.pem").unwrap());
|
|
let key_file = &mut BufReader::new(File::open("../examples/key.pem").unwrap());
|
|
let keys = rustls_pemfile::private_key(key_file).unwrap().unwrap();
|
|
let cert_chain = rustls_pemfile::certs(cert_file)
|
|
.collect::<Result<Vec<_>, _>>()
|
|
.unwrap();
|
|
let tls_config = Arc::new(
|
|
ServerConfig::builder()
|
|
.with_no_client_auth()
|
|
.with_single_cert(cert_chain, keys)
|
|
.unwrap(),
|
|
);
|
|
|
|
// start server
|
|
server::ServerBuilder::new()
|
|
.bind("basic", "127.0.0.1:8443", move |_| {
|
|
chain_factory(TlsAcceptor::new(tls_config.clone())).and_then(fn_service(
|
|
|io: Io<_>| async move {
|
|
println!("New client is connected");
|
|
|
|
io.send(
|
|
ntex_bytes::Bytes::from_static(b"Welcome!\n"),
|
|
&codec::BytesCodec,
|
|
)
|
|
.await
|
|
.map_err(Either::into_inner)?;
|
|
|
|
loop {
|
|
match io.recv(&codec::BytesCodec).await {
|
|
Ok(Some(msg)) => {
|
|
println!("Got message: {:?}", msg);
|
|
io.send(msg.freeze(), &codec::BytesCodec)
|
|
.await
|
|
.map_err(Either::into_inner)?;
|
|
}
|
|
Err(e) => {
|
|
println!("Got error: {:?}", e);
|
|
break;
|
|
}
|
|
Ok(None) => break,
|
|
}
|
|
}
|
|
println!("Client is disconnected");
|
|
Ok(())
|
|
},
|
|
))
|
|
})?
|
|
.workers(1)
|
|
.run()
|
|
.await
|
|
}
|