Moved both webservers into one, some other small fixes

This commit is contained in:
famfo 2022-01-15 17:09:46 +01:00
parent e3b2acf23f
commit e891c126ba
5 changed files with 31 additions and 58 deletions

View file

@ -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]

View file

@ -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"

View file

@ -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<String>,
}
impl Tx {
fn new(tx: Sender<String>) -> Self {
Tx { tx }
}
}
async fn handle_post(json: serde_json::Value, tx: Tx) -> Result<impl warp::Reply, warp::Rejection> {
) -> Result<impl warp::Reply, warp::Rejection> {
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<impl warp::Reply
.unwrap()
.trim();
let message = &json["commits"][0]["message"].as_str().unwrap().trim();
tx.tx
.send(format!("New commit on {}: {} - {}", repo, message, author))
tx.send(format!("New commit on {}: {} - {}", repo, message, author))
.await
.expect("Failed to send string to main thread");
}
@ -46,17 +33,3 @@ async fn handle_post(json: serde_json::Value, tx: Tx) -> Result<impl warp::Reply
Ok(warp::reply::with_status("Ok", warp::http::StatusCode::OK))
}
pub async fn run(tx: Sender<String>, 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(())
}

View file

@ -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<String>,
http_listen_db: Option<SocketAddr>,
http_listen_git: SocketAddr,
http_listen: Option<SocketAddr>,
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<String>,
mut git_recv: Receiver<String>,
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>) -> String {
if let Some(s) = r {
s
} else {
String::new()
}
}

View file

@ -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<String>,
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(())
}