Rust Web 框架
用最少的代码,构建生产级 Web 应用。 Hotaru 将 Rust 的卓越性能与优雅的 API 设计完美融合。
use hotaru::prelude::*;
LServer!(APP = Server::new()
.binding("127.0.0.1:3000")
.build());
endpoint! {
APP.url("/"),
pub hello<HTTP> {
text_response("Hello, Hotaru!")
}
}
为何选择 Hotaru
所见即所得。没有隐藏的复杂性,没有意外的行为。只有简洁、明确的 Rust 代码。
HTTP、WebSocket 和自定义 TCP 协议。一个框架,无限可能。
内置 Akari 模板引擎,轻松实现服务端渲染。
请求验证、请求体限制、方法白名单,开箱即用。
异步中间件,支持协议感知的继承机制。
灵活的模式匹配,支持类型化参数。
开发体验
use hotaru::prelude::*;
use hotaru::http::*;
LServer!(APP = Server::new()
.binding("127.0.0.1:3000")
.build());
endpoint! {
APP.url("/"),
pub index<HTTP> {
text_response("Hello, World!")
}
}
#[tokio::main]
async fn main() {
APP.clone().run().await;
}
endpoint! {
APP.url("/users/<int:id>"),
pub get_user<HTTP> {
let user_id = req.pattern("id")
.unwrap_or("unknown".to_string());
json_response(object!({
id: user_id,
name: "Alice",
email: "alice@example.com"
}))
}
}
// GET /users/42 → {"id":"42","name":"Alice",...}
endpoint! {
APP.url("/profile"),
pub profile<HTTP> {
akari_render!(
"profile.html",
name = "Alice",
email = "alice@example.com",
posts = ["First post", "Second post"]
)
}
}
// Server-rendered HTML with type-safe templates
middleware! {
pub Logger<HTTP> {
let start = std::time::Instant::now();
let path = req.path().to_string();
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::<GlobalLogger>()
.append_middleware::<GlobalMetrics>()
)
.build());