Skip to content

Commit

Permalink
polish: polish up the api
Browse files Browse the repository at this point in the history
  • Loading branch information
geofmureithi committed Dec 17, 2023
1 parent 94c0920 commit 9893804
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 178 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Kasuku.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[internals]
cache-path = "./cache"
cache-path = "/tmp/kasuku-cache"

[vaults.planning]
label = "Planning"
plugins = ["tasks"]
mount = "/home/geoff/Documents/kasuku"
file_types = ["md"]
Expand Down
2 changes: 2 additions & 0 deletions crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ xtra = { git = "https://github.com/Restioson/xtra", features = [
"macros",
] }
futures = "0.3"
pulldown-cmark-to-cmark = "11.0.2"
pulldown-cmark = { version = "0.9.3" }
51 changes: 25 additions & 26 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,30 @@ impl KasukuRuntime {
res.unwrap()
})
.await;
let db = Glue::new(ks);
let mut db = Glue::new(ks);
db.execute(
"
DROP TABLE subscriptions;
DROP TABLE vaults;
DROP TABLE entries;
CREATE TABLE IF NOT EXISTS subscriptions (
plugin TEXT NOT NULL,
event TEXT NOT NULL,
event_type TEXT NOT NULL,
data TEXT
);
CREATE TABLE IF NOT EXISTS vaults (
name TEXT NOT NULL PRIMARY KEY,
mount TEXT NOT NULL,
);
CREATE TABLE IF NOT EXISTS entries (
path TEXT NOT NULL PRIMARY KEY,
vault TEXT NOT NULL,
last_modified INTEGER,
meta TEXT
)",
)
.unwrap();

let db = Arc::new(Mutex::new(db));
let runtime = Runtime::new().unwrap();
Expand All @@ -76,23 +99,6 @@ impl KasukuRuntime {
.unwrap();
plugin.on_load(::types::Context::acquire()).await.unwrap();
}
// db.lock().unwrap().storage.save().await.unwrap();
db.lock()
.as_mut()
.unwrap()
.execute(
"CREATE TABLE IF NOT EXISTS vaults (
name TEXT NOT NULL PRIMARY KEY,
mount TEXT NOT NULL,
);
CREATE TABLE IF NOT EXISTS entries (
path TEXT NOT NULL PRIMARY KEY,
vault TEXT NOT NULL,
last_modified INTEGER,
meta TEXT
)",
)
.unwrap();

for (vault, vault_config) in config.vaults.clone() {
let mount = vault_config.mount.clone();
Expand All @@ -105,8 +111,7 @@ impl KasukuRuntime {
"INSERT INTO vaults(name, mount) VALUES ('{vault}', '{}') ON CONFLICT(name) DO
UPDATE SET mount = EXCLUDED.mount",
mount.to_str().unwrap()
))
.unwrap_or(vec![]);
));

// tokio::spawn(async move {
use futures::stream::StreamExt;
Expand All @@ -130,7 +135,6 @@ impl KasukuRuntime {
loop {
match entries.next().await {
Some(Ok(entry)) => {
println!("file: {}", entry.path().display());
let _res = db.lock().unwrap().execute(format!(
"INSERT INTO entries(path, vault) VALUES('{}', '{}')",
entry.path().display(),
Expand Down Expand Up @@ -177,11 +181,6 @@ pub async fn app<D: Send + Sync + Clone + 'static>(port: u16, data: D) {
"/",
get(graphiql).post_service(GraphQL::new(schema.clone())),
)
// .route(
// "/api/v1/:namespace/:plugin/*path",
// get(getter).post(do_action),
// )
// .route("/api/v1/:namespace/:plugin", get(getter))
.layer(CorsLayer::new().allow_origin(Any).allow_methods(vec![
Method::GET,
Method::POST,
Expand Down
59 changes: 55 additions & 4 deletions crates/backend/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ::types::MarkdownEvent;
use ::types::{Emit, Event};
use async_graphql::*;
use kasuku_database::prelude::Payload;
Expand All @@ -20,7 +21,7 @@ pub struct File {

#[ComplexObject]
impl File {
async fn render(&self, ctx: &Context<'_>, renderer: Option<String>) -> serde_json::Value {
async fn preview(&self, ctx: &Context<'_>) -> serde_json::Value {
let runtime: &KasukuRuntime = ctx.data().unwrap();
let plugin: ::types::PluginWrapper<BackendPlugin, _> =
runtime.get_plugin_by_name("tasks").unwrap();
Expand Down Expand Up @@ -49,10 +50,60 @@ impl QueryRoot {
path: String,
renderer: Option<String>,

Check warning on line 51 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `renderer`

Check warning on line 51 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `renderer`

Check warning on line 51 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `renderer`

Check warning on line 51 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `renderer`
) -> serde_json::Value {
File { path }
.render(ctx, renderer)
.await
let runtime: &KasukuRuntime = ctx.data().unwrap();
let res = runtime
.database
.lock()
.unwrap()
.execute("SELECT * FROM subscriptions WHERE event = 'MarkdownEvent';")
.unwrap();
let subscriptions = res.get(0).unwrap();
match subscriptions {
Payload::Select { rows, .. } => {
let filters: Vec<MarkdownEvent> = rows

Check warning on line 63 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `filters`

Check warning on line 63 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `filters`

Check warning on line 63 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `filters`

Check warning on line 63 in crates/backend/src/query.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `filters`
.iter()
.map(|row| match row.get(3).unwrap() {
kasuku_database::prelude::Value::Str(val) => {
serde_json::from_str(&val).unwrap()
}
_ => unreachable!(),
})
.collect();
let plugin: ::types::PluginWrapper<BackendPlugin, _> =
runtime.get_plugin_by_name("tasks").unwrap();
let mut md = ::types::File::read(&path).await.unwrap();
md = plugin
.process_file(::types::Context::acquire(), md)
.await
.unwrap();
let mut buf = String::new();
let md_events: Vec<pulldown_cmark::Event<'_>> =match &md {
::types::File::Markdown(inner) => inner.get_contents(),
_ => unreachable!(),
};
pulldown_cmark_to_cmark::cmark(md_events.iter(), &mut buf).unwrap();
println!("{buf}");

}
_ => unreachable!(),
};

let plugin: ::types::PluginWrapper<BackendPlugin, _> =
runtime.get_plugin_by_name("tasks").unwrap();
let res = plugin
.render(
::types::Context::acquire(),
Event {
path,
data: Emit {
data: vec![],
r#type: "Event".to_string(),
},
},
)
.await
.unwrap();
serde_json::from_slice(&res.0).unwrap()
}
async fn vaults(&self, ctx: &Context<'_>) -> Vec<Vault> {
let runtime: &KasukuRuntime = ctx.data().unwrap();
Expand Down
Loading

0 comments on commit 9893804

Please sign in to comment.