Crates.io | tophat |
lib.rs | tophat |
version | 0.3.0 |
source | src |
created_at | 2020-05-11 14:16:17.583803 |
updated_at | 2020-12-30 19:30:42.634493 |
description | A small, pragmatic, and flexible async http server |
homepage | |
repository | https://github.com/hwchen/tophat |
max_upload_size | |
id | 240124 |
size | 278,715 |
A small, pragmatic, and flexible async HTTP server library. Currently in beta.
Cargo.toml:
tophat = "0.3.0"
The goal is to be low-level and small enough to work with different async runtimes and not dictate user architecture, while having enough convenience functions to still easily build a REST api. More library than framework.
Also, this:
async fn handler<W>(_req: Request, resp_wtr: ResponseWriter<W>) -> Result<ResponseWritten, Glitch>
where W: AsyncRead + AsyncWrite + Clone + Send + Sync + Unpin + 'static,
{
// Default `send` is 200 OK
let done = resp_wtr.send()?;
// Do things here after resp is written, if you like
Ok(done)
}
instead of:
async fn handler(req:Request) -> Result<Response, Error> {
Ok(Response::empty())
}
Don't be scared away by the generics and trait bounds! They won't bite! (probably)
futures::{AsyncRead, AsyncWrite}
.features = ["router"]
, very minimal.features = ["cors"]
.features = ["identity"]
.Glitch
and GlitchExt
, to conveniently chain onto both Result
and Option
.Correct handling of the HTTP protocol is a priority.
Using smol
as the async runtime. Example is single-threaded, see smol
docs for how to make a multi-threaded executor.
use smol::{Async, Task};
use std::net::TcpListener;
use async_dup::Arc;
use tophat::server::accept;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = Async::<TcpListener>::bind("127.0.0.1:9999")?;
smol::run(async {
loop {
let (stream, _) = listener.accept().await?;
let stream = Arc::new(stream);
let task = Task::spawn(async move {
let serve = accept(stream, |_req, resp_wtr| async {
resp_wtr.send().await
}).await;
if let Err(err) = serve {
eprintln!("Error: {}", err);
}
});
task.detach();
}
})
}
I wouldn't consider this a batteries-included framework which tries to make every step easy. There are conveniences, but overall tophat is pretty minimal. For those who don't like boilerplate, another framework would probably work better. Users of tophat need to be familiar async runtimes, setting up a TCP stream, Arc
, traits, generics, etc. Tophat won't hold your hand.
In exchange, tophat provides more transparency and more control. Tophat won't dictate how to structure your app, it should play nicely with your architecture.
And if you want to know what tophat is doing under the hood, the code is meant to be simple and straightforward (Hopefully this also leads to better compile times!).
I was inspired to write tophat because:
ResponseWriter
instead of returning Response
: https://github.com/hyperium/hyper/issues/2181Especially to async-h1, whose eye for structure and design I appreciate, and whose code base tophat is built from. And to hyper, whose devotion to performance and correctness is inspiring, and whose basic http libraries tophat has incorporated.
Licensed under either of
at your option.