-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.rs
44 lines (38 loc) · 1.46 KB
/
dispatch.rs
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 std::convert::Infallible;
use teloxide::prelude::Requester;
use teloxide::update_listeners::UpdateListener;
use teloxide::update_listeners::webhooks::{axum_to_router, Options};
use crate::bot::core::bot_config::TELEGRAM_BOT_ENDPOINT_HEALTHCHECK;
use crate::bot::core::healthcheck::endpoint::healthcheck_endpoint;
pub async fn axum_update_listener<R>(
bot: R,
options: Options,
) -> Result<impl UpdateListener<Err = Infallible>, R::Err>
where
R: Requester + Send + 'static,
<R as Requester>::DeleteWebhook: Send,
{
// loosely derived from: teloxide: src/update_listeners/webhooks/axum.rs
let Options { address, .. } = options;
let (mut update_listener, stop_flag, app) = axum_to_router(bot, options).await?;
let my_router = axum::Router::new()
.route(TELEGRAM_BOT_ENDPOINT_HEALTHCHECK, axum::routing::get(healthcheck_endpoint))
.fallback_service(app);
let stop_token = update_listener.stop_token();
tokio::spawn(async move {
let tcp_listener = tokio::net::TcpListener::bind(address)
.await
.inspect_err(|_err| {
stop_token.stop();
})
.expect("Couldn't bind to the address");
axum::serve(tcp_listener, my_router)
.with_graceful_shutdown(stop_flag)
.await
.inspect_err(|_err| {
stop_token.stop();
})
.expect("Axum server error");
});
Ok(update_listener)
}