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