juiz_core 0.0.13

Core library for JUIZ application. This package is not required if you want to build juiz functional module.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use axum::{
    extract::{ConnectInfo, Multipart, Path, Query, State},
    http::HeaderMap,
    response::IntoResponse,
    routing, Json, Router,
};
use juiz_sdk::anyhow;
use reqwest::StatusCode;
use std::{
    net::SocketAddr,
    sync::{Arc, Mutex},
};

use crate::brokers::crud_broker::CRUDBroker;
use crate::{
    brokers::http::http_router::{multipart_to_capsule_map, system, FullQuery},
    prelude::*,
};

use super::{full_query_to_map, json_output_wrap};
use utoipa::OpenApi;

#[utoipa::path(
    post,
    path = "/api/{class_name}/{function_name}",
    params(
        FullQuery,
    ),
    request_body = Value,
    responses(
        (status = 200, description = "Post object parameter", body = [String])
    ),
    tag = "universal.any",
)]
pub async fn object_post_handler(
    Path((class_name, function_name)): Path<(String, String)>,
    query: Query<FullQuery>, //, path_query: Query<PathQuery>,
    headers: HeaderMap,
    remote_addr: ConnectInfo<SocketAddr>,
    State(crud_broker): State<Arc<Mutex<CRUDBroker>>>,
    Json(body): Json<Value>,
) -> impl IntoResponse {
    let map = full_query_to_map(&query);
    log::trace!("[POST] HTTPBroker/object_post_handler({class_name}, {function_name}, {body}, {map:?}) called");
    let v = tokio::task::spawn_blocking(move || {
        juiz_lock(&crud_broker).unwrap().create_class(
            class_name.as_str(),
            function_name.as_str(),
            construct_capsule_map(
                // リクエストのボディ (マニフェストなど) を引数として渡す (issue #19)
                body_to_capsule_map(body, &headers)?,
                "CREATE",
                class_name.as_str(),
                function_name.as_str(),
                query,
                headers,
                remote_addr,
            ),
        )
    })
    .await;
    let r = json_output_wrap(v.unwrap());
    r
}

fn body_to_capsule_map(body: Value, _headers: &HeaderMap) -> Result<CapsuleMap, anyhow::Error> {
    body.try_into()
}

/// 自分自身の HTTP ブローカーを指す識別子 (http://<host>:<port>/...) を core://core/... に書き換える。
///
/// process/list などは core のオブジェクトを http://<Host ヘッダ>/... の形で返すので、
/// それをそのまま同じサーバーに渡されたときは、自分宛てに HTTP で再リクエストせず
/// ローカルのオブジェクトとして扱う。そうしないと処理中のリクエストが持つロックを
/// 2 本目のリクエストが待ち続けてタイムアウトする (issue #43)。
fn localize_identifier(identifier: &str, host: Option<&str>) -> String {
    let Some(host) = host else {
        return identifier.to_owned();
    };
    let Some(rest) = identifier.strip_prefix("http://") else {
        return identifier.to_owned();
    };
    let Some((broker_name, path)) = rest.split_once('/') else {
        return identifier.to_owned();
    };
    if is_same_broker(broker_name, host) {
        format!("core://core/{path}")
    } else {
        identifier.to_owned()
    }
}

/// broker_name (host:port) が、Host ヘッダ (host[:port]) で受けたこのサーバー自身を指すか。
/// localhost / 127.0.0.1 / 0.0.0.0 / ::1 はポートが同じなら同一とみなす。
fn is_same_broker(broker_name: &str, host: &str) -> bool {
    if broker_name == host {
        return true;
    }
    fn split_host_port(s: &str) -> (&str, &str) {
        match s.rsplit_once(':') {
            Some((h, p)) if !p.contains(']') => (h, p),
            _ => (s, "80"),
        }
    }
    fn is_loopback(h: &str) -> bool {
        matches!(h, "localhost" | "127.0.0.1" | "0.0.0.0" | "[::1]" | "[::]")
    }
    let (b_host, b_port) = split_host_port(broker_name);
    let (h_host, h_port) = split_host_port(host);
    b_port == h_port && (b_host == h_host || (is_loopback(b_host) && is_loopback(h_host)))
}

fn construct_capsule_map(
    mut capsule_map: CapsuleMap,
    method_name: &str,
    class_name: &str,
    function_name: &str,
    query: Query<FullQuery>,
    headers: HeaderMap,
    remote_addr: ConnectInfo<SocketAddr>,
) -> CapsuleMap {
    capsule_map.set_param("method_name", method_name);
    capsule_map.set_param("class_name", class_name);
    capsule_map.set_param("function_name", function_name);
    match query.identifier.clone() {
        None => {}
        Some(v) => {
            let host = headers.get("host").and_then(|h| h.to_str().ok());
            capsule_map.set_param("identifier", localize_identifier(v.as_str(), host).as_str());
        }
    }
    match query.path.clone() {
        None => {}
        Some(v) => {
            capsule_map.set_param("path", v.as_str());
        }
    }
    match query.recursive.clone() {
        None => {}
        Some(v) => {
            capsule_map.set_param("recursive", v.as_str());
        }
    }
    match query.system_uuid.clone() {
        None => {}
        Some(v) => {
            capsule_map.set_param("system_uuid", v.as_str());
        }
    }
    match query.topic_name.clone() {
        None => {}
        Some(v) => {
            capsule_map.set_param("topic_name", v.as_str());
        }
    }
    // println!("HEADER>>>> {headers:?}");
    match headers.get("host") {
        Some(header) => match header.to_str() {
            Ok(host) => {
                let accessed_broker_id = format!("http://{}", host);
                capsule_map.set_param("accessed_broker_id", accessed_broker_id.as_str());

                let accessed_broker_type = "http".to_owned();
                capsule_map.set_param("accessed_broker_type", accessed_broker_type.as_str());
                let accessed_broker_name = host.to_owned();
                capsule_map.set_param("accessed_broker_name", accessed_broker_name.as_str());
            }
            Err(_) => {}
        },
        None => {}
    }
    let remote_addr_str = remote_addr.0.to_string().as_str().to_owned();
    capsule_map.set_param("remote_addr", remote_addr_str.as_str());

    if class_name == "system" && function_name == "add_mastersystem" {
        let _r = match capsule_map.get("profile") {
            Ok(capsule_ptr) => capsule_ptr.lock_modify_as_value(|v| {
                match v
                    .as_object_mut()
                    .unwrap()
                    .get_mut("subsystem")
                    .unwrap()
                    .as_object_mut()
                {
                    Some(obj) => {
                        let broker_name =
                            obj.get("broker_name").unwrap().as_str().unwrap().to_owned();
                        let broker_tokens = broker_name.split(":").collect::<Vec<&str>>();
                        let port_str = broker_tokens.get(1).unwrap();
                        let remote_tokens = remote_addr_str.split(":").collect::<Vec<&str>>();
                        let addr_str = (*remote_tokens.get(0).unwrap()).to_owned();

                        let new_broker_name = addr_str + ":" + port_str;
                        obj.insert("broker_name".to_owned(), jvalue!(new_broker_name));
                    }
                    None => todo!(),
                }
            }),
            Err(_) => todo!(),
        };
    }

    capsule_map
}

#[utoipa::path(
    patch,
    path = "/api/{class_name}/{function_name}",
    params(
        FullQuery
    ),
    request_body = Value,
    responses(
        (status = 200, description = "Get object parameter", body = [String])
    ),
    tag = "universal.any",
)]
pub async fn object_patch_handler(
    Path((class_name, function_name)): Path<(String, String)>,
    query: Query<FullQuery>,
    headers: HeaderMap,
    // multipart: Multipart,
    remote_addr: ConnectInfo<SocketAddr>,
    State(crud_broker): State<Arc<Mutex<CRUDBroker>>>,
    Json(body): Json<Value>,
) -> impl IntoResponse {
    let map = full_query_to_map(&query);
    log::trace!("[PATCH] ({class_name}, {function_name}, {body}, {map:?})が呼ばれました");
    let v = tokio::task::spawn_blocking(move || {
        juiz_lock(&crud_broker).unwrap().update_class(
            class_name.as_str(),
            function_name.as_str(),
            construct_capsule_map(
                body_to_capsule_map(body, &headers)?,
                "UPDATE",
                class_name.as_str(),
                function_name.as_str(),
                query,
                headers,
                remote_addr,
            ),
        )
    })
    .await;
    log::debug!("【patch】 Resultは{v:?}");
    let r = json_output_wrap(v.unwrap());
    r
}

#[utoipa::path(
    put,
    path = "/api/{class_name}/{function_name}",
    params(
        FullQuery
    ),
    request_body(content_type = "multipart/formdata", content = MultiPart),
    responses(
        (status = 200, description = "Get object parameter", body = [String])
    ),
    tag = "universal.any",
)]
pub async fn object_put_handler(
    Path((class_name, function_name)): Path<(String, String)>,
    query: Query<FullQuery>,
    headers: HeaderMap,
    //multipart: Multipart,
    remote_addr: ConnectInfo<SocketAddr>,
    State(crud_broker): State<Arc<Mutex<CRUDBroker>>>,
    multipart: Multipart,
) -> impl IntoResponse {
    let map = full_query_to_map(&query);
    log::trace!("[PUT] ({class_name}, {function_name}, {map:?}, {multipart:?}) called");
    match multipart_to_capsule_map(multipart).await {
        Ok(capsule_map) => {
            let v = tokio::task::spawn_blocking(move || {
                juiz_lock(&crud_broker).unwrap().update_class(
                    class_name.as_str(),
                    function_name.as_str(),
                    construct_capsule_map(
                        capsule_map,
                        "UPDATE",
                        class_name.as_str(),
                        function_name.as_str(),
                        query,
                        headers,
                        remote_addr,
                    ),
                )
            })
            .await;
            let r = json_output_wrap(v.unwrap());
            r.into_response()
        }
        Err(e) => {
            log::error!("multipart_to_capsule_map() failed. Err({e:?})");
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(jvalue!({
                    "message": format!("Internal Server Error:  {:#}, {:}", e, e.to_string())
                })),
            )
                .into_response()
        }
    }
}

#[utoipa::path(
    get,
    path = "/api/{class_name}/{function_name}",
    //context_path = "{full_path}", 
    params(
        //FullQuery
        ("query" = FullQuery, Query, deprecated = false, description = ""),
        ("Host" = String, Header, deprecated = false, description = "")
    ),
    responses(
        (status = 200, description = "Get object parameter", body = [String])
    ),
    tag = "universal.any",
)]
pub async fn object_get_handler(
    Path((class_name, function_name)): Path<(String, String)>,
    query: Query<FullQuery>,
    headers: HeaderMap,
    remote_addr: ConnectInfo<SocketAddr>,
    State(crud_broker): State<Arc<Mutex<CRUDBroker>>>,
) -> impl IntoResponse {
    //let host = "";
    let full_path = "";
    let map = full_query_to_map(&query);
    log::trace!(
        "[GET] ({class_name}, {function_name}, {map:?}, {full_path:?}, {headers:?}) called"
    );
    if class_name == "system" && function_name == "openapi.json" {
        return system::system_openapi_handler().into_response();
    } else {
        let v = tokio::task::spawn_blocking(move || {
            juiz_lock(&crud_broker).unwrap().read_class(
                class_name.as_str(),
                function_name.as_str(),
                construct_capsule_map(
                    CapsuleMap::new(),
                    "READ",
                    class_name.as_str(),
                    function_name.as_str(),
                    query,
                    headers,
                    remote_addr,
                ),
            )
        })
        .await;
        return json_output_wrap(v.unwrap()).into_response();
    }
}

#[utoipa::path(
    delete,
    path = "/api/{class_name}/{function_name}",
    params(
        FullQuery
    ),
    responses(
        (status = 200, description = "Delete object parameter", body = [String])
    ),
    tag = "universal.any"
)]
pub async fn object_delete_handler(
    Path((class_name, function_name)): Path<(String, String)>,
    query: Query<FullQuery>,
    headers: HeaderMap,
    remote_addr: ConnectInfo<SocketAddr>,
    State(crud_broker): State<Arc<Mutex<CRUDBroker>>>,
) -> impl IntoResponse {
    let map = full_query_to_map(&query);
    log::trace!("HTTPBroker/object_delete_handler({class_name}, {map:?}) called");
    let v = tokio::task::spawn_blocking(move || {
        juiz_lock(&crud_broker).unwrap().delete_class(
            class_name.as_str(),
            function_name.as_str(),
            construct_capsule_map(
                CapsuleMap::new(),
                "DELETE",
                class_name.as_str(),
                function_name.as_str(),
                query,
                headers,
                remote_addr,
            ),
        )
    })
    .await;
    let r = json_output_wrap(v.unwrap());
    r
}

pub fn object_router(crud_broker: Arc<Mutex<CRUDBroker>>) -> Router {
    Router::new()
        .route(
            "/:class_name/:function_name",
            routing::patch(object_patch_handler)
                .get(object_get_handler)
                .delete(object_delete_handler)
                .post(object_post_handler)
                .put(object_put_handler),
        )
        .with_state(Arc::clone(&crud_broker))
}

#[derive(OpenApi)]
#[openapi(
    paths(
        object_get_handler,
        object_patch_handler,
        object_delete_handler,
        object_post_handler,
        object_put_handler,
    ),
    components(schemas())
)]
#[allow(unused)]
pub struct ApiDoc;

#[cfg(test)]
mod tests {
    use super::localize_identifier;

    #[test]
    fn localize_identifier_rewrites_own_http_identifier() {
        let id = "http://127.0.0.1:8000/process/inc0::inc";
        assert_eq!(
            localize_identifier(id, Some("127.0.0.1:8000")),
            "core://core/process/inc0::inc"
        );
        assert_eq!(
            localize_identifier(id, Some("localhost:8000")),
            "core://core/process/inc0::inc"
        );
        assert_eq!(
            localize_identifier(
                "http://192.168.0.10:8000/container_process/get0::get:counter0",
                Some("192.168.0.10:8000")
            ),
            "core://core/container_process/get0::get:counter0"
        );
    }

    #[test]
    fn localize_identifier_keeps_other_identifiers() {
        let other_port = "http://127.0.0.1:8001/process/inc0::inc";
        assert_eq!(
            localize_identifier(other_port, Some("127.0.0.1:8000")),
            other_port
        );
        let other_host = "http://192.168.0.11:8000/process/inc0::inc";
        assert_eq!(
            localize_identifier(other_host, Some("192.168.0.10:8000")),
            other_host
        );
        let core = "core://core/process/inc0::inc";
        assert_eq!(localize_identifier(core, Some("127.0.0.1:8000")), core);
        let id = "http://127.0.0.1:8000/process/inc0::inc";
        assert_eq!(localize_identifier(id, None), id);
    }
}