From e891c126bac4665ca94db2169d57b406283b5996 Mon Sep 17 00:00:00 2001 From: famfo Date: Sat, 15 Jan 2022 17:09:46 +0100 Subject: [PATCH] Moved both webservers into one, some other small fixes --- Cargo.toml | 1 - sample_uberbot.toml | 4 +--- src/bots/git.rs | 39 ++++++--------------------------------- src/main.rs | 27 +++++++++------------------ src/web_service.rs | 18 +++++++++++++++--- 5 files changed, 31 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cba371a..c6faf0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,6 @@ async-circe = { git = "https://git.karx.xyz/circe/async-circe" } lazy_static = "1.4" sedregex = "0.2" rusqlite = { version = "0.26", features = ["bundled"] } -hyper = { version = "0.14", features = ["server"] } warp = "0.3" [features] diff --git a/sample_uberbot.toml b/sample_uberbot.toml index 7de780d..5784662 100644 --- a/sample_uberbot.toml +++ b/sample_uberbot.toml @@ -13,9 +13,7 @@ spotify_client_secret = "" prefix = "!" # Web service config -http_listen_db = "127.0.0.1:8080" +http_listen = "127.0.0.1:8080" # Git webhook config -http_listen_git = "127.0.0.1:51001" git_channel = "#main" - diff --git a/src/bots/git.rs b/src/bots/git.rs index 260875c..5d2df41 100644 --- a/src/bots/git.rs +++ b/src/bots/git.rs @@ -1,33 +1,21 @@ use serde_json::Value::Null; -use std::net::SocketAddr; use tokio::sync::mpsc::Sender; -use warp::Filter; -#[derive(Clone)] -struct Tx { +pub async fn handle_post( + json: serde_json::Value, tx: Sender, -} - -impl Tx { - fn new(tx: Sender) -> Self { - Tx { tx } - } -} - -async fn handle_post(json: serde_json::Value, tx: Tx) -> Result { +) -> Result { if json["commits"] != Null { let commits = json["commits"].as_array().unwrap(); let repo = &json["repository"]["full_name"].as_str().unwrap().trim(); if commits.len() != 1 { - tx.tx - .send(format!("{} new commits on {}:", commits.len(), repo)) + tx.send(format!("{} new commits on {}:", commits.len(), repo)) .await .expect("Failed to send string to main thread"); for commit in commits { let author = &commit["author"]["name"].as_str().unwrap().trim(); let message = &commit["message"].as_str().unwrap().trim(); - tx.tx - .send(format!("{} - {}", author, message)) + tx.send(format!("{} - {}", author, message)) .await .expect("Failed to send string to main thread"); } @@ -37,8 +25,7 @@ async fn handle_post(json: serde_json::Value, tx: Tx) -> Result Result, listen: SocketAddr) -> Result<(), tokio::io::Error> { - let tx = Tx::new(tx); - let tx_filter = warp::any().map(move || tx.clone()); - - let filter = warp::post() - .and(warp::body::json()) - .and(tx_filter) - .and_then(handle_post); - - warp::serve(filter).run(listen).await; - - Ok(()) -} diff --git a/src/main.rs b/src/main.rs index 3e72f42..70d5b79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use std::net::SocketAddr; use std::thread; use std::{collections::HashMap, env}; use tokio::select; -use tokio::sync::mpsc::{channel, Receiver}; +use tokio::sync::mpsc::{channel, Receiver, Sender}; use tracing_subscriber::EnvFilter; // this will be displayed when the help command is used @@ -70,8 +70,7 @@ struct ClientConf { spotify_client_secret: String, prefix: String, db_path: Option, - http_listen_db: Option, - http_listen_git: SocketAddr, + http_listen: Option, git_channel: String, } @@ -101,12 +100,9 @@ async fn main() -> anyhow::Result<()> { ); let http_listen = client_config - .http_listen_db + .http_listen .unwrap_or_else(|| SocketAddr::from(([127, 0, 0, 1], 5000))); - let (git_tx, git_recv) = channel(512); - tokio::spawn(bots::git::run(git_tx, client_config.http_listen_git)); - let config = Config::runtime_config( client_config.channels, client_config.host, @@ -129,7 +125,9 @@ async fn main() -> anyhow::Result<()> { git_channel: client_config.git_channel, }; - if let Err(e) = executor(state, git_recv, http_listen).await { + let (git_tx, git_recv) = channel(512); + + if let Err(e) = executor(state, git_tx, git_recv, http_listen).await { tracing::error!("Error in message loop: {}", e); } @@ -143,15 +141,16 @@ async fn main() -> anyhow::Result<()> { async fn executor( mut state: AppState, + git_tx: Sender, mut git_recv: Receiver, http_listen: SocketAddr, ) -> anyhow::Result<()> { let web_db = state.db.clone(); let git_channel = state.git_channel.clone(); select! { - r = web_service::run(web_db, http_listen) => r?, + r = web_service::run(web_db, git_tx, http_listen) => r?, r = message_loop(&mut state) => r?, - r = git_recv.recv() => state.client.privmsg(&git_channel, &get_str(r)).await?, + r = git_recv.recv() => state.client.privmsg(&git_channel, &r.unwrap_or_default()).await?, _ = terminate_signal() => { tracing::info!("Sending QUIT message"); state.client.quit(Some("überbot shutting down")).await?; @@ -296,11 +295,3 @@ async fn handle_privmsg( } Ok(()) } - -fn get_str(r: Option) -> String { - if let Some(s) = r { - s - } else { - String::new() - } -} diff --git a/src/web_service.rs b/src/web_service.rs index e68663f..7d100a9 100644 --- a/src/web_service.rs +++ b/src/web_service.rs @@ -1,13 +1,25 @@ use crate::ExecutorConnection; use std::net::SocketAddr; +use tokio::sync::mpsc::Sender; use warp::Filter; -pub async fn run(db: ExecutorConnection, listen: SocketAddr) -> anyhow::Result<()> { +pub async fn run( + db: ExecutorConnection, + tx: Sender, + listen: SocketAddr, +) -> anyhow::Result<()> { let db_filter = warp::any().map(move || db.clone()); - let filter = warp::any().and(db_filter).and_then(handle); + let db_filter = warp::any().and(db_filter).and_then(handle); + let tx_filter = warp::any().map(move || tx.clone()); + let tx_filter = warp::path("webhook") + .and(warp::post()) + .and(warp::body::json()) + .and(tx_filter) + .and_then(crate::bots::git::handle_post); + + let filter = db_filter.or(tx_filter); warp::serve(filter).run(listen).await; - Ok(()) }