smol/examples/simple-server.rs

89 lines
2.7 KiB
Rust

//! A simple HTTP+TLS server based on `async-native-tls`.
//!
//! Run with:
//!
//! ```
//! cargo run --example simple-server
//! ```
//!
//! Open in the browser any of these addresses:
//!
//! - http://localhost:8000/
//! - https://localhost:8001/ (you'll need to import the TLS certificate first!)
//!
//! Refer to `README.md` to see how to import or generate the TLS certificate.
use std::net::{TcpListener, TcpStream};
use std::thread;
use anyhow::Result;
use async_native_tls::{Identity, TlsAcceptor};
use futures::prelude::*;
use smol::{Async, Task};
const RESPONSE: &[u8] = br#"
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 47
<!DOCTYPE html><html><body>Hello!</body></html>
"#;
/// Reads a request from the client and sends it a response.
async fn serve(mut stream: Async<TcpStream>, tls: Option<TlsAcceptor>) -> Result<()> {
match tls {
None => {
println!("Serving http://{}", stream.get_ref().local_addr()?);
stream.write_all(RESPONSE).await?;
}
Some(tls) => {
println!("Serving https://{}", stream.get_ref().local_addr()?);
// In case of HTTPS, establish a secure TLS connection first.
let mut stream = tls.accept(stream).await?;
stream.write_all(RESPONSE).await?;
stream.flush().await?;
stream.close().await?;
}
}
Ok(())
}
/// Listens for incoming connections and serves them.
async fn listen(listener: Async<TcpListener>, tls: Option<TlsAcceptor>) -> Result<()> {
// Display the full host address.
match &tls {
None => println!("Listening on http://{}", listener.get_ref().local_addr()?),
Some(_) => println!("Listening on https://{}", listener.get_ref().local_addr()?),
}
loop {
// Accept the next connection.
let (stream, _) = listener.accept().await?;
// Spawn a background task serving this connection.
Task::spawn(serve(stream, tls.clone())).unwrap().detach();
}
}
fn main() -> Result<()> {
// Initialize TLS with the local certificate, private key, and password.
let identity = Identity::from_pkcs12(include_bytes!("../identity.pfx"), "password")?;
let tls = TlsAcceptor::from(native_tls::TlsAcceptor::new(identity)?);
// Create an executor thread pool.
let num_threads = num_cpus::get().max(1);
for _ in 0..num_threads {
thread::spawn(|| smol::run(future::pending::<()>()));
}
// Start HTTP and HTTPS servers.
smol::run(async {
let http = listen(Async::<TcpListener>::bind("127.0.0.1:8000")?, None);
let https = listen(Async::<TcpListener>::bind("127.0.0.1:8001")?, Some(tls));
future::try_join(http, https).await?;
Ok(())
})
}