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