From c254b1e3f2980de8dafc46abb637a94de82e5c62 Mon Sep 17 00:00:00 2001 From: Samuel Gomez Date: Tue, 28 Jan 2025 19:54:45 +0000 Subject: [PATCH] feat: implementing knowledge endpoint --- api/src/logic/knowledge.rs | 35 +++++++++++++++++++++++++++++ api/src/logic/mod.rs | 1 + api/src/logic/passthrough.rs | 2 ++ api/src/router/secured_key.rs | 4 +++- api/src/server.rs | 8 ++++++- entities/src/domain/constant/mod.rs | 1 + unified/src/unified.rs | 5 +++-- 7 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 api/src/logic/knowledge.rs diff --git a/api/src/logic/knowledge.rs b/api/src/logic/knowledge.rs new file mode 100644 index 0000000..13b439c --- /dev/null +++ b/api/src/logic/knowledge.rs @@ -0,0 +1,35 @@ +use super::{read, HookExt, PublicExt, RequestExt}; +use crate::server::{AppState, AppStores}; +use axum::{routing::get, Router}; +use entities::{Id, MongoStore}; +use fake::Dummy; +use serde::{Deserialize, Serialize}; +use std::sync::Arc; + +pub fn get_router() -> Router> { + Router::new() + .route("/", get(read::)) + .route("/:id", get(read::)) +} + +struct ReadRequest; + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Dummy)] +#[serde(rename_all = "camelCase")] +pub struct Knowledge { + #[serde(rename = "_id")] + pub id: Id, + pub connection_platform: String, + pub title: String, + pub knowledge: Option, +} + +impl HookExt for ReadRequest {} +impl PublicExt for ReadRequest {} +impl RequestExt for ReadRequest { + type Output = Knowledge; + + fn get_store(stores: AppStores) -> MongoStore { + stores.knowledge + } +} diff --git a/api/src/logic/mod.rs b/api/src/logic/mod.rs index ce68791..d3899a4 100644 --- a/api/src/logic/mod.rs +++ b/api/src/logic/mod.rs @@ -31,6 +31,7 @@ pub mod connection_oauth_definition; pub mod event_access; pub mod event_callback; pub mod events; +pub mod knowledge; pub mod metrics; pub mod oauth; pub mod openapi; diff --git a/api/src/logic/passthrough.rs b/api/src/logic/passthrough.rs index 9424e2c..04e0324 100644 --- a/api/src/logic/passthrough.rs +++ b/api/src/logic/passthrough.rs @@ -53,6 +53,8 @@ pub async fn passthrough_request( ) .await?; + tracing::info!("Executing {} request on {}", method, uri.path()); + let destination = Destination { platform: connection.platform.clone(), action: Action::Passthrough { diff --git a/api/src/router/secured_key.rs b/api/src/router/secured_key.rs index 9d5d80e..56b01eb 100644 --- a/api/src/router/secured_key.rs +++ b/api/src/router/secured_key.rs @@ -5,7 +5,8 @@ use crate::{ connection_model_schema::{ public_get_connection_model_schema, PublicGetConnectionModelSchema, }, - event_access, events, metrics, oauth, passthrough, secrets, unified, vault_connection, + event_access, events, knowledge, metrics, oauth, passthrough, secrets, unified, + vault_connection, }, middleware::{ header_auth, @@ -36,6 +37,7 @@ pub async fn get_router(state: &Arc) -> Router> { .nest("/connections", connection::get_router()) .nest("/event-access", event_access::get_router()) .nest("/events", events::get_router()) + .nest("/knowledge", knowledge::get_router()) .nest("/metrics", metrics::get_router()) .nest("/oauth", oauth::get_router()) .nest("/passthrough", passthrough::get_router()) diff --git a/api/src/server.rs b/api/src/server.rs index 205d879..d1c3720 100644 --- a/api/src/server.rs +++ b/api/src/server.rs @@ -1,7 +1,10 @@ use crate::{ domain::{ConnectionsConfig, K8sMode, Metric}, helper::{K8sDriver, K8sDriverImpl, K8sDriverLogger}, - logic::{connection_oauth_definition::FrontendOauthConnectionDefinition, openapi::OpenAPIData}, + logic::{ + connection_oauth_definition::FrontendOauthConnectionDefinition, knowledge::Knowledge, + openapi::OpenAPIData, + }, router, }; use anyhow::{anyhow, Context, Result}; @@ -50,6 +53,7 @@ pub struct AppStores { pub public_connection: MongoStore, pub public_connection_details: MongoStore, pub public_model_schema: MongoStore, + pub knowledge: MongoStore, pub secrets: MongoStore, pub settings: MongoStore, } @@ -105,6 +109,7 @@ impl Server { let connection_config = MongoStore::new(&db, &Store::ConnectionDefinitions).await?; let event_access = MongoStore::new(&db, &Store::EventAccess).await?; let event = MongoStore::new(&db, &Store::Events).await?; + let knowledge = MongoStore::new(&db, &Store::ConnectionModelDefinitions).await?; let clients = MongoStore::new(&db, &Store::Clients).await?; let secrets_store = MongoStore::::new(&db, &Store::Secrets).await?; @@ -152,6 +157,7 @@ impl Server { public_connection_details, connection_config, event_access, + knowledge, event, clients, }; diff --git a/entities/src/domain/constant/mod.rs b/entities/src/domain/constant/mod.rs index 671e20b..92d24c4 100644 --- a/entities/src/domain/constant/mod.rs +++ b/entities/src/domain/constant/mod.rs @@ -101,6 +101,7 @@ pub const PAGE_SIZE_KEY: &str = "pageSize"; pub const PAGINATION_KEY: &str = "pagination"; pub const STATUS_HEADER_KEY: &str = "response-status"; pub const META_KEY: &str = "meta"; +pub const ACTION_KEY: &str = "action"; // Database constants pub const MAX_LIMIT: usize = 100; diff --git a/unified/src/unified.rs b/unified/src/unified.rs index 647fa0e..d5aead1 100644 --- a/unified/src/unified.rs +++ b/unified/src/unified.rs @@ -713,8 +713,11 @@ fn build_unified_response( tracing::info!("No response body to map for this action. ID: {}", config.id); } + let mut builder = Response::builder(); + // Insert passthrough data if needed if is_passthrough { + builder = builder.header(ACTION_KEY.to_string(), config.title.to_string()); if let Some(passthrough) = passthrough { if let Value::Object(ref mut resp) = response { resp.insert(PASSTHROUGH_KEY.to_string(), passthrough); @@ -747,8 +750,6 @@ fn build_unified_response( resp.insert(META_KEY.to_string(), metadata_value.as_value().clone()); } - let mut builder = Response::builder(); - if status.is_success() { builder = builder .header::<&'static str, HeaderValue>(STATUS_HEADER_KEY, status.as_u16().into())