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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use super::handle;
use crate::{errors, event_loop::Webhook};
use hyper::{
service::{make_service_fn, service_fn},
Server,
};
use std::{convert::Infallible, net::SocketAddr, sync::Arc};
use tokio::time::timeout;
use tracing::instrument;
#[must_use = "webhook server needs to be `start`ed to run the event loop"]
pub struct Http<'a> {
webhook: Webhook<'a>,
}
impl<'a> Http<'a> {
pub(crate) const fn new(webhook: Webhook<'a>) -> Self {
Self { webhook }
}
}
impl<'a> Http<'a> {
#[instrument(name = "http_webhook", skip(self))]
pub async fn start(self) -> Result<Infallible, errors::HttpWebhook> {
let Webhook {
event_loop,
bind_to,
port,
updates_url,
url,
ip_address,
certificate,
max_connections,
allowed_updates,
request_timeout,
drop_pending_updates,
} = self.webhook;
let set_webhook = event_loop
.bot
.set_webhook(
url,
ip_address,
certificate,
max_connections,
allowed_updates,
drop_pending_updates,
)
.call();
timeout(request_timeout, set_webhook).await??;
let set_commands = event_loop.set_commands_descriptions();
match timeout(request_timeout, set_commands).await {
Ok(Err(method)) => {
return Err(errors::HttpWebhook::SetMyCommands(method))
}
Err(timeout) => {
return Err(errors::HttpWebhook::SetMyCommandsTimeout(timeout))
}
Ok(_) => (),
};
let event_loop = Arc::new(event_loop);
let addr = SocketAddr::new(bind_to, port);
let updates_url = Arc::new(updates_url);
Server::bind(&addr)
.serve(make_service_fn(move |_| {
let event_loop = Arc::clone(&event_loop);
let updates_url = Arc::clone(&updates_url);
async move {
let service = service_fn(move |request| {
handle(
Arc::clone(&event_loop),
request,
Arc::clone(&updates_url),
)
});
Ok::<_, hyper::Error>(service)
}
}))
.await?;
unreachable!("[tbot] The webhook server unexpectedly returned.");
}
}