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
55 lines
1.6 KiB
Rust
55 lines
1.6 KiB
Rust
use std::io;
|
|
|
|
use log::info;
|
|
use ntex::http::header::HeaderValue;
|
|
use ntex::http::{HttpService, Response};
|
|
use ntex::{server, time::Seconds, util::Ready};
|
|
use tls_openssl::ssl::{self, SslFiletype, SslMethod};
|
|
|
|
#[ntex::main]
|
|
async fn main() -> io::Result<()> {
|
|
std::env::set_var("RUST_LOG", "trace");
|
|
let _ = env_logger::try_init();
|
|
|
|
println!("Started openssl web server: 127.0.0.1:8443");
|
|
|
|
// load ssl keys
|
|
let mut builder = ssl::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();
|
|
|
|
// h2 alpn config
|
|
builder.set_alpn_select_callback(|_, protos| {
|
|
const H2: &[u8] = b"\x02h2";
|
|
if protos.windows(3).any(|window| window == H2) {
|
|
Ok(b"h2")
|
|
} else {
|
|
Err(ssl::AlpnError::NOACK)
|
|
}
|
|
});
|
|
builder.set_alpn_protos(b"\x02h2").unwrap();
|
|
|
|
let acceptor = builder.build();
|
|
|
|
// start server
|
|
server::ServerBuilder::new()
|
|
.bind("basic", "127.0.0.1:8443", move |_| {
|
|
HttpService::build()
|
|
.client_timeout(Seconds(1))
|
|
.disconnect_timeout(Seconds(1))
|
|
.h2(|req| {
|
|
info!("{:?}", req);
|
|
let mut res = Response::Ok();
|
|
res.header("x-head", HeaderValue::from_static("dummy value!"));
|
|
Ready::Ok::<_, io::Error>(res.body("Hello world!"))
|
|
})
|
|
.openssl(acceptor.clone())
|
|
})?
|
|
.workers(1)
|
|
.run()
|
|
.await
|
|
}
|