summaryrefslogtreecommitdiff
path: root/exes/webhook/src/main.rs
blob: efd41474d438164314be9a6ad70119a8e6a55568 (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
45
46
47
mod config;
mod handler;
use std::{future::Future, pin::Pin};

use crate::{
    config::WebhookConfig,
    handler::{handler::WebhookService, make_service::MakeSvc},
};
use hyper::Server;
use leash::{ignite, AnyhowResultFuture, Component};
use shared::{config::Settings, log::info, nats_crate::Client};

#[derive(Clone, Copy)]
struct WebhookServer {}

impl Component for WebhookServer {
    type Config = WebhookConfig;
    const SERVICE_NAME: &'static str = "webhook";

    fn start(&self, settings: Settings<Self::Config>) -> AnyhowResultFuture<()> {
        Box::pin(async move {
            info!("Starting server on {}", settings.server.listening_adress);

            let bind = settings.server.listening_adress;
            let nats =
                Into::<Pin<Box<dyn Future<Output = anyhow::Result<Client>>>>>::into(settings.nats)
                    .await?;

            let make_service = MakeSvc::new(WebhookService {
                config: settings.config,
                nats: nats.clone(),
            });

            let server = Server::bind(&bind).serve(make_service);

            server.await?;

            Ok(())
        })
    }

    fn new() -> Self {
        Self {}
    }
}

ignite!(WebhookServer);