Refactor the web service
This commit is contained in:
parent
c0c4aefa27
commit
5577475b6c
|
@ -1,15 +1,20 @@
|
||||||
use crate::ExecutorConnection;
|
use crate::ExecutorConnection;
|
||||||
|
use irc::client::Client;
|
||||||
|
use reqwest::StatusCode;
|
||||||
use serde_json::Value::Null;
|
use serde_json::Value::Null;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tokio::sync::mpsc::Sender;
|
use std::sync::Arc;
|
||||||
use warp::{Filter, Reply, reply};
|
use tokio::sync::broadcast::Receiver;
|
||||||
|
use warp::{reply, Filter, Reply};
|
||||||
|
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
db: ExecutorConnection,
|
db: ExecutorConnection,
|
||||||
webhook_tx: Sender<String>,
|
wh_irc: Arc<Client>,
|
||||||
|
wh_channel: String,
|
||||||
listen: SocketAddr,
|
listen: SocketAddr,
|
||||||
) -> anyhow::Result<()> {
|
mut cancel: Receiver<()>
|
||||||
let quote_get = warp::get()
|
) {
|
||||||
|
let quote_get = warp::path("quotes")
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.and(warp::any().map(move || db.clone()))
|
.and(warp::any().map(move || db.clone()))
|
||||||
.then(handle_get_quote);
|
.then(handle_get_quote);
|
||||||
|
@ -17,35 +22,48 @@ pub async fn run(
|
||||||
let webhook_post = warp::path("webhook")
|
let webhook_post = warp::path("webhook")
|
||||||
.and(warp::post())
|
.and(warp::post())
|
||||||
.and(warp::body::json())
|
.and(warp::body::json())
|
||||||
.and(warp::any().map(move || webhook_tx.clone()))
|
.and(warp::any().map(move || wh_irc.clone()))
|
||||||
|
.and(warp::any().map(move || wh_channel.clone()))
|
||||||
.then(handle_webhook);
|
.then(handle_webhook);
|
||||||
|
|
||||||
let filter = quote_get.or(webhook_post);
|
let filter = quote_get.or(webhook_post);
|
||||||
warp::serve(filter).run(listen).await;
|
warp::serve(filter).bind_with_graceful_shutdown(listen, async move {
|
||||||
Ok(())
|
let _ = cancel.recv().await;
|
||||||
|
}).1.await;
|
||||||
|
tracing::info!("Web service finished")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_get_quote(_: ExecutorConnection) -> impl Reply {
|
async fn handle_get_quote(_: ExecutorConnection) -> impl Reply {
|
||||||
reply::html(include_str!("res/quote_tmpl.html"))
|
reply::html(include_str!("res/quote_tmpl.html"))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_webhook(
|
async fn handle_webhook(json: serde_json::Value, irc: Arc<Client>, channel: String) -> impl Reply {
|
||||||
json: serde_json::Value,
|
|
||||||
tx: Sender<String>,
|
|
||||||
) -> impl Reply {
|
|
||||||
if json["commits"] != Null {
|
if json["commits"] != Null {
|
||||||
let commits = json["commits"].as_array().unwrap();
|
let commits = json["commits"].as_array().unwrap();
|
||||||
let repo = &json["repository"]["full_name"].as_str().unwrap().trim();
|
let repo = &json["repository"]["full_name"].as_str().unwrap().trim();
|
||||||
if commits.len() != 1 {
|
if commits.len() != 1 {
|
||||||
tx.send(format!("{} new commits on {}:", commits.len(), repo))
|
if let Err(e) = irc.send_privmsg(
|
||||||
.await
|
channel.clone(),
|
||||||
.expect("Failed to send string to main thread");
|
format!("{} new commits on {}:", commits.len(), repo),
|
||||||
|
) {
|
||||||
|
return reply::with_status(
|
||||||
|
format!("An error has occurred: {}", e),
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
for commit in commits {
|
for commit in commits {
|
||||||
let author = &commit["author"]["name"].as_str().unwrap().trim();
|
let author = &commit["author"]["name"].as_str().unwrap().trim();
|
||||||
let message = &commit["message"].as_str().unwrap().trim();
|
let message = &commit["message"].as_str().unwrap().trim();
|
||||||
tx.send(format!("{} - {}", author, message))
|
if let Err(e) =
|
||||||
.await
|
irc.send_privmsg(channel.clone(), format!("{} - {}", author, message))
|
||||||
.expect("Failed to send string to main thread");
|
{
|
||||||
|
return reply::with_status(
|
||||||
|
format!("An error has occurred: {}", e),
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let author = &json["commits"][0]["author"]["name"]
|
let author = &json["commits"][0]["author"]["name"]
|
||||||
|
@ -53,10 +71,17 @@ async fn handle_webhook(
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.trim();
|
.trim();
|
||||||
let message = &json["commits"][0]["message"].as_str().unwrap().trim();
|
let message = &json["commits"][0]["message"].as_str().unwrap().trim();
|
||||||
tx.send(format!("New commit on {}: {} - {}", repo, message, author))
|
if let Err(e) = irc.send_privmsg(
|
||||||
.await
|
channel,
|
||||||
.expect("Failed to send string to main thread");
|
format!("New commit on {}: {} - {}", repo, message, author),
|
||||||
|
) {
|
||||||
|
return reply::with_status(
|
||||||
|
format!("An error has occurred: {}", e),
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
warp::reply::with_status("Ok", warp::http::StatusCode::OK)
|
StatusCode::CREATED.into_response()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue