#web #framework #async #server #shio

shio

Shio is a fast, simple, and asynchronous micro web-framework for Rust

9 unstable releases (3 breaking)

0.3.0 Jan 26, 2018
0.2.1 Jan 26, 2018
0.2.0 Aug 30, 2017
0.1.0 Aug 30, 2017
0.0.8 Aug 29, 2017

#20 in HTTP server

Download history 85/week @ 2018-06-14 21/week @ 2018-06-21 26/week @ 2018-06-28 31/week @ 2018-07-05 14/week @ 2018-07-12 34/week @ 2018-07-19 1/week @ 2018-07-26 39/week @ 2018-08-02 135/week @ 2018-08-09 14/week @ 2018-08-16 20/week @ 2018-08-23 4/week @ 2018-08-30 15/week @ 2018-09-06

311 downloads per month

MIT/Apache

58KB
1.5K SLoC

Shio

Rust Build Status Coverage Status Crates.io Crates.io Docs.rs IRC

Shio is a fast, simple, and asynchronous micro web-framework for Rust.

  • Asynchronous. Handlers are both handled asynchronously and may be asynchronous themselves. A shio::Handler receives a tokio_core::reactor::Handle which may be used to schedule additional work on the thread-local event loop.

  • Multithreaded. By default, requests are handled by multiple threads, each running an event loop powered by tokio.

  • Stability. Shio is fully committed to working and continuing to work on stable Rust.

Usage

[dependencies]
shio = "0.2.0"
extern crate shio;

use shio::prelude::*;

fn hello_world(_: Context) -> Response {
    Response::with("Hello World!\n")
}

fn hello(ctx: Context) -> Response {
    Response::with(format!("Hello, {}!\n", &ctx.get::<Parameters>()["name"]))
}

fn main() {
    Shio::default()
        .route((Method::GET, "/", hello_world))
        .route((Method::GET, "/{name}", hello))
        .run(":7878").unwrap();
}

Examples

Stateful

A request handler is a value that implements the shio::Handler trait.

Handlers are not cloned on each request and therefore may contain state. Note that any fields must be Send + Sync.

extern crate shio;

use std::thread;
use std::sync::atomic::{AtomicUsize, Ordering};
use shio::prelude::*;

#[derive(Default)]
struct HandlerWithState {
    counter: AtomicUsize,
}

impl shio::Handler for HandlerWithState {
    type Result = Response;

    fn call(&self, _: Context) -> Self::Result {
        let counter = self.counter.fetch_add(1, Ordering::Relaxed);

        Response::with(format!(
            "Hi, #{} (from thread: {:?})\n",
            counter,
            thread::current().id()
        ))
    }
}

Even More Examples

Many more usage examples/ are included with Shio.

Examples may be ran with cargo run -p <example name>. For instance, to run the hello example, use:

$ cargo run -p hello

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~7.5MB
~126K SLoC