2020-03-24 22:21:29 +01:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
|
|
|
|
#[macro_use] extern crate rocket;
|
2020-03-25 22:30:22 +01:00
|
|
|
use rocket::State;
|
|
|
|
use rocket::response::status::NotFound;
|
|
|
|
|
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use serde_json::Value;
|
2020-03-26 18:33:46 +01:00
|
|
|
use rocket_contrib::uuid::Uuid;
|
2020-03-24 22:21:29 +01:00
|
|
|
|
2020-03-25 22:30:22 +01:00
|
|
|
use std::sync::Mutex;
|
2020-03-25 02:14:49 +01:00
|
|
|
|
|
|
|
mod workunit;
|
|
|
|
use workunit::WUnit;
|
2020-03-26 01:39:22 +01:00
|
|
|
use workunit::EStatus;
|
2020-03-25 02:14:49 +01:00
|
|
|
|
2020-03-25 22:30:22 +01:00
|
|
|
const VERSION: &str = "0.1.0";
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
struct SharedState {
|
|
|
|
list: Mutex<Vec<WUnit>>
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:21:29 +01:00
|
|
|
#[get("/")]
|
|
|
|
fn index() -> &'static str {
|
2020-03-25 02:14:49 +01:00
|
|
|
"Welcome to the AV1 Encoder Master Server"
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:30:22 +01:00
|
|
|
#[get("/version")]
|
|
|
|
fn version() -> &'static str {
|
|
|
|
"0.1.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/get_jobs")]
|
2020-03-26 01:39:22 +01:00
|
|
|
fn get_jobs(shared: State<SharedState>) -> Json<Value> {
|
2020-03-25 22:30:22 +01:00
|
|
|
let list = shared.list.lock().unwrap();
|
2020-03-26 01:39:22 +01:00
|
|
|
Json(serde_json::to_value(&list[..]).unwrap())
|
|
|
|
}
|
2020-03-25 22:30:22 +01:00
|
|
|
|
2020-03-26 01:39:22 +01:00
|
|
|
#[get("/request_job")]
|
|
|
|
fn request_job(shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
|
|
|
|
let mut list = shared.list.lock().unwrap().clone();
|
2020-03-25 22:30:22 +01:00
|
|
|
|
2020-03-26 01:39:22 +01:00
|
|
|
list.retain(|x| x.status == EStatus::Queued);
|
2020-03-26 18:33:46 +01:00
|
|
|
list.sort_by(|a, b| b.description.length.cmp(&a.description.length));
|
2020-03-26 01:39:22 +01:00
|
|
|
|
|
|
|
let job = list.get(0);
|
|
|
|
|
|
|
|
Ok(Json(serde_json::to_value(&job).unwrap()))
|
2020-03-25 22:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/get_job/<id>")]
|
2020-03-26 18:33:46 +01:00
|
|
|
fn get_job(id: Uuid, shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
|
2020-03-26 01:39:22 +01:00
|
|
|
let list = shared.list.lock().unwrap().clone();
|
2020-03-25 22:30:22 +01:00
|
|
|
|
2020-03-26 18:33:46 +01:00
|
|
|
let job = list.into_iter().find(|x| x.id == *id).ok_or(NotFound(format!("Job not Found: {id}", id = id)));
|
2020-03-25 22:30:22 +01:00
|
|
|
|
|
|
|
match job {
|
2020-03-26 01:39:22 +01:00
|
|
|
Ok(j) => Ok(Json(serde_json::to_value(&j).unwrap())),
|
2020-03-25 22:30:22 +01:00
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 18:33:46 +01:00
|
|
|
#[post("/add_job", format = "json", data = "<message>")]
|
|
|
|
fn add_job(message: Json<workunit::WDesc>, shared: State<SharedState>) -> Result<String, String> {
|
|
|
|
println!("{:#?}", message);
|
|
|
|
let job = message.into_inner();
|
|
|
|
shared.list.lock().unwrap().push(WUnit::new(job));
|
2020-03-26 01:39:22 +01:00
|
|
|
Ok(format!("{:#?}", shared))
|
2020-03-24 22:21:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-03-26 01:39:22 +01:00
|
|
|
rocket::ignite()
|
2020-03-25 22:30:22 +01:00
|
|
|
.manage(SharedState::default())
|
2020-03-26 01:39:22 +01:00
|
|
|
.mount("/", routes![index, version, get_jobs, get_job, request_job, add_job])
|
2020-03-25 22:30:22 +01:00
|
|
|
.launch();
|
2020-03-24 22:21:29 +01:00
|
|
|
}
|