實驗性 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());