AV1Master/src/main.rs

75 lines
1.9 KiB
Rust
Raw Normal View History

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-24 22:21:29 +01:00
2020-03-25 02:14:49 +01:00
use std::path::PathBuf;
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);
list.sort_by(|a, b| b.length.cmp(&a.length));
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 01:39:22 +01:00
fn get_job(id: u32, shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
let list = shared.list.lock().unwrap().clone();
2020-03-25 22:30:22 +01:00
2020-03-26 01:39:22 +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 01:39:22 +01:00
#[post("/add_job")]
fn add_job(shared: State<SharedState>) -> Result<String, std::io::Error> {
shared.list.lock().unwrap().push(WUnit::new(2, "iduno", None, 10, workunit::EOptions::default()));
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
}