功能 代碼
即刻開始

實驗性 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!")
    }
}
路徑參數
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 模板
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());

試用而家嘅
Tokio + HTTP/1.1 路徑。

加入 Hotaru,複製上面已驗證嘅 Quickstart,再喺本機啟動伺服器。Hotaru 0.8.x 仍然處於實驗階段。

cargo add hotaru