1 个不稳定版本
0.1.0 | 2019年9月4日 |
---|
#137 in #routing
13KB
142 行
Craft
Craft 是一个轻量级 HTTP 框架。
状态: 概念证明
安装
将以下内容添加到您的应用的 Cargo.toml
文件中。
[dependencies]
craft = "0.1.0"
用法
use craft;
use hyper::*;
fn hello(_request: &Request<Body>) -> Response<Body> {
Response::new(Body::from("Hello, World!"))
}
#[runtime::main]
async fn main() {
let config = craft::Config::new("127.0.0.1", 3000);
let mut handler_stack = craft::Stack::empty();
handler_stack.set(hello);
craft::get("/hello", handler_stack);
craft::start(&config).await;
}
Craft 采取以模块为先的方法来处理中间件
use craft;
use hyper::*;
fn first_handler(_request: &Request<Body>, next: &dyn Fn() -> Response<Body>) -> Response<Body> {
println!("begin first_handler");
let response = next();
println!("end first_handler");
response
}
fn second_handler(_request: &Request<Body>, next: &dyn Fn() -> Response<Body>) -> Response<Body> {
println!("begin second_handler");
let response = next();
println!("end second_handler");
response
}
fn hello(_request: &Request<Body>) -> Response<Body> {
Response::new(Body::from("Hello, World!"))
}
#[runtime::main]
async fn main() {
let config = craft::Config::new("127.0.0.1", 3000);
let mut handler_stack = craft::Stack::empty();
handler_stack.push(first_handler);
handler_stack.push(second_handler);
handler_stack.set(hello);
craft::get("/hello", handler_stack);
craft::start(&config).await;
}
开发
$ cargo +nightly run --example hello
$ cargo +nightly run --example multi_handler
$ curl -X GET localhost:3000/hello
Hello, World!
依赖
~7.5MB
~139K SLoC