reactive_graph_type_system_rest/
components.rs

1use std::str::FromStr;
2use std::sync::Arc;
3
4use crate::json_schema_response;
5use actix_web::HttpResponse;
6use actix_web::get;
7use actix_web::web;
8use mime::APPLICATION_JSON;
9use reactive_graph_graph::ComponentTypeId;
10use reactive_graph_type_system_api::ComponentManager;
11use reactive_graph_type_system_json_schema::components::schema_components;
12
13#[get("/types/components")]
14pub async fn get_components(component_manager: web::Data<Arc<dyn ComponentManager + Send + Sync>>) -> HttpResponse {
15    HttpResponse::Ok().content_type(APPLICATION_JSON.to_string()).json(component_manager.get_all())
16}
17
18#[get("/types/components/{namespace:.*}")]
19pub async fn get_component(path: web::Path<String>, component_manager: web::Data<Arc<dyn ComponentManager + Send + Sync>>) -> HttpResponse {
20    let namespace = path.into_inner();
21    let ty = match ComponentTypeId::from_str(&namespace) {
22        Ok(ty) => ty,
23        Err(e) => {
24            return HttpResponse::NotFound().content_type(APPLICATION_JSON.to_string()).body(e.to_string());
25        }
26    };
27    match component_manager.get(&ty) {
28        Some(component) => HttpResponse::Ok().content_type(APPLICATION_JSON.to_string()).json(&component),
29        None => HttpResponse::NotFound()
30            .content_type(APPLICATION_JSON.to_string())
31            .body(format!("Component {ty} not found")),
32    }
33}
34
35#[get("/types/components/schema")]
36pub async fn json_schema_components() -> HttpResponse {
37    json_schema_response(schema_components())
38}