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;
|
2021-03-29 20:52:04 +02:00
|
|
|
use std::fs::File;
|
2021-03-29 17:10:53 +02:00
|
|
|
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;
|
|
|
|
|
2021-03-29 20:52:04 +02:00
|
|
|
mod settings;
|
|
|
|
use settings::GlobalSettings;
|
|
|
|
|
2021-03-29 02:01:53 +02:00
|
|
|
struct JobSender(Mutex<Sender<Job>>);
|
|
|
|
|
|
|
|
#[post("/", data = "<payload>")]
|
|
|
|
fn gitea_webhook(payload: Json<GiteaWebHook>, sender: State<JobSender>) -> Status {
|
2021-03-29 18:12:06 +02:00
|
|
|
// TODO: validate Gitea signature
|
|
|
|
|
2021-03-29 02:01:53 +02:00
|
|
|
{
|
|
|
|
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 20:52:04 +02:00
|
|
|
fn repo_updater(rx: Receiver<Job>, homedir: PathBuf, config: GlobalSettings) {
|
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 20:52:04 +02:00
|
|
|
if let Err(err) = job.run(&homedir, &config) {
|
2021-03-29 02:01:53 +02:00
|
|
|
error!("couldn't process job: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 20:52:04 +02:00
|
|
|
fn parse_config(mut path: PathBuf) -> anyhow::Result<GlobalSettings> {
|
|
|
|
path.push("lohr-config");
|
|
|
|
path.set_extension("yaml");
|
|
|
|
let config = if let Ok(file) = File::open(path.as_path()) {
|
|
|
|
serde_yaml::from_reader(file)?
|
|
|
|
} else {
|
|
|
|
Default::default()
|
|
|
|
};
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
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 20:52:04 +02:00
|
|
|
let config = parse_config(homedir.clone())?;
|
|
|
|
|
2021-03-29 02:01:53 +02:00
|
|
|
thread::spawn(move || {
|
2021-03-29 20:52:04 +02:00
|
|
|
repo_updater(receiver, homedir, config);
|
2021-03-29 02:01:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", routes![gitea_webhook])
|
|
|
|
.manage(JobSender(Mutex::new(sender)))
|
|
|
|
.launch();
|
2021-03-29 20:52:04 +02:00
|
|
|
|
|
|
|
Ok(())
|
2021-03-27 10:42:49 +01:00
|
|
|
}
|