実験的な 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());