summaryrefslogtreecommitdiff
path: root/exes/webhook/src/lib.rs
blob: 933f38ea9a5e7d5d12271a5c735f232eaf29c843 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#![deny(
    clippy::all,
    clippy::correctness,
    clippy::suspicious,
    clippy::style,
    clippy::complexity,
    clippy::perf,
    clippy::pedantic,
    clippy::nursery,
    unsafe_code
)]

mod config;
mod handler;
use std::{future::Future, pin::Pin};

use crate::{
    config::Webhook,
    handler::{make_service::MakeSvc, WebhookService},
};
use async_nats::Client;
use hyper::Server;
use leash::{AnyhowResultFuture, Component};
use shared::config::Settings;
use tokio::sync::oneshot;
use tracing::info;
#[derive(Clone, Copy)]
pub struct WebhookServer {}

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

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

            let bind = settings.server.listening_adress;
            info!("Nats connected!");
            let nats = Into::<Pin<Box<dyn Future<Output = anyhow::Result<Client>> + Send>>>::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
                .with_graceful_shutdown(async {
                    stop.await.expect("should not fail");
                })
                .await?;

            Ok(())
        })
    }

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