2024-01-30 21:34:19 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2024-01-29 18:18:29 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
mod exec;
|
|
|
|
mod terminal;
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
enum ServiceError {
|
|
|
|
#[error("received an invalid body format for a valid message")]
|
|
|
|
BodyFormatError,
|
|
|
|
}
|
|
|
|
|
2024-01-30 21:34:19 +00:00
|
|
|
struct Service {
|
|
|
|
terminals: HashMap<String, ()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Service {
|
|
|
|
fn serve(&mut self, message: crate::messaging::Message) {
|
|
|
|
match message.subject() {
|
|
|
|
crate::messaging::Subject::OpenTerminal => {}
|
|
|
|
crate::messaging::Subject::Exec => todo!(),
|
|
|
|
crate::messaging::Subject::Health => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 23:51:56 +00:00
|
|
|
async fn route_message(message: crate::messaging::Message) -> Result<(), ServiceError> {
|
|
|
|
match message.subject() {
|
|
|
|
crate::messaging::Subject::Health => {}
|
|
|
|
crate::messaging::Subject::Exec => {
|
|
|
|
let ctx = Ctx::with_body(message.body())?;
|
|
|
|
let _ = self::exec::exec(ctx).await;
|
|
|
|
}
|
|
|
|
crate::messaging::Subject::OpenTerminal => {
|
|
|
|
let ctx = Ctx::with_body(message.body())?;
|
|
|
|
let _ = self::terminal::open_terminal(ctx).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:18:29 +00:00
|
|
|
struct Ctx<T> {
|
|
|
|
body: T,
|
2024-01-30 21:34:19 +00:00
|
|
|
terminals: HashMap<String, (crate::pty::Pty, tokio::process::Child)>,
|
2024-01-29 18:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Ctx<T>
|
|
|
|
where
|
|
|
|
T: TryFrom<Bytes>,
|
|
|
|
{
|
2024-01-29 23:51:56 +00:00
|
|
|
fn with_body(body: Bytes) -> Result<Self, ServiceError> {
|
2024-01-30 21:34:19 +00:00
|
|
|
let body = body
|
|
|
|
.try_into()
|
|
|
|
.map_err(|_err| ServiceError::BodyFormatError)?;
|
|
|
|
|
2024-01-29 18:18:29 +00:00
|
|
|
Ok(Self {
|
2024-01-30 21:34:19 +00:00
|
|
|
body,
|
|
|
|
terminals: HashMap::default(),
|
2024-01-29 18:18:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn init_services(client: crate::messaging::Client) -> anyhow::Result<()> {
|
2024-01-30 15:40:46 +00:00
|
|
|
let mut recv = client.messages_channel().await?;
|
2024-01-29 18:18:29 +00:00
|
|
|
|
|
|
|
tokio::spawn(async move {
|
2024-01-30 15:40:46 +00:00
|
|
|
while let Some(msg) = recv.recv().await {
|
2024-01-29 18:18:29 +00:00
|
|
|
// TODO: How do i handle this error?
|
2024-01-30 15:40:46 +00:00
|
|
|
if let Err(err) = route_message(msg).await {
|
2024-01-29 18:18:29 +00:00
|
|
|
tracing::warn!("{}", err);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|