Update dep: hyper
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
0eb47eac7b
commit
1ffa463fd6
3 changed files with 51 additions and 39 deletions
54
src/main.rs
54
src/main.rs
|
|
@ -26,7 +26,6 @@ use crate::error::Error;
|
|||
use crate::web::webhooks;
|
||||
use crate::webhook::WebHook;
|
||||
|
||||
use std::convert::Infallible;
|
||||
use std::fs::File;
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read};
|
||||
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||
|
|
@ -34,13 +33,11 @@ use std::path::{Path, PathBuf};
|
|||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use clap::{command, value_parser, Arg};
|
||||
use hyper::{
|
||||
service::{make_service_fn, service_fn},
|
||||
Server,
|
||||
};
|
||||
use hyper::{server::conn::http1, service::service_fn};
|
||||
use hyper_util::rt::tokio::{TokioIo, TokioTimer};
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::{net::TcpListener, sync::mpsc};
|
||||
use xmpp::BareJid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
|
@ -135,25 +132,6 @@ async fn main() -> Result<!, Error> {
|
|||
|
||||
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
|
||||
|
||||
if let Some(token) = config.webhook_token {
|
||||
let value_tx = Arc::new(Mutex::new(value_tx));
|
||||
let make_svc = make_service_fn(move |_conn| {
|
||||
let value_tx = value_tx.clone();
|
||||
let token = token.clone();
|
||||
async move {
|
||||
Ok::<_, Infallible>(service_fn(move |req| {
|
||||
let value_tx = value_tx.clone();
|
||||
let token = token.clone();
|
||||
webhooks(req, token, value_tx)
|
||||
}))
|
||||
}
|
||||
});
|
||||
let server = Server::bind(&config.addr).serve(make_svc);
|
||||
println!("Listening on http://{}", &config.addr);
|
||||
|
||||
let _join = tokio::spawn(server);
|
||||
}
|
||||
|
||||
let mut client = XmppClient::new(
|
||||
config.jid,
|
||||
config.password.as_str(),
|
||||
|
|
@ -161,9 +139,35 @@ async fn main() -> Result<!, Error> {
|
|||
config.nickname,
|
||||
);
|
||||
|
||||
let tcp_bind = TcpListener::bind(config.addr).await?;
|
||||
let token: Option<&'static String> =
|
||||
unsafe { core::mem::transmute(config.webhook_token.as_ref()) };
|
||||
let value_tx = Arc::new(Mutex::new(value_tx));
|
||||
|
||||
loop {
|
||||
let value_tx = value_tx.clone();
|
||||
|
||||
tokio::select! {
|
||||
_ = client.next() => (),
|
||||
accept = tcp_bind.accept() => {
|
||||
if let Ok((tcp, _)) = accept {
|
||||
let io = TokioIo::new(tcp);
|
||||
tokio::task::spawn(async move {
|
||||
if let Err(err) = http1::Builder::new()
|
||||
.timer(TokioTimer::new())
|
||||
.serve_connection(io, service_fn(|request| {
|
||||
let value_tx = value_tx.clone();
|
||||
async move {
|
||||
webhooks(request, token, value_tx).await
|
||||
}
|
||||
}))
|
||||
.await
|
||||
{
|
||||
println!("Error serving connection: {:?}", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
wh = value_rx.recv() => {
|
||||
if let Some(wh) = wh {
|
||||
client.webhook(wh).await
|
||||
|
|
|
|||
Loading…
Reference in a new issue