AV1Master/src/main.rs

104 lines
2.7 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-26 21:55:13 +01:00
use serde_json::json;
use serde::{Serialize, Deserialize};
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-26 21:55:13 +01:00
use std::collections::HashMap;
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 {
2020-03-26 21:55:13 +01:00
jobs: Mutex<HashMap<uuid::Uuid, WUnit>>
2020-03-25 22:30:22 +01:00
}
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-26 21:55:13 +01:00
let list = shared.jobs.lock().unwrap().clone();
println!("{:#?}", list);
//Json(json!("god hlep me"))
Json(serde_json::to_value(&list).unwrap())
2020-03-26 01:39:22 +01:00
}
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>> {
2020-03-26 21:55:13 +01:00
let mut list: Vec<WUnit> = shared.jobs.lock().unwrap()
2020-03-26 23:03:56 +01:00
.values()
.filter(|x| x.status == EStatus::Queued).cloned()
2020-03-26 21:55:13 +01:00
.collect();
2020-03-25 22:30:22 +01:00
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 21:55:13 +01:00
let list = shared.jobs.lock().unwrap();
2020-03-25 22:30:22 +01:00
2020-03-26 21:55:13 +01:00
let job = list.get(&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 23:03:56 +01:00
#[get("/edit_status/<id>/<status>")]
fn edit_status(id: Uuid, status: String, shared: State<SharedState>) -> Result<String, Box<std::error::Error>> {
let mut list = shared.jobs.lock().unwrap();
let job = list.get_mut(&id).ok_or("what")?;
let status = match status.as_str() {
"queued" => Ok(EStatus::Queued),
"reserved" => Ok(EStatus::Reserved),
"completed" => Ok(EStatus::Completed),
_ => Err("Not a valid status, valid statuses are queued, reserved, completed")
}?;
job.status = status;
Ok("Status changed".to_string())
}
2020-03-26 18:33:46 +01:00
#[post("/add_job", format = "json", data = "<message>")]
2020-03-26 23:03:56 +01:00
fn add_job(message: Json<workunit::WDesc>, shared: State<SharedState>) {
2020-03-26 18:33:46 +01:00
println!("{:#?}", message);
let job = message.into_inner();
2020-03-26 21:55:13 +01:00
let id = uuid::Uuid::new_v4();
shared.jobs.lock().unwrap().insert(id, WUnit::new(id, job));
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 23:03:56 +01:00
.mount("/", routes![index, version, get_jobs, get_job, request_job, edit_status, add_job])
2020-03-25 22:30:22 +01:00
.launch();
2020-03-24 22:21:29 +01:00
}