kotoba-server 0.1.16

Complete HTTP server and frontend integration system for Kotoba graph database
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! HTTPサーバー
//!
//! このモジュールはHTTPサーバーのメインインターフェースを提供します。

use kotoba_core::types::Result;
use crate::http::ir::*;
use crate::http::parser::HttpConfigParser;
use crate::http::engine::{HttpEngine, RawHttpRequest};
use crate::http::graphql::*;
// use kotoba_storage::prelude::*; // Storage crate has issues
use kotoba_rewrite::prelude::*;
use kotoba_schema::manager::SchemaManager;
use std::sync::Arc;
use std::path::Path;
use tokio::net::{TcpListener, TcpStream};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::collections::HashMap;

/// HTTPサーバー
pub struct HttpServer {
    engine: HttpEngine,
    listener: Option<TcpListener>,
    graphql_schema: Option<KotobaSchema>,
}

impl HttpServer {
    /// 新しいHTTPサーバーを作成
    pub async fn new(
        config: HttpConfig,
        mvcc: Arc<MVCCManager>,
        merkle: Arc<MerkleDAG>,
        rewrite_engine: Arc<RewriteEngine>,
    ) -> Result<Self> {
        let engine = HttpEngine::new(config, mvcc, merkle, rewrite_engine).await?;
        Ok(Self {
            engine,
            listener: None,
            graphql_schema: None,
        })
    }

    /// GraphQL スキーマを有効化
    pub fn enable_graphql(&mut self, schema_manager: Arc<SchemaManager>) {
        self.graphql_schema = Some(create_schema(schema_manager));
    }

    /// 設定に基づいてGraphQLを有効化
    pub fn enable_graphql_if_configured(&mut self, schema_manager: Arc<SchemaManager>) {
        if self.engine.config.server.graphql_enabled.unwrap_or(false) {
            self.enable_graphql(schema_manager);
        }
    }

    /// 設定ファイルからHTTPサーバーを作成
    pub async fn from_config_file<P: AsRef<Path>>(
        config_path: P,
        mvcc: Arc<MVCCManager>,
        merkle: Arc<MerkleDAG>,
        rewrite_engine: Arc<RewriteEngine>,
    ) -> Result<Self> {
        let config = HttpConfigParser::parse(config_path)?;
        let mut server = Self::new(config, mvcc, merkle, rewrite_engine).await?;
        // TODO: GraphQL を設定ファイルから有効化できるようにする
        Ok(server)
    }

    /// サーバーを開始
    pub async fn start(&mut self) -> Result<()> {
        let config = self.engine.get_config();
        let address = format!("{}:{}", config.server.host, config.server.port);

        println!("Starting Kotoba HTTP server on {}", address);
        let listener = TcpListener::bind(&address).await?;
        self.listener = Some(listener);

        println!("Server started successfully");
        Ok(())
    }

    /// サーバーを実行(メインループ)
    pub async fn run(&self) -> Result<()> {
        if let Some(listener) = &self.listener {
            println!("Accepting connections...");

            loop {
                let (socket, addr) = listener.accept().await?;
                println!("New connection from: {}", addr);

                // engineをクローンして渡す(ライフタイム問題を解決)
                let engine = self.engine.clone();
                tokio::spawn(async move {
                    if let Err(e) = Self::handle_connection(socket, &engine).await {
                        eprintln!("Error handling connection: {}", e);
                    }
                });
            }
        } else {
            Err(crate::types::KotobaError::InvalidArgument(
                "Server not started. Call start() first.".to_string()
            ))
        }
    }

    /// 個別の接続を処理
    async fn handle_connection(mut socket: TcpStream, engine: &HttpEngine) -> Result<()> {
        let mut buffer = [0; 8192]; // 8KB buffer

        // HTTPリクエストを読み取り
        let n = socket.read(&mut buffer).await?;
        if n == 0 {
            return Ok(()); // Connection closed
        }

        let request_data = &buffer[..n];
        let request_str = String::from_utf8_lossy(request_data);

        // HTTPリクエストをパース
        let raw_request = Self::parse_http_request(&request_str)?;

        // GraphQL リクエストをチェック
        let response = if raw_request.path == "/graphql" && self.graphql_schema.is_some() {
            self.handle_graphql_request(raw_request).await?
        } else {
            engine.handle_request(raw_request).await?
        };

        // HTTPレスポンスを送信
        let response_bytes = Self::format_http_response(&response)?;
        socket.write_all(&response_bytes).await?;
        socket.flush().await?;

        Ok(())
    }

    /// GraphQL リクエストを処理
    async fn handle_graphql_request(&self, raw_request: RawHttpRequest) -> Result<HttpResponse> {
        if let Some(schema) = &self.graphql_schema {
            // リクエストボディから GraphQL クエリを取得
            let request_body = String::from_utf8(raw_request.body)
                .map_err(|e| crate::types::KotobaError::InvalidArgument(
                    format!("Invalid UTF-8 in request body: {}", e)
                ))?;

            // GraphQL リクエストを実行
            let response_json = graphql_handler(schema, request_body).await
                .map_err(|e| crate::types::KotobaError::InvalidArgument(
                    format!("GraphQL execution failed: {}", e)
                ))?;

            // HTTP レスポンスを作成
            let mut headers = HashMap::new();
            headers.insert("content-type".to_string(), "application/json".to_string());
            headers.insert("access-control-allow-origin".to_string(), "*".to_string());
            headers.insert("access-control-allow-methods".to_string(), "GET, POST, OPTIONS".to_string());
            headers.insert("access-control-allow-headers".to_string(), "Content-Type".to_string());

            Ok(HttpResponse::new(
                raw_request.id,
                HttpStatus::ok(),
                HttpHeaders { headers },
                Some(ContentHash::sha256(response_json.as_bytes())),
            ))
        } else {
            Ok(HttpResponse::new(
                raw_request.id,
                HttpStatus::internal_server_error(),
                HttpHeaders::new(),
                None,
            ))
        }
    }

    /// HTTPリクエスト文字列をパース
    fn parse_http_request(request_str: &str) -> Result<RawHttpRequest> {
        let mut lines = request_str.lines();

        // リクエストラインをパース
        let request_line = lines.next().ok_or_else(|| {
            crate::types::KotobaError::InvalidArgument("Empty request".to_string())
        })?;

        let parts: Vec<&str> = request_line.split_whitespace().collect();
        if parts.len() < 2 {
            return Err(crate::types::KotobaError::InvalidArgument(
                "Invalid request line".to_string()
            ));
        }

        let method = parts[0].to_string();
        let mut path_and_query = parts[1].to_string();

        // クエリパラメータを分離
        let (path, query_string) = if let Some((p, q)) = path_and_query.split_once('?') {
            (p.to_string(), Some(q.to_string()))
        } else {
            (path_and_query, None)
        };

        let mut request = RawHttpRequest::new(method, path);
        if let Some(query) = query_string {
            request = request.with_query(query);
        }

        // ヘッダーをパース
        let mut headers = Vec::new();
        for line in lines {
            if line.is_empty() {
                break; // ヘッダーの終わり
            }

            if let Some((key, value)) = line.split_once(": ") {
                headers.push((key.to_string(), value.to_string()));
            }
        }

        for (key, value) in headers {
            request = request.with_header(key, value);
        }

        // ボディをパース(残りのデータ)
        let body_start = request_str.find("\r\n\r\n")
            .map(|pos| pos + 4)
            .unwrap_or(request_str.len());
        let body = request_str[body_start..].as_bytes().to_vec();
        request = request.with_body(body);

        Ok(request)
    }

    /// HTTPレスポンスをフォーマット
    fn format_http_response(response: &HttpResponse) -> Result<Vec<u8>> {
        let mut response_str = format!(
            "HTTP/1.1 {} {}\r\n",
            response.status.code, response.status.reason
        );

        // ヘッダーを追加
        for (key, value) in &response.headers.headers {
            response_str.push_str(&format!("{}: {}\r\n", key, value));
        }

        // Content-Lengthヘッダー
        if let Some(body_ref) = &response.body_ref {
            // TODO: 実際のボディサイズを取得
            response_str.push_str(&format!("Content-Length: 0\r\n"));
        } else {
            response_str.push_str("Content-Length: 0\r\n");
        }

        response_str.push_str("\r\n");

        // ボディを追加(実際の実装では外部ストレージから取得)
        let mut response_bytes = response_str.into_bytes();
        if let Some(_body_ref) = &response.body_ref {
            // TODO: ボディコンテンツを取得して追加
            // response_bytes.extend(body_content);
        }

        Ok(response_bytes)
    }

    /// サーバーの状態を取得
    pub async fn get_status(&self) -> ServerStatus {
        let state = self.engine.get_state().await;
        let config = self.engine.get_config();

        ServerStatus {
            host: config.server.host.clone(),
            port: config.server.port,
            requests_processed: state.requests_processed,
            uptime_seconds: state.uptime_seconds,
            routes_count: config.routes.len(),
            middlewares_count: config.middlewares.len(),
        }
    }
}

/// サーバーのステータス情報
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ServerStatus {
    pub host: String,
    pub port: u16,
    pub requests_processed: u64,
    pub uptime_seconds: u64,
    pub routes_count: usize,
    pub middlewares_count: usize,
}

/// サーバービルダー(設定ファイルからサーバーを作成するためのヘルパー)
pub struct ServerBuilder {
    config_path: Option<String>,
    config: Option<HttpConfig>,
}

impl ServerBuilder {
    pub fn new() -> Self {
        Self {
            config_path: None,
            config: None,
        }
    }

    /// 設定ファイルパスを設定
    pub fn with_config_file<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.config_path = Some(path.as_ref().to_string_lossy().to_string());
        self
    }

    /// 直接設定を設定
    pub fn with_config(mut self, config: HttpConfig) -> Self {
        self.config = Some(config);
        self
    }

    /// サーバーを構築
    pub async fn build(
        self,
        mvcc: Arc<MVCCManager>,
        merkle: Arc<MerkleDAG>,
        rewrite_engine: Arc<RewriteEngine>,
    ) -> Result<HttpServer> {
        let config = if let Some(config) = self.config {
            config
        } else if let Some(path) = self.config_path {
            HttpConfigParser::parse(path)?
        } else {
            return Err(crate::types::KotobaError::InvalidArgument(
                "Either config file or config object must be provided".to_string()
            ));
        };

        HttpServer::new(config, mvcc, merkle, rewrite_engine).await
    }
}

impl Default for ServerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// 簡単なHTTPサーバー起動関数
pub async fn run_server<P: AsRef<Path>>(
    config_path: P,
    mvcc: Arc<MVCCManager>,
    merkle: Arc<MerkleDAG>,
    rewrite_engine: Arc<RewriteEngine>,
) -> Result<()> {
    let mut server = HttpServer::from_config_file(
        config_path,
        mvcc,
        merkle,
        rewrite_engine,
    ).await?;

    server.start().await?;
    server.run().await?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    // use kotoba_storage::prelude::*; // Storage crate has issues
    use kotoba_rewrite::prelude::*;

    #[test]
    fn test_parse_http_request() {
        let request_str = "GET /ping?key=value HTTP/1.1\r\n\
                          Host: localhost:8080\r\n\
                          User-Agent: test\r\n\
                          \r\n\
                          test body";

        let request = HttpServer::parse_http_request(request_str).unwrap();

        assert_eq!(request.method, "GET");
        assert_eq!(request.path, "/ping");
        assert_eq!(request.query_string, Some("key=value".to_string()));
        assert_eq!(request.headers.len(), 2);
        assert_eq!(request.body, b"test body");
    }

    #[tokio::test]
    async fn test_server_builder() {
        let mvcc = Arc::new(MVCCManager::new());
        let merkle = Arc::new(MerkleDAG::new());
        let rewrite_engine = Arc::new(RewriteEngine::new());

        let config = HttpConfig::new(ServerConfig::default());
        let server = ServerBuilder::new()
            .with_config(config)
            .build(mvcc, merkle, rewrite_engine)
            .await
            .unwrap();

        let status = server.get_status().await;
        assert_eq!(status.host, "127.0.0.1");
        assert_eq!(status.port, 8080);
        assert_eq!(status.requests_processed, 0);
    }
}