lohr/src/main.rs

61 lines
1.3 KiB
Rust
Raw Normal View History

2021-03-29 02:01:53 +02:00
#![feature(proc_macro_hygiene, decl_macro)]
2021-03-29 17:10:53 +02:00
use std::env;
use std::path::PathBuf;
2021-03-29 02:01:53 +02:00
use std::sync::{
mpsc::{channel, Receiver, Sender},
Mutex,
};
use std::thread;
2021-03-29 17:10:53 +02:00
use rocket::{http::Status, post, routes, State};
2021-03-29 02:01:53 +02:00
use rocket_contrib::json::Json;
use log::error;
mod gitea;
use gitea::GiteaWebHook;
mod job;
use job::Job;
struct JobSender(Mutex<Sender<Job>>);
#[post("/", data = "<payload>")]
fn gitea_webhook(payload: Json<GiteaWebHook>, sender: State<JobSender>) -> Status {
{
let sender = sender.0.lock().unwrap();
2021-03-29 17:10:53 +02:00
let repo = &payload.repository;
sender.send(Job::new(repo.clone())).unwrap();
2021-03-29 02:01:53 +02:00
}
Status::Ok
}
2021-03-29 17:10:53 +02:00
fn repo_updater(rx: Receiver<Job>, homedir: PathBuf) {
2021-03-29 02:01:53 +02:00
loop {
2021-03-29 17:10:53 +02:00
let mut job = rx.recv().unwrap();
2021-03-29 02:01:53 +02:00
2021-03-29 17:10:53 +02:00
if let Err(err) = job.run(&homedir) {
2021-03-29 02:01:53 +02:00
error!("couldn't process job: {}", err);
}
}
}
2021-03-27 10:42:49 +01:00
fn main() {
2021-03-29 02:01:53 +02:00
let (sender, receiver) = channel();
2021-03-29 17:10:53 +02:00
let homedir = env::var("LOHR_HOME").unwrap_or_else(|_| "./".to_string());
let homedir: PathBuf = homedir.into();
let homedir = homedir.canonicalize().expect("LOHR_HOME isn't valid!");
2021-03-29 02:01:53 +02:00
thread::spawn(move || {
2021-03-29 17:10:53 +02:00
repo_updater(receiver, homedir);
2021-03-29 02:01:53 +02:00
});
rocket::ignite()
.mount("/", routes![gitea_webhook])
.manage(JobSender(Mutex::new(sender)))
.launch();
2021-03-27 10:42:49 +01:00
}