tsukuyomi-rs / izanami

[WIP] A simple Web application interface inspired from ASGI.
https://tsukuyomi-rs.github.io/izanami
Apache License 2.0
3 stars 0 forks source link

switch to std future #40

Closed ubnt-intrepid closed 4 years ago

ubnt-intrepid commented 4 years ago

The current definition of Handler and the abstraction is based on the older futures-0.1 ecosystem and we need to switch our mind to the new futures and async/.await world.

A candidate of design (inspired from uvicorn):

#[async_trait]
trait App {
    async fn call(&self, req: &mut Request<'_>) -> io::Result<()>;
}

struct App(...);

#[async_trait]
impl App for MyApp {
    async fn call(&self, req: &mut Request<'_>) -> io::Result<()> {
        req.reply(StatusCode::CREATED, "hello").await
    }
}

struct WebSocketApp(..);

#[async_trait]
impl App for MyApp {
    async fn call(&self, req: &mut Request<'_>) -> io::Result<()> {
        let websocket = req.reply_ws(...).await?;

        let (reader, writer) = websocket.split();
        loop {
            match reader.read_msg().await? {
                Some(WsMsg::Ping(data)) => self.pong(data).await?,
                None => break,
            }
        }

        Ok(())
    }
}
struct Request<'a> { ... }

impl Request<'_> {
    async fn reply(&mut self, status: StatusCode, reply: impl Reply)
        -> io::Result<()> { ... }

    async fn reply_ws(&mut self, status: StatusCode, hdrs: Headers)
        -> io::Result<WebSocket<'_>> { ... }
}
ubnt-intrepid commented 4 years ago

The project was rebooted.