AV1Master/src/main.rs

132 lines
4.2 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;
2020-03-30 19:43:37 +02:00
use rocket::Data;
2020-03-25 22:30:22 +01:00
use rocket_contrib::json::Json;
use serde_json::Value;
2020-03-26 21:55:13 +01:00
use serde_json::json;
2020-03-26 18:33:46 +01:00
use rocket_contrib::uuid::Uuid;
2020-03-30 02:21:16 +02:00
use rocket_contrib::serve::StaticFiles;
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-30 20:43:47 +02:00
use std::path::Path;
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-04-20 09:20:07 +02:00
const VERSION: &str = "0.5.0";
2020-03-25 22:30:22 +01:00
#[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("/")]
2020-03-30 02:21:16 +02:00
fn index() -> String {
format!("Wecome to the AV1Master Server version {version}\n
This currently requires a distro with CAP_SYS_USER_NS enabled and correct permissions
curl -L {baseurl}/client.sh > client.sh && chmod +x ./av1client && ./av1client {baseurl}", version=VERSION, baseurl="https://av1.dodsorf.as")
2020-03-25 02:14:49 +01:00
}
2020-03-25 22:30:22 +01:00
#[get("/version")]
fn version() -> &'static str {
2020-03-28 02:55:31 +01:00
VERSION
2020-03-25 22:30:22 +01:00
}
#[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();
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-30 02:21:16 +02:00
pub struct RealIP(std::net::IpAddr);
impl<'a, 'r> rocket::request::FromRequest<'a, 'r> for RealIP {
type Error = ();
fn from_request(request: &'a rocket::Request<'r>) -> rocket::request::Outcome<Self, Self::Error> {
match request.client_ip() {
Some(ip) => rocket::Outcome::Success(RealIP(ip)),
None => rocket::Outcome::Failure((rocket::http::Status::from_code(401).unwrap(), ()))
}
}
}
2020-03-26 23:03:56 +01:00
#[get("/edit_status/<id>/<status>")]
2020-03-30 02:21:16 +02:00
fn edit_status(id: Uuid, status: String, shared: State<SharedState>, remote_addr: RealIP) -> Result<String, Box<std::error::Error>> {
2020-03-26 23:03:56 +01:00
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),
2020-03-30 02:21:16 +02:00
"reserved" => Ok(EStatus::Reserved(remote_addr.0.to_string())),
"completed" => Ok(EStatus::Completed(remote_addr.0.to_string())),
"error" => Ok(EStatus::Error(remote_addr.0.to_string())),
2020-03-28 02:55:31 +01:00
_ => Err("Not a valid status, valid statuses are queued, reserved, completed, and error")
2020-03-26 23:03:56 +01:00
}?;
job.status = status;
Ok("Status changed".to_string())
}
2020-03-30 19:43:37 +02:00
2020-03-30 20:43:47 +02:00
#[post("/upload/<id>", data = "<video>")]
fn upload(id: Uuid, video: Data, shared: State<SharedState>) -> Result<String, std::io::Error> {
if shared.jobs.lock().unwrap().contains_key(&id) == false {
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Job not found"))
}
else {
let filename = format!("results/{name}.{id}.webm", name = shared.jobs.lock().unwrap().get(&id).unwrap().description.file_name, id = id);
let url = format!("{host}/{id}\n", host = "https://av1.dodsorf.as", id = id);
video.stream_to_file(Path::new(&filename))?;
Ok(url)
}
2020-03-30 19:43:37 +02:00
}
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
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-30 02:21:16 +02:00
.mount("/", StaticFiles::from("src/static")) // switch to templates or something cause this is dumb
2020-03-30 20:43:47 +02:00
.mount("/", routes![index, version, get_jobs, get_job, request_job, edit_status, add_job, upload])
2020-03-25 22:30:22 +01:00
.launch();
2020-03-24 22:21:29 +01:00
}