3个不稳定版本

使用旧的Rust 2015

0.2.1 2018年2月8日
0.2.0 2017年12月4日
0.1.1 2017年12月2日

#715 in HTTP服务器


用于 ferrum-router

MIT 许可证

55KB
1K SLoC

Ferrum

为Rust设计的可扩展、并发Web框架。

Ferrum是基于 Iron 的一个分支。

Hello World 示例

extern crate ferrum;

use ferrum::*;

fn main() {
    Ferrum::new(|_: &mut Request| {
        Ok(Response::new().with_content("Hello world!", mime::TEXT_PLAIN))
    }).http("localhost:3000").unwrap();
}

响应计时器示例

extern crate time;
extern crate ferrum;

use ferrum::*;
use time::precise_time_ns;

struct ResponseTime;

impl typemap::Key for ResponseTime {
    type Value = u64;
}

impl BeforeMiddleware for ResponseTime {
    fn before(&self, request: &mut Request) -> FerrumResult<()> {
        request.extensions.insert::<ResponseTime>(precise_time_ns());
        Ok(())
    }
}

impl AfterMiddleware for ResponseTime {
    fn after(&self, request: &mut Request, response: Response) -> FerrumResult<Response> {
        let delta = precise_time_ns() - *request.extensions.get::<ResponseTime>().unwrap();
        println!("Request took: {} ms", (delta as f64) / 1000000.0);
        Ok(response)
    }
}

fn hello_world(_: &mut Request) -> FerrumResult<Response> {
    Ok(Response::new().with_content("Hello World", mime::TEXT_PLAIN))
}

fn main() {
    let mut chain = Chain::new(hello_world);
    chain.link_before(ResponseTime);
    chain.link_after(ResponseTime);
    Ferrum::new(chain).http("localhost:3000").unwrap();
}

概述

Ferrum是一个在Rust中构建的高级别Web框架,基于 hyper。Ferrum旨在利用Rust的最大特性——其优秀的类型系统和在单线程和多线程环境中的所有权原则。

Ferrum高度并发,可以在负载均衡器后或通过在更强大的机器上运行更多线程进行水平扩展。Ferrum通过避免核心框架中的共享写入和锁定来避免高度并发代码中遇到瓶颈。

哲学

Ferrum旨在尽可能地可扩展和可插拔;Ferrum的核心集中,通过将其留给中间件和插件来避免不必要的功能。

中间件和插件是扩展Ferrum新功能的主要方式。在其他Web框架中由中间件提供的许多扩展现在由更简单的插件系统处理。

插件允许对请求和响应进行懒加载、自动缓存的扩展,非常适合解析、访问以及其他懒加载方式操作HTTP连接。

当需要修改请求流的控制流、劫持请求的整个处理过程、检查传入的请求或进行最终后处理时,才使用中间件。这包括路由、挂载、静态资源服务、最终模板渲染、身份验证和日志记录等领域。

Ferrum只提供了设置状态、主体和各种头的基本功能,以及创建插件和中间件的基础设施。Ferrum不捆绑任何插件或中间件。

底层HTTP实现

“Ferrum”基于并使用hyper作为其HTTP实现,并从中提取了几个类型,包括其头表示、状态以及其他核心HTTP类型。在使用Ferrum时,通常没有必要直接使用hyperfuturesmime,因为Ferrum提供了对这些核心设施的前端,但有时也需要依赖它。

安装

如果您使用Cargo,只需将Ferrum添加到您的Cargo.toml

[dependencies.ferrum]
version = "*"

示例

查看示例目录!

您可以使用以下命令运行单个示例:cargo run --example example-name。请注意,对于基准测试,您应该确保使用--release标志,这将导致Cargo编译整个工具链并启用优化。如果没有--release,您将得到真正令人沮丧的数字。

许可证

MIT

依赖关系

~10MB
~202K SLoC