kotoba_handler/
lib.rs

1//! Kotoba Unified Handler
2//!
3//! このクレートはKotobaエコシステム全体の統合的なhandlerを提供します。
4//! 既存のkotoba-jsonnetとkotoba-kotobasの機能を統合し、
5//! サーバー、CLI、WASM実行を統一的に扱います。
6
7pub mod error;
8pub mod types;
9pub mod handler;
10pub mod executor;
11pub mod runtime;
12pub mod integration;
13
14// TODO: Create server module
15// #[cfg(feature = "server")]
16// pub mod server;
17
18#[cfg(feature = "wasm")]
19pub mod wasm;
20
21#[cfg(feature = "websocket")]
22pub mod websocket;
23
24#[cfg(feature = "cli")]
25pub mod cli;
26
27#[cfg(feature = "web")]
28pub mod web;
29
30#[cfg(feature = "templates")]
31pub mod templates;
32
33#[cfg(feature = "database")]
34pub mod database;
35
36#[cfg(feature = "auth")]
37pub mod auth;
38
39#[cfg(feature = "dev_server")]
40pub mod dev_server;
41
42pub use error::{HandlerError, Result};
43pub use types::*;
44pub use handler::UnifiedHandler;
45pub use executor::HandlerExecutor;
46
47// Re-export KeyValueStore for convenience
48pub use kotoba_storage::KeyValueStore;
49pub use std::sync::Arc;
50
51/// Handlerの初期化と実行を簡略化するためのヘルパー関数
52// TODO: Implement server functionality
53// #[cfg(feature = "server")]
54// pub async fn run_server(addr: &str) -> Result<()> {
55//     server::run(addr).await
56// }
57
58/// WASM環境でのhandler初期化
59#[cfg(feature = "wasm")]
60pub fn init_wasm_handler() -> Result<wasm::WasmHandler> {
61    wasm::WasmHandler::new()
62}
63
64/// CLI経由でのhandler実行
65#[cfg(feature = "cli")]
66pub async fn execute_cli_handler(file: &str, args: Vec<String>) -> Result<String> {
67    cli::execute_handler(file, args).await
68}
69
70/// 最もシンプルなhandler実行関数 (ジェネリックバージョン)
71/// 使用例: execute_simple_handler_with_storage(&storage, content, context).await
72pub async fn execute_simple_handler_with_storage<T: KeyValueStore + 'static>(
73    storage: Arc<T>,
74    content: &str,
75    context: HandlerContext,
76) -> Result<String> {
77    let handler = UnifiedHandler::new(storage);
78    handler.execute(content, context).await
79}
80
81/// 最もシンプルなhandler実行関数 (デフォルト実装)
82/// 注意: この関数はKeyValueStoreを必要とするため、直接使用できません
83/// execute_simple_handler_with_storageを使用してください
84// pub async fn execute_simple_handler(content: &str, context: HandlerContext) -> Result<String> {
85//     // この関数は削除されました。execute_simple_handler_with_storageを使用してください
86//     Err(HandlerError::Config("KeyValueStoreが必要です".to_string()))
87// }
88
89/// Webアプリケーションの実行
90#[cfg(feature = "web")]
91pub async fn run_web_app(addr: &str, config: web::WebConfig) -> Result<()> {
92    web::run_web_app(addr, config).await
93}
94
95/// 開発サーバーの実行
96#[cfg(feature = "dev_server")]
97pub async fn run_dev_server(addr: &str, config: dev_server::DevServerConfig) -> Result<()> {
98    dev_server::run_dev_server(addr, config).await
99}
100
101/// データベース接続の初期化
102#[cfg(feature = "database")]
103pub async fn init_database(url: &str) -> Result<database::DatabaseConnection> {
104    database::init_connection(url).await
105}
106
107/// 認証ミドルウェアの作成
108#[cfg(feature = "auth")]
109pub fn create_auth_middleware(config: auth::AuthConfig) -> auth::AuthMiddleware {
110    auth::AuthMiddleware::new(config)
111}
112
113/// テンプレートエンジンの初期化
114#[cfg(feature = "templates")]
115pub fn init_template_engine(template_dir: &str) -> Result<templates::TemplateEngine> {
116    templates::TemplateEngine::new(template_dir)
117}
118