实验性 Rust 网络框架
目前使用 Tokio 构建 HTTP/1.1 Server 与 Client,并通过对称的 Endpoint / Outpoint API 组织入站与出站逻辑。Hotaru 0.8.x 仍处于 pre-1.0 实验与加固阶段。
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!")
}
}
当前能力
主路径使用 trans Endpoint DSL 与自动注册,同时保留显式注册和实验性的手写定义路径。
主仓库目前交付 HTTP/1.1 与 HTTPS;Protocol trait 为自定义协议实现提供扩展点。
Akari 模板与可选 Web 中间件可用于 Tokio/HTTP 服务端渲染应用。
通过 HttpSafety 配置请求体、Header、行长度与方法限制;0.8.x HTTP 栈仍在持续加固。
Endpoint 处理入站请求,Outpoint 组织出站工作;两者共享协议与运行时模型。
支持字面量、类型化、正则、通配符与 catch-all 路由段。
开发体验
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!")
}
}
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",...}
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
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());