2024-05-11 23:23:00 +02:00
|
|
|
use hyperlog_protos::hyperlog::{
|
|
|
|
graph_server::{Graph, GraphServer},
|
|
|
|
*,
|
|
|
|
};
|
2024-05-12 21:07:21 +02:00
|
|
|
use std::{collections::HashMap, net::SocketAddr};
|
2024-05-11 23:23:00 +02:00
|
|
|
use tonic::{transport, Response};
|
|
|
|
|
|
|
|
use crate::{
|
2024-05-13 22:57:20 +02:00
|
|
|
commands::{Command, Commander, CommanderExt},
|
2024-05-11 23:23:00 +02:00
|
|
|
querier::{Querier, QuerierExt},
|
|
|
|
state::SharedState,
|
|
|
|
};
|
|
|
|
|
2024-05-12 14:35:35 +02:00
|
|
|
#[allow(dead_code)]
|
2024-05-11 23:23:00 +02:00
|
|
|
pub struct Server {
|
|
|
|
querier: Querier,
|
2024-05-13 22:57:20 +02:00
|
|
|
commander: Commander,
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2024-05-13 22:57:20 +02:00
|
|
|
pub fn new(querier: Querier, commander: Commander) -> Self {
|
|
|
|
Self { querier, commander }
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2024-05-12 12:58:54 +02:00
|
|
|
item: Some(GraphItem {
|
2024-05-12 21:07:21 +02:00
|
|
|
path: "kjuulh".into(),
|
|
|
|
contents: Some(graph_item::Contents::User(UserGraphItem {
|
|
|
|
items: HashMap::from([(
|
|
|
|
"some".to_string(),
|
|
|
|
GraphItem {
|
|
|
|
path: "some".into(),
|
|
|
|
contents: Some(graph_item::Contents::Item(ItemGraphItem {
|
|
|
|
title: "some-title".into(),
|
|
|
|
description: "some-description".into(),
|
|
|
|
item_state: Some(item_graph_item::ItemState::NotDone(
|
|
|
|
ItemStateNotDone {},
|
|
|
|
)),
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
)]),
|
2024-05-12 12:58:54 +02:00
|
|
|
})),
|
|
|
|
}),
|
2024-05-11 23:23:00 +02:00
|
|
|
}))
|
|
|
|
}
|
2024-05-12 21:07:21 +02:00
|
|
|
|
|
|
|
async fn get_available_roots(
|
|
|
|
&self,
|
|
|
|
request: tonic::Request<GetAvailableRootsRequest>,
|
|
|
|
) -> std::result::Result<tonic::Response<GetAvailableRootsResponse>, tonic::Status> {
|
|
|
|
let req = request.into_inner();
|
|
|
|
tracing::trace!("get available roots: req({:?})", req);
|
|
|
|
|
|
|
|
Ok(Response::new(GetAvailableRootsResponse {
|
|
|
|
roots: vec!["kjuulh".into()],
|
|
|
|
}))
|
|
|
|
}
|
2024-05-12 22:24:37 +02:00
|
|
|
|
|
|
|
async fn create_section(
|
|
|
|
&self,
|
|
|
|
request: tonic::Request<CreateSectionRequest>,
|
|
|
|
) -> std::result::Result<tonic::Response<CreateSectionResponse>, tonic::Status> {
|
|
|
|
let req = request.into_inner();
|
|
|
|
tracing::trace!("create section: req({:?})", req);
|
|
|
|
|
2024-05-13 22:57:20 +02:00
|
|
|
if req.root.is_empty() {
|
|
|
|
return Err(tonic::Status::new(
|
|
|
|
tonic::Code::InvalidArgument,
|
|
|
|
"root cannot be empty".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.path.is_empty() {
|
|
|
|
return Err(tonic::Status::new(
|
|
|
|
tonic::Code::InvalidArgument,
|
|
|
|
"path cannot be empty".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if req
|
|
|
|
.path
|
|
|
|
.iter()
|
|
|
|
.filter(|item| item.is_empty())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.first()
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
return Err(tonic::Status::new(
|
|
|
|
tonic::Code::InvalidArgument,
|
|
|
|
"path cannot contain empty paths".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if req
|
|
|
|
.path
|
|
|
|
.iter()
|
|
|
|
.filter(|item| item.contains("."))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.first()
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
return Err(tonic::Status::new(
|
|
|
|
tonic::Code::InvalidArgument,
|
|
|
|
"path cannot contain `.`".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.commander
|
|
|
|
.execute(Command::CreateSection {
|
|
|
|
root: req.root,
|
|
|
|
path: req.path,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.map_err(to_tonic_err)?;
|
|
|
|
|
2024-05-12 22:24:37 +02:00
|
|
|
Ok(Response::new(CreateSectionResponse {}))
|
|
|
|
}
|
2024-05-13 22:57:20 +02:00
|
|
|
|
|
|
|
async fn create_root(
|
|
|
|
&self,
|
|
|
|
request: tonic::Request<CreateRootRequest>,
|
|
|
|
) -> std::result::Result<tonic::Response<CreateRootResponse>, tonic::Status> {
|
|
|
|
let req = request.into_inner();
|
|
|
|
tracing::trace!("create root: req({:?})", req);
|
|
|
|
|
|
|
|
if req.root.is_empty() {
|
|
|
|
return Err(tonic::Status::new(
|
|
|
|
tonic::Code::InvalidArgument,
|
|
|
|
"root cannot be empty".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.commander
|
|
|
|
.execute(Command::CreateRoot { root: req.root })
|
|
|
|
.await
|
|
|
|
.map_err(to_tonic_err)?;
|
|
|
|
|
|
|
|
Ok(Response::new(CreateRootResponse {}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: create more defined protobuf categories for errors
|
|
|
|
fn to_tonic_err(err: anyhow::Error) -> tonic::Status {
|
|
|
|
tonic::Status::new(tonic::Code::Unknown, err.to_string())
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ServerExt {
|
|
|
|
fn grpc_server(&self) -> Server;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerExt for SharedState {
|
|
|
|
fn grpc_server(&self) -> Server {
|
2024-05-13 22:57:20 +02:00
|
|
|
Server::new(self.querier(), self.commander())
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|