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());