tsukuyomi 0.2.1

A next generation Web framework for Rust
Documentation

Crates.io Crates.io (Downloads) Docs.rs Gitter

Tsukuyomi is a next generation Web framework for Rust.

The ultimate goal of this project is to provide a Web framework for developing the asynchronous and fast Web services, with the help of ecosystem of Rust for asynchronous network services like Tokio and Hyper.

Features

  • Supports HTTP/1.x and HTTP/2.0 protocols, based on Hyper 0.12
  • Basic support for HTTP/1.1 protocol upgrade
  • TLS support by using rustls
  • Support for both TCP and Unix domain socket
  • Custom error handling
  • Basic support for Cookie management
  • Middleware support

The following features does not currently implemented but will be supported in the future version:

  • Custom session storage
  • Authentication
  • Embedded WebSocket handling

Getting Started

Tsukuyomi requires the latest version of Rust compiler. The required Rust toolchain is 1.27 or higher.

[dependencies]
tsukuyomi = "0.2"
futures = "0.1.22"
extern crate tsukuyomi;
extern crate futures;

use tsukuyomi::{App, Input, Error, Handler};
use futures::Future;

// A *synchronous* handler function.
// It will return a `Responder` which immediately convert into an HTTP response,
// and does not need any asynchronous computation.
fn handler(_input: &mut Input) -> &'static str {
    "Hello, Tsukuyomi.\n"
}

// An *asynchronous* handler function.
// It will return a `Future` representing the remaining computation in the handler.
fn async_handler(input: &mut Input)
    -> impl Future<Item = String, Error = Error> + Send + 'static
{
    input.body_mut().read_all().convert_to::<String>()
        .and_then(|body| {
            Ok(format!("Received: {}", body))
        })
        .inspect(|_| {
            // You can access a mutable reference to `Input` stored in
            // the task-local storage by using Input::with_current():
            Input::with_current(|input| {
                println!("[debug] path = {}", input.uri().path());
            })
        })
}

fn main() -> tsukuyomi::AppResult<()> {
    let app = App::builder()
        .mount("/", |m| {
            m.get("/")
             .handle(Handler::new_ready(handler));

            m.post("/async")
             .handle(Handler::new_async(async_handler));
        })
        .finish()?;

    tsukuyomi::run(app)
}

Using futures-await (requires Nightly compiler)

[dependencies]
tsukuyomi = "0.2"
futures-await = "0.1"
#![feature(proc_macro, proc_macro_non_items, generators)]

extern crate tsukuyomi;
extern crate futures_await as futures;

use futures::prelude::{async, await, Future};
use tsukuyomi::{App, Input, Handler};

#[async]
fn handler() -> tsukuyomi::Result<String> {
    let read_all = Input::with_current(|input| input.body_mut().read_all());
    let message: String = await!(read_all.convert_to())?;
    Ok(format!("Received: {}", message))
}

fn main() -> tsukuyomi::AppResult<()> {
    let app = App::builder()
        .mount("/", |m| {
            m.post("/")
             .handle(Handler::new_fully_async(handler));
        })
        .finish()?;

    tsukuyomi::run(app)
}

More examples are located in examples/.

If you want to experiment with these examples, try to clone this repository and run the following command:

$ cargo run -p example-basic

Documentation

Build Status

Travis CI Appveor Coveralls
Build Status Build status Coverage Status

License

Tsukuyomi is licensed under either of MIT license or Apache License, Version 2.0 at your option.