8 个不稳定版本 (3 个重大变更)
使用旧版 Rust 2015
0.3.0 | 2018 年 1 月 26 日 |
---|---|
0.2.1 |
|
0.2.0 | 2017 年 8 月 30 日 |
0.1.0 | 2017 年 8 月 30 日 |
0.0.8 | 2017 年 8 月 29 日 |
#595 在 HTTP 服务器 中排名
每月 38 次下载
59KB
1.5K SLoC
Shio
Shio 是一个快速、简单且异步的 Rust 微型 Web 框架。
-
异步。处理器可以异步处理,也可以是异步的。一个
shio::Handler
接收一个tokio_core::reactor::Handle
,该句柄可用于在线程局部事件循环中安排更多工作。 -
多线程。默认情况下,请求由多个线程处理,每个线程运行一个由
tokio
提供动力的事件循环。 -
稳定性。Shio 完全致力于在 稳定版 Rust 上进行工作和继续工作。
使用方法
[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();
}
示例
有状态
请求处理器是一个实现了 shio::Handler
特性的值。
处理器在每个请求上都不会被克隆,因此可能包含状态。注意,任何字段都必须是 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()
))
}
}
更多示例
Shio 包含许多更多使用 examples/ 示例。
示例可以使用 cargo run -p <example name>
运行。例如,要运行 hello
示例,请使用
$ cargo run -p hello
许可证
许可证为以下之一
- Apache 许可证 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- 麻省理工学院许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
根据您的选择。
贡献
除非您明确声明,否则根据 Apache-2.0 许可证定义,您有意提交用于包含在作品中的任何贡献,将按照上述方式双许可,不附加任何额外条款或条件。
依赖项
~13MB
~232K SLoC