feat: move core to tui and begin grpc work
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-05-11 23:23:00 +02:00
parent 86cba91b16
commit 4a0fcd1bbb
25 changed files with 309 additions and 112 deletions

View File

@@ -0,0 +1,97 @@
use hyperlog_core::log::{GraphItem, ItemState};
pub enum Command {
CreateRoot {
root: String,
},
CreateSection {
root: String,
path: Vec<String>,
},
CreateItem {
root: String,
path: Vec<String>,
title: String,
description: String,
state: ItemState,
},
UpdateItem {
root: String,
path: Vec<String>,
title: String,
description: String,
state: ItemState,
},
ToggleItem {
root: String,
path: Vec<String>,
},
Move {
root: String,
src: Vec<String>,
dest: Vec<String>,
},
}
pub struct Commander {}
impl Commander {
pub fn execute(&self, cmd: Command) -> anyhow::Result<()> {
match cmd {
Command::CreateRoot { root } => todo!(),
Command::CreateSection { root, path } => todo!(),
Command::CreateItem {
root,
path,
title,
description,
state,
} => todo!(),
Command::UpdateItem {
root,
path,
title,
description,
state,
} => todo!(),
Command::ToggleItem { root, path } => todo!(),
Command::Move { root, src, dest } => todo!(),
}
Ok(())
}
pub async fn create_root(&self, root: &str) -> anyhow::Result<()> {
todo!()
}
pub async fn create(&self, root: &str, path: &[&str], item: GraphItem) -> anyhow::Result<()> {
todo!()
}
pub async fn get(&self, root: &str, path: &[&str]) -> Option<GraphItem> {
todo!()
}
pub async fn section_move(
&self,
root: &str,
src_path: &[&str],
dest_path: &[&str],
) -> anyhow::Result<()> {
todo!()
}
pub async fn delete(&self, root: &str, path: &[&str]) -> anyhow::Result<()> {
todo!()
}
pub async fn update_item(
&self,
root: &str,
path: &[&str],
item: &GraphItem,
) -> anyhow::Result<()> {
todo!()
}
}

View File

@@ -0,0 +1,82 @@
use std::{collections::HashMap, net::SocketAddr};
use hyperlog_protos::hyperlog::{
graph_server::{Graph, GraphServer},
*,
};
use tonic::{transport, Response};
use crate::{
querier::{Querier, QuerierExt},
state::SharedState,
};
pub struct Server {
querier: Querier,
}
impl Server {
pub fn new(querier: Querier) -> Self {
Self { querier }
}
}
#[tonic::async_trait]
impl Graph for Server {
async fn get(
&self,
request: tonic::Request<GetRequest>,
) -> std::result::Result<tonic::Response<GetReply>, tonic::Status> {
let msg = request.get_ref();
tracing::trace!("get: req({:?})", msg);
Ok(Response::new(GetReply {
items: vec![
GraphItem {
path: "some.path".into(),
contents: Some(graph_item::Contents::Item(ItemGraphItem {
title: "some-title".into(),
description: "some-description".into(),
state: ItemState::NotDone as i32,
})),
},
GraphItem {
path: "some.path.section".into(),
contents: Some(graph_item::Contents::Section(SectionGraphItem {
items: HashMap::new(),
})),
},
GraphItem {
path: "some.path".into(),
contents: Some(graph_item::Contents::User(UserGraphItem {
items: HashMap::new(),
})),
},
],
}))
}
}
pub trait ServerExt {
fn grpc_server(&self) -> Server;
}
impl ServerExt for SharedState {
fn grpc_server(&self) -> Server {
Server::new(self.querier())
}
}
pub async fn serve(state: &SharedState, host: SocketAddr) -> anyhow::Result<()> {
tracing::info!("listening on {}", host);
let graph_server = state.grpc_server();
transport::Server::builder()
.add_service(GraphServer::new(graph_server))
.serve(host)
.await?;
Ok(())
}

View File

@@ -2,49 +2,13 @@ use std::{net::SocketAddr, sync::Arc};
use crate::state::{SharedState, State};
mod external_grpc;
mod external_http;
mod internal_http;
mod external_grpc {
use std::net::SocketAddr;
use hyperlog_protos::hyperlog::{
graph_server::{Graph, GraphServer},
HelloReply, HelloRequest,
};
use tonic::{transport, Response};
mod commands;
mod querier;
use crate::state::SharedState;
#[derive(Default)]
struct Server {}
#[tonic::async_trait]
impl Graph for Server {
async fn say_hello(
&self,
request: tonic::Request<HelloRequest>,
) -> std::result::Result<tonic::Response<HelloReply>, tonic::Status> {
tracing::info!("received hello request");
Ok(Response::new(HelloReply {
message: "hello".into(),
}))
}
}
pub async fn serve(state: &SharedState, host: SocketAddr) -> anyhow::Result<()> {
tracing::info!("listening on {}", host);
let graph_server = Server::default();
transport::Server::builder()
.add_service(GraphServer::new(graph_server))
.serve(host)
.await?;
Ok(())
}
}
mod state;
#[derive(Clone)]

View File

@@ -0,0 +1,33 @@
use hyperlog_core::log::GraphItem;
use crate::state::SharedState;
pub struct Querier {}
impl Querier {
pub fn new() -> Self {
Self {}
}
pub fn get_available_roots(&self) -> Option<Vec<String>> {
todo!()
}
pub fn get(
&self,
root: &str,
path: impl IntoIterator<Item = impl Into<String>>,
) -> Option<GraphItem> {
todo!()
}
}
pub trait QuerierExt {
fn querier(&self) -> Querier;
}
impl QuerierExt for SharedState {
fn querier(&self) -> Querier {
Querier::new()
}
}