#hyper-server #https #hyper #https-server #tls #http #rustls

flexible-hyper-server-tls

Easily choose between HTTP or HTTPS when using hyper

6 releases

0.3.0 Mar 15, 2024
0.2.1 Mar 14, 2024
0.2.0 Jan 1, 2024
0.1.2 Dec 20, 2023
0.1.1 Jul 26, 2023

#8 in #hyper-server

Download history 197/week @ 2024-01-01 20/week @ 2024-01-08 104/week @ 2024-01-22 26/week @ 2024-01-29 59/week @ 2024-02-12 174/week @ 2024-02-19 648/week @ 2024-02-26 34/week @ 2024-03-04 388/week @ 2024-03-11 332/week @ 2024-03-18 104/week @ 2024-03-25 266/week @ 2024-04-01 315/week @ 2024-04-08 271/week @ 2024-04-15

991 downloads per month
Used in product-os-server

MIT/Apache

16KB
181 lines

Flexible Hyper Server TLS

A library that lets you choose whether to accept HTTPS or HTTP connections with Hyper. Very useful for situations where applications are self-hosted and the user gets to optionally provide their own HTTPS certificates.

This library also provides some helper functions that simplify the TLS setup by using the safe defaults from Rustls.

The aim of this library is to be simple and have minimal extra dependencies, while still allowing the user to customize things like TLS config.

For situations where you don't need to choose between HTTP and HTTPS, check out simple-hyper-server-tls or tls-listener.

Usage

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 builder = AcceptorBuilder::new(listener);

    let mut acceptor = if use_tls {
        let tls_acceptor =
            rustls_helpers::get_tlsacceptor_from_files("./cert.cer", "./key.pem").unwrap();
        builder.https(tls_acceptor).build()
    } else {
        builder.build()
    };

    acceptor.serve(service_fn(hello_world)).await;
}

Dependencies

~7–16MB
~203K SLoC