Expand description
fandhe-backend の最小コア。
§このクレートの役割
crates/core は HTTP/1.1 パーサ・keep-alive・3 種拡張点
(Middleware / UpgradeHandler / RequestGate、extension モジュール)を
実装する最小コアの置き場所。TASK-1.4-1(#69)で 3 拡張点の trait 定義を、
TASK-1.4-2(#70)でコアループ(接続受理・リクエストループ、server
モジュール)による実接続を提供する。3 拡張点で表現できないリダイレクト・
レスポンス改変(interceptor::Interceptor、イシュー #420)は feature
ゲート不要の追加拡張点として同モジュールに置く(詳細は interceptor を参照)。
§workspace 内での依存方向
docs/spec/04-requirements.md REQ-1 / docs/spec/05-tasks.md TASK-1.5 の方針に従い、
workspace 全体の依存方向は次の一方向を維持する:
server → routes → http::*core(本クレート)はこの依存グラフの末端に位置し、plugin-* の固有シンボルには
一切依存しない。プラグインは feature 経由でコアの拡張点(Middleware /
UpgradeHandler / RequestGate)を実装する側であり、コアからプラグインへの
依存は発生しない(pay-for-what-you-use、.claude/rules/pay-for-what-you-use.md)。
fandhe-backend-http(sans-IO な HTTP/1.1 パーサ、TASK-1.3)は workspace 内の下位層クレート
であり、外部 crates.io 依存はここでも増やさない。
crates/routes(fandhe-backend-routes)は TASK-1.5(#14)で新設し、server → routes
エッジを Cargo.toml の依存宣言として実体化した。fandhe_backend_routes::Router は
impl server::Handler for fandhe_backend_routes::Router により本クレートの
既定ハンドラとしてそのまま登録できる(crates/core/examples/minimal.rs
参照)。依存方向の機械検証は scripts/dep-direction-check.sh(TASK-1.5)が
cargo metadata の依存エッジホワイトリスト照合で行う。
§feature 一覧
webrtc-proxy(TASK-2.1 / #18):crates/plugin-webrtc-proxyをoptional = true+dep:構文で組み込み、[server::Server::webrtc_proxy] で登録したfandhe_backend_plugin_webrtc_proxy::ProxyConfigに基づきPOST /rtc/offerをパスインターセプトする。無効時(既定)は依存・コード・unsafeを一切含まない(クレート非公開のpluginモジュールの doc・docs/design/plugin-boundary.mdを参照)。webrtc(TASK-8.1 / #26):crates/plugin-webrtc(in-process 型、webrtc-rs直接依存)を同じくプラグイン境界パターンで組み込み、 [server::Server::webrtc] で登録したfandhe_backend_plugin_webrtc::WebRtcConfigに 基づきPOST /rtc/offerをパスインターセプトする。webrtc-proxyと 同時有効時はwebrtc-proxyが優先される(plugin::try_interceptの doc)。無効時(既定)は依存・コード・unsafeを一切含まない。websocket(TASK-4.1 / #22):crates/plugin-websocketをoptional = true+dep:構文で組み込み、[server::Server::websocket] で登録したfandhe_backend_plugin_websocket::WebSocketConfigの指すパス(既定/ws)へのGET+Upgrade: websocketをUpgradeHandler拡張点経由で 検知し、fandhe_backend_plugin_websocket::handle_upgradeへ完全委譲する(RFC 6455 ハンドシェイク・フレーミングはcrates/plugin-websocket側の責務)。設定型WebSocketConfigは [plugin_websocket] モジュールとして再エクスポートし (イシュー #435)、fandhe-backend-plugin-websocketへ直接依存せずに構築できる。 無効時(既定)は依存・コード・unsafeを一切含まない。graphql(TASK-2.4 / #21):crates/plugin-graphqlを組み込み、 [server::Server::graphql] へ渡す設定型GraphQlConfigを [plugin_graphql] モジュールとして再エクスポートする(イシュー #435)。 利用者はfandhe-backend-plugin-graphqlへ直接依存せずにfandhe_backend_core::plugin_graphql::GraphQlConfigを構築できる。 無効時(既定)は依存・コード・unsafeを一切含まない。cors(イシュー #305):crates/plugin-corsを組み込み、 [server::Server::cors] へ渡す設定型CorsConfigを [plugin_cors] モジュールとして再エクスポートする(イシュー #435)。 利用者はfandhe-backend-plugin-corsへ直接依存せずにfandhe_backend_core::plugin_cors::CorsConfigを構築できる。 無効時(既定)は依存・コード・unsafeを一切含まない。tracing(TASK-10.1 / #56):crates/plugin-tracingを組み込み、 [server::Server::tracing] へ渡す設定型TracingConfigを [plugin_tracing] モジュールとして再エクスポートする(イシュー #435)。 利用者はfandhe-backend-plugin-tracingへ直接依存せずにfandhe_backend_core::plugin_tracing::TracingConfigを構築できる。 無効時(既定)は依存・コード・unsafeを一切含まない。openapi(TASK-2.1 / #256):crates/plugin-openapiを組み込み、 [server::Server::openapi_with] へ渡す設定型OpenApiDocを [plugin_openapi] モジュールとして再エクスポートする(イシュー #435)。 利用者はfandhe-backend-plugin-openapiへ直接依存せずにfandhe_backend_core::plugin_openapi::OpenApiDocを構築できる。 無効時(既定)は依存・コード・unsafeを一切含まない。static(イシュー #318):crates/plugin-staticを組み込み、 [server::Server::static_files] へ渡す設定型StaticFilesConfigを [plugin_static] モジュールとして再エクスポートする(イシュー #421)。 利用者はfandhe-backend-plugin-staticへ直接依存せずにfandhe_backend_core::plugin_static::StaticFilesConfigを構築できる。 無効時(既定)は依存・コード・unsafeを一切含まない。compression(イシュー #321):crates/plugin-compressionを組み込み、 [server::Server::compression] へ渡す設定型CompressionConfigを [plugin_compression] モジュールとして再エクスポートする(イシュー #421)。 利用者はfandhe-backend-plugin-compressionへ直接依存せずにfandhe_backend_core::plugin_compression::CompressionConfigを構築できる。 無効時(既定)は依存・コード・unsafeを一切含まない。
webrtc / webrtc-proxy の設定型 WebRtcConfig / ProxyConfig も同様に
それぞれ [plugin_webrtc] / [plugin_webrtc_proxy] モジュールとして
再エクスポートする(イシュー #435)。
§今後のタスクとの対応
- TASK-2.1(#18)で確立したプラグイン境界パターン(feature flag +
dep:構文、cfg-free なコアループ + 固定シグネチャシーム)は非公開pluginモジュールのtry_intercept(パスインターセプト型)・try_handle_upgrade(Upgrade 型、TASK-4.1 / #22 で実装)の 2 種として 実証済みであり、後続プラグインはdocs/design/plugin-boundary.mdの 適用手順に従って同パターンを踏襲する
§クイックスタート(TASK-11.5 / #95、docs/guide/getting-started.md の裏取り)
Server に fandhe_backend_routes::Router を 1 件登録するだけで最小サーバが
組み立てられる。bind → run の流れは crates/core/examples/minimal.rs
(cargo run --example minimal -p fandhe-backend-core で実行可能)と同一。
本 doc test は no_run(実際に listen はしない)だが cargo test --doc で
コンパイル可能性を機械検証する(AI ファースト保守性、
.claude/rules/feature-modification.md の「実装にはテスト追加を伴う」)。
use fandhe_backend_core::Server;
use fandhe_backend_http::response::Response;
use fandhe_backend_routes::Router;
let router = Router::new().route("GET", "/", |_head, _body| {
Response::new(200, b"hello\n".to_vec())
});
let server = Server::new().handler(router);
// 外部公開する場合は呼び出し側の責任でバインドアドレスを明示する
// (.claude/rules/security.md の攻撃表面最小化)。ここではループバック限定。
let bound = server.bind("127.0.0.1:3000").await?;
bound.run().awaitプロセス終了を run() の kill に頼らず、accept 停止 → in-flight
完了待ち(上限時間・超過時強制クローズ)を伴って安全に停止したい場合は
server::BoundServer::run_until(イシュー #313)を使う。run() は
run_until への薄い委譲であり後方互換を維持する。利用例は
crates/core/examples/graceful_shutdown.rs、設計判断は
docs/design/graceful-shutdown.md を参照。
Re-exports§
pub use extension::GateContext;pub use extension::GateOutcome;pub use extension::Middleware;pub use extension::RequestGate;pub use extension::UpgradeHandler;pub use interceptor::Interceptor;pub use diagnostics::DiagnosticEvent;pub use diagnostics::Diagnostics;pub use diagnostics::StderrDiagnostics;pub use server::BoundServer;pub use server::Handler;pub use server::RebindHandle;pub use server::Server;pub use server::handle_connection;pub use server::handle_connection_with_peer_addr;pub use streaming::BodyWriter;pub use streaming::StreamClosed;pub use streaming::StreamingResponse;
Modules§
- diagnostics
- ライブラリ内部の実行時診断を利用側で差し替え可能にする診断シンク (イシュー #720)。
- extension
- コアが公開する 3 種の拡張点(
Middleware/UpgradeHandler/RequestGate)。 - interceptor
- ユーザー向けインターセプト・レスポンス改変拡張点
Interceptor(イシュー #420)。 - server
- コアループ(接続受理・リクエストループ)と 3 拡張点の実接続(TASK-1.4-2 / #70)。
- streaming
- レスポンス側 chunked ストリーミング送信の opt-in API(イシュー #319)。
Functions§
- version
- このクレートのバージョン文字列を返す。