DB request for get_data
This commit is contained in:
parent
4b88966e81
commit
25f3492c08
2 changed files with 91 additions and 13 deletions
5
migrations/20240506135416_testdata.sql
Normal file
5
migrations/20240506135416_testdata.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
-- Add migration script here
|
||||||
|
|
||||||
|
INSERT INTO metadata VALUES ("bar", false, DateTime('now'), "123", 4, DateTime('now'), "customData");
|
||||||
|
|
||||||
|
INSERT INTO secret_versions VALUES ("secret_data", DateTime('now'), DateTime('now'), 1, "bar");
|
||||||
|
|
@ -7,11 +7,20 @@ pub mod structs;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
use crate::engines::kv::structs::KvSecret;
|
use crate::{
|
||||||
use crate::{engines::kv::logic::body_to_json, storage::DatabaseDriver};
|
engines::kv::{
|
||||||
use axum::extract::State;
|
logic::body_to_json,
|
||||||
use axum::Extension;
|
structs::{KvSecret, SecretMeta},
|
||||||
use axum::{extract::Path, routing::*, Router};
|
},
|
||||||
|
storage::DatabaseDriver,
|
||||||
|
};
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
routing::*,
|
||||||
|
Router,
|
||||||
|
};
|
||||||
|
use log::{info, trace};
|
||||||
|
use sqlx::{error::UnexpectedNullError, Row};
|
||||||
|
|
||||||
pub fn kv_router(pool: DatabaseDriver) -> Router {
|
pub fn kv_router(pool: DatabaseDriver) -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
|
|
@ -40,15 +49,50 @@ async fn post_config() -> &'static str {
|
||||||
todo!("not implemented")
|
todo!("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_data() -> &'static str {
|
async fn get_data(State(pool): State<DatabaseDriver>, Path(path): Path<String>) -> &'static str {
|
||||||
todo!("not implemented")
|
// SQL Anfrage gegen Path
|
||||||
|
match sqlx::query("SELECT * FROM secret_versions WHERE secret_path = $1")
|
||||||
|
.bind(path)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(v) => {
|
||||||
|
let secret_data: String = v.get("secret_data");
|
||||||
|
let created_time: String = v.get("created_time");
|
||||||
|
let deletion_time: String = v.get("deletion_time");
|
||||||
|
let version_number: i64 = v.get("version_number");
|
||||||
|
let secret_path: String = v.get("secret_path");
|
||||||
|
info!(
|
||||||
|
"{:?}",
|
||||||
|
(
|
||||||
|
secret_data,
|
||||||
|
created_time,
|
||||||
|
deletion_time,
|
||||||
|
version_number,
|
||||||
|
secret_path
|
||||||
|
)
|
||||||
|
);
|
||||||
|
// Handle returned data
|
||||||
|
return "Success";
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
match e {
|
||||||
|
sqlx::Error::RowNotFound => trace!("{:?}", e),
|
||||||
|
_ => panic!("{:?}", e),
|
||||||
|
}
|
||||||
|
info!("{:?}", e);
|
||||||
|
return "Error";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_data(
|
async fn post_data(
|
||||||
Path(kv_path): Path<String>,
|
State(pool): State<DatabaseDriver>,
|
||||||
Extension(mount_path): Extension<String>,
|
Path(path): Path<String>,
|
||||||
body: String,
|
body: String,
|
||||||
) -> &'static str {
|
) -> &'static str {
|
||||||
|
// Insert Metadata first -> Else: Error because of foreign key constraint
|
||||||
|
|
||||||
let mut body_json = body_to_json(body);
|
let mut body_json = body_to_json(body);
|
||||||
|
|
||||||
let secret: KvSecret = KvSecret {
|
let secret: KvSecret = KvSecret {
|
||||||
|
|
@ -57,12 +101,35 @@ async fn post_data(
|
||||||
};
|
};
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Secret: {}, Content: {}, Version: {}, path: {}",
|
"Secret: {}, Content: {}, Version: {}, path: {}",
|
||||||
kv_path,
|
path,
|
||||||
secret.data,
|
secret.data,
|
||||||
secret.version.unwrap_or(0),
|
secret.version.unwrap_or(0),
|
||||||
mount_path,
|
path
|
||||||
);
|
);
|
||||||
"RoutingTest foo successful"
|
|
||||||
|
let created_time = "05-03-2024 12:00:00";
|
||||||
|
let deletion_time = "05-03-2024 12:00:00";
|
||||||
|
let version = "0";
|
||||||
|
match sqlx::query!(
|
||||||
|
"INSERT INTO secret_versions VALUES ($1, $2, $3, $4, $5)",
|
||||||
|
secret.data,
|
||||||
|
created_time,
|
||||||
|
deletion_time,
|
||||||
|
version,
|
||||||
|
version
|
||||||
|
)
|
||||||
|
.execute(&pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(v) => {
|
||||||
|
trace!("{:?}", v);
|
||||||
|
"Success"
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
trace!("{:?}", e);
|
||||||
|
"Error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// todo!("not implemented")
|
// todo!("not implemented")
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +153,13 @@ async fn get_meta() -> &'static str {
|
||||||
todo!("not implemented")
|
todo!("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_meta() -> &'static str {
|
async fn post_meta(
|
||||||
|
State(pool): State<DatabaseDriver>,
|
||||||
|
Path((mount_path, kv_path)): Path<(String, String)>,
|
||||||
|
body: String,
|
||||||
|
) -> &'static str {
|
||||||
|
let mut body_json = body_to_json(body);
|
||||||
|
let meta_data: SecretMeta = Default::default();
|
||||||
todo!("not implemented")
|
todo!("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue