flexible_hyper_server_tls/lib.rs
1#![warn(clippy::pedantic, clippy::nursery, rust_2018_idioms)]
2#![allow(clippy::module_name_repetitions)]
3
4//! This library lets you easily create a Hyper acceptor that be configured to either accept HTTP or HTTPS connections.
5//! This is useful for applications that users will self-host, and have the option to run as HTTP or provide their own HTTPS certificates.
6//! At the moment, this library only supports accepting HTTP/1 connections
7//!
8//! **Note: HTTP and HTTPS cannot be accepted at the same time, you decide which one to use when creating the acceptor.**
9//! If you want to use both HTTP and HTTPS, you can create 2 acceptors listening on different ports.
10//!
11//! ## Features
12//! - `rustls_helpers` (enabled by default): Provides some helpers for configuring TLS
13//! - `tls12` (enabled by default): Enables support for TLS 1.2
14//! - `aws_lc_rs` (enabled by default): Uses the `aws_lc_rs` crate for cryptography
15//! - `ring`: Uses the `ring` crate for cryptography
16//!
17//! ## Example
18//! ```
19//! use flexible_hyper_server_tls::*;
20//! use http_body_util::Full;
21//! use hyper::body::{Bytes, Incoming};
22//! use hyper::service::service_fn;
23//! use hyper::{Request, Response};
24//! use std::convert::Infallible;
25//! use tokio::net::TcpListener;
26//!
27//! async fn hello_world(_req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
28//! Ok(Response::new(Full::<Bytes>::from("Hello, World!")))
29//! }
30//!
31//! #[tokio::main]
32//! async fn main() {
33//! let use_tls = true;
34//!
35//! let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
36//!
37//! let mut acceptor = HttpOrHttpsAcceptor::new(listener);
38//!
39//! if use_tls {
40//! let tls = rustls_helpers::get_tlsacceptor_from_files("./cert.cer", "./key.pem").unwrap();
41//! acceptor = acceptor.with_tls(tls);
42//! }
43//!
44//! loop {
45//! if let Ok((peer_addr, conn_fut)) = acceptor.accept(service_fn(hello_world)).await {
46//! println!("New connection from {peer_addr}");
47//!
48//! tokio::spawn(async move {
49//! if let Err(err) = conn_fut.await {
50//! eprintln!("Error serving connection: {err:?}")
51//! }
52//! });
53//! }
54//! }
55//! }
56//! ```
57
58mod accept;
59#[cfg(feature = "rustls_helpers")]
60pub mod rustls_helpers;
61mod stream;
62
63// Export into main library
64pub use accept::{AcceptorError, HttpOrHttpsAcceptor};