summaryrefslogtreecommitdiffstats
path: root/src/service.rs
blob: b67ba510eb44db358b9b4b14af53d9c47926efd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use serde::Deserialize;

use crate::Status;

pub mod command;
pub mod http;
pub mod tcp;

pub trait IntoService {
    fn into_service(
        self,
        tx: tokio::sync::watch::Sender<Status>,
    ) -> impl std::future::Future<Output = ()>;
}

pub fn default_interval() -> std::time::Duration {
    std::time::Duration::from_secs(5)
}

#[derive(Debug, Clone, Deserialize)]
pub struct ServiceConfig {
    pub name: String,
    #[serde(flatten)]
    pub kind: ServiceKind,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
#[serde(tag = "kind")]
pub enum ServiceKind {
    Http(http::Http),
    Tcp(tcp::Tcp),
    Exec(command::Command),
}

impl IntoService for ServiceKind {
    async fn into_service(self, tx: tokio::sync::watch::Sender<Status>) {
        match self {
            ServiceKind::Http(h) => h.into_service(tx).await,
            ServiceKind::Tcp(t) => t.into_service(tx).await,
            ServiceKind::Exec(c) => c.into_service(tx).await,
        }
    }
}