You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.1 KiB

mod db;
mod handlers;
use warp::{body, Filter, Rejection, Reply};
4 years ago
extern crate env_logger;
extern crate log;
4 years ago
use db::lock_db;
use serde::de::DeserializeOwned;
use u_lib::{api::Paths, config::MASTER_PORT, models::*};
4 years ago
use uuid::Uuid;
fn get_content<M>() -> impl Filter<Extract = (BaseMessage<'static, M>,), Error = Rejection> + Clone
where
M: ToMsg + Sync + Send + DeserializeOwned + 'static,
{
body::content_length_limit(1024 * 64).and(body::json::<BaseMessage<M>>())
4 years ago
}
fn init() {
env_logger::init();
lock_db();
}
4 years ago
#[tokio::main]
async fn main() {
init();
let infallible_none = |_| async { Ok::<(Option<Uuid>,), std::convert::Infallible>((None,)) };
4 years ago
4 years ago
let new_agent = warp::post()
.and(warp::path(Paths::init))
4 years ago
.and(get_content::<IAgent>())
.and_then(handlers::add_agent);
let get_agents = warp::get()
.and(warp::path(Paths::get_agents))
.and(
warp::path::param::<Uuid>()
.map(Some)
.or_else(infallible_none),
)
.and_then(handlers::get_agents);
4 years ago
let upload_jobs = warp::post()
.and(warp::path(Paths::upload_jobs))
.and(get_content::<Vec<JobMeta>>())
.and_then(handlers::upload_jobs);
let get_jobs = warp::get()
.and(warp::path(Paths::get_jobs))
.and(
warp::path::param::<Uuid>()
.map(Some)
.or_else(infallible_none),
)
.and_then(handlers::get_jobs);
4 years ago
let get_agent_jobs = warp::get()
.and(warp::path(Paths::get_agent_jobs))
.and(
warp::path::param::<Uuid>()
.map(Some)
.or_else(infallible_none),
)
.and_then(|uid| handlers::get_agent_jobs(uid, false));
4 years ago
let get_personal_jobs = warp::get()
.and(warp::path(Paths::get_agent_jobs))
.and(warp::path::param::<Uuid>().map(Some))
.and_then(|uid| handlers::get_agent_jobs(uid, true));
4 years ago
let del = warp::get()
.and(warp::path(Paths::del))
.and(warp::path::param::<Uuid>())
.and_then(handlers::del);
4 years ago
4 years ago
let set_jobs = warp::post()
.and(warp::path(Paths::set_jobs))
.and(warp::path::param::<Uuid>())
4 years ago
.and(get_content::<Vec<Uuid>>())
.and_then(handlers::set_jobs);
4 years ago
let report = warp::post()
.and(warp::path(Paths::report))
.and(get_content::<Vec<ExactJob>>())
4 years ago
.and_then(handlers::report);
4 years ago
let auth_token = warp::header::exact("authorization", "Bearer 123qwe");
4 years ago
let agent_zone = new_agent
.or(get_jobs.clone())
4 years ago
.or(get_personal_jobs)
.or(report);
let auth_zone = auth_token.and(
get_agents
.or(get_jobs)
.or(upload_jobs)
.or(del)
.or(set_jobs)
.or(get_agent_jobs),
);
let routes = agent_zone.or(auth_zone);
4 years ago
warp::serve(routes.with(warp::log("warp")))
.run(([0, 0, 0, 0], MASTER_PORT))
.await;
4 years ago
}
#[cfg(test)]
mod tests {
use super::*;
/*
#[tokio::test]
async fn test_gather() {
}
*/
}