特性 代码
立即开始

实验性 Rust 网络框架

小巧。精致。
面向协议。

目前使用 Tokio 构建 HTTP/1.1 Server 与 Client,并通过对称的 Endpoint / Outpoint API 组织入站与出站逻辑。Hotaru 0.8.x 仍处于 pre-1.0 实验与加固阶段。

main.rs
use hotaru::http::*;
use hotaru::prelude::*;

LServer!(
    APP = Server::new()
        .binding("127.0.0.1:3003")
        .single_protocol(ProtocolBuilder::new(
            HTTP::server(HttpSafety::default())
        ))
        .build()
);

fn main() {
    run_server!(APP);
}

endpoint! {
    APP.url("/"),
    pub index<HTTP> {
        text_response("Hello, Hotaru!")
    }
}

当前能力

Hotaru 目前能够提供什么。
围绕面向协议的核心构建。

有主张的默认

主路径使用 trans Endpoint DSL 与自动注册,同时保留显式注册和实验性的手写定义路径。

面向协议的核心

主仓库目前交付 HTTP/1.1 与 HTTPS;Protocol trait 为自定义协议实现提供扩展点。

服务端 Web 辅助能力

Akari 模板与可选 Web 中间件可用于 Tokio/HTTP 服务端渲染应用。

可配置的请求限制

通过 HttpSafety 配置请求体、Header、行长度与方法限制;0.8.x HTTP 栈仍在持续加固。

Server + Client

Endpoint 处理入站请求,Outpoint 组织出站工作;两者共享协议与运行时模型。

类型化与模式路由

支持字面量、类型化、正则、通配符与 catch-all 路由段。

开发体验

一条明确的主路径。
需要时也有显式选择。

Hello World
use hotaru::http::*;
use hotaru::prelude::*;

LServer!(
    APP = Server::new()
        .binding("127.0.0.1:3003")
        .single_protocol(ProtocolBuilder::new(
            HTTP::server(HttpSafety::default())
        ))
        .build()
);

fn main() {
    run_server!(APP);
}

endpoint! {
    APP.url("/"),
    pub index<HTTP> {
        text_response("Hello, Hotaru!")
    }
}
Path Parameters
endpoint! {
    APP.url("/users/<int:id>"),
    pub get_user<HTTP> {
        let user_id = req.param("id")
            .unwrap_or("unknown".to_string());

        akari_json!({
            id: user_id,
            name: "Alice",
            email: "[email protected]"
        })
    }
}

// GET /users/42 -> {"id":"42","name":"Alice",...}
Akari Templates
endpoint! {
    APP.url("/profile"),
    pub profile<HTTP> {
        akari_render!(
            "profile.html",
            name = "Alice",
            email = "[email protected]",
            posts = ["First post", "Second post"]
        )
    }
}

// Server-rendered HTML with type-safe templates
Async Middleware
middleware! {
    pub Logger<HTTP> {
        let start = std::time::Instant::now();
        let path = req.path();
        let req = next(req).await?;
        println!("[LOG] {} - {:?}", path, start.elapsed());
        Ok(req)
    }
}

LServer!(APP = Server::new()
    .binding("127.0.0.1:3003")
    .single_protocol(
        ProtocolBuilder::new(HTTP::server(HttpSafety::default()))
            .append_middleware::<Logger>()
    )
    .build());

试用当前的
Tokio + HTTP/1.1 路径。

添加 Hotaru,复制上方已验证的 Quickstart,并在本地启动服务。Hotaru 0.8.x 仍处于实验阶段。

cargo add hotaru