setup basic app

This commit is contained in:
Antoine Martin 2021-03-29 02:01:53 +02:00
parent 2cb5dbd22c
commit b37891af49
7 changed files with 1207 additions and 2 deletions

1111
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -9,3 +9,8 @@ description = "A Git mirroring daemon"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "1.0.40"
log = "0.4.14"
rocket = "0.4.7"
rocket_contrib = { version = "0.4.7", features = [ "json" ] }
serde = { version = "1.0.125", features = [ "derive" ] }

2
Rocket.toml Normal file
View file

@ -0,0 +1,2 @@
[global]
home = "./"

1
rust-toolchain Normal file
View file

@ -0,0 +1 @@
nightly

13
src/gitea.rs Normal file
View file

@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub(crate) struct Repository {
pub(crate) name: String,
pub(crate) full_name: String,
pub(crate) clone_url: String,
}
#[derive(Deserialize, Serialize)]
pub(crate) struct GiteaWebHook {
pub(crate) repository: Repository,
}

12
src/job.rs Normal file
View file

@ -0,0 +1,12 @@
pub struct Job {
repo: String,
}
impl Job {
pub fn new(repo: String) -> Self {
Self { repo }
}
pub fn run(&self) -> anyhow::Result<()> {
todo!()
}
}

View file

@ -1,3 +1,64 @@
fn main() { #![feature(proc_macro_hygiene, decl_macro)]
println!("Hello, world!");
use std::path::{Path, PathBuf};
use std::sync::{
mpsc::{channel, Receiver, Sender},
Mutex,
};
use std::thread;
use rocket::{fairing::AdHoc, http::Status, post, routes, State};
use rocket_contrib::json::Json;
use log::error;
mod gitea;
use gitea::GiteaWebHook;
mod job;
use job::Job;
struct HomeDir(PathBuf);
struct JobSender(Mutex<Sender<Job>>);
#[post("/", data = "<payload>")]
fn gitea_webhook(payload: Json<GiteaWebHook>, sender: State<JobSender>) -> Status {
{
let sender = sender.0.lock().unwrap();
sender
.send(Job::new(payload.repository.full_name.clone()))
.unwrap();
}
Status::Ok
}
fn repo_updater(rx: Receiver<Job>) {
loop {
let job = rx.recv().unwrap();
if let Err(err) = job.run() {
error!("couldn't process job: {}", err);
}
}
}
fn main() {
let (sender, receiver) = channel();
thread::spawn(move || {
repo_updater(receiver);
});
rocket::ignite()
.mount("/", routes![gitea_webhook])
.manage(JobSender(Mutex::new(sender)))
.attach(AdHoc::on_attach("Assets Config", |rocket| {
let home_dir = rocket.config().get_str("home").unwrap();
let home_dir = Path::new(home_dir).into();
Ok(rocket.manage(HomeDir(home_dir)))
}))
.launch();
} }