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