功能特色 程式碼
立即開始

實驗性 Rust 網路框架

輕巧。精緻。
面向協定。

目前使用 Tokio 建構 HTTP/1.1 Server 與 Client,並透過對稱的 Endpoint / Outpoint API 組織入站與出站邏輯。Hotaru 0.8.x 仍處於 pre-1.0 實驗與強化階段。

main.rs
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!")
    }
}

目前能力

Hotaru 目前能提供什麼。
圍繞面向協定的核心建構。

有主張的預設

主要路徑使用 trans Endpoint DSL 與自動註冊,同時保留明確註冊與實驗性的手寫定義路徑。

面向協定的核心

主工作區目前提供 HTTP/1.1 與 HTTPS;Protocol trait 是自訂協定實作的擴充點。

伺服器端 Web 輔助功能

Akari 樣板與可選的 Web 中介軟體可用於 Tokio/HTTP 伺服器端渲染應用程式。

可設定的請求限制

透過 HttpSafety 設定請求本文、Header、行長度與方法限制;0.8.x HTTP 堆疊仍在持續強化。

Server + Client

Endpoint 處理入站交換,Outpoint 組織出站工作;兩者共享協定與執行階段模型。

型別化與模式路由

支援字面值、型別化、正規表示式、萬用字元與 catch-all 路由片段。

開發體驗

一條明確的主要路徑。
需要時亦有明確選擇。

Hello World
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!")
    }
}
Path Parameters
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",...}
Akari Templates
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
Async Middleware
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());

試用目前的
Tokio + HTTP/1.1 路徑。

加入 Hotaru,複製上方已驗證的 Quickstart,並在本機啟動伺服器。Hotaru 0.8.x 仍處於實驗階段。

cargo add hotaru