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.

126 lines
3.1 KiB

mod db;
mod handlers;
use warp::{body, Filter, Rejection};
4 years ago
#[macro_use]
extern crate log;
extern crate env_logger;
4 years ago
use db::lock_db;
use serde::de::DeserializeOwned;
use u_lib::{
config::MASTER_PORT,
messaging::{AsMsg, BaseMessage},
models::*,
};
4 years ago
use uuid::Uuid;
fn get_content<M>() -> impl Filter<Extract = (BaseMessage<'static, M>,), Error = Rejection> + Clone
where
M: AsMsg + Sync + Send + DeserializeOwned + 'static,
{
body::content_length_limit(1024 * 64).and(body::json::<BaseMessage<M>>())
4 years ago
}
fn prefill_jobs() {
let agent_hello = JobMeta::builder()
.with_type(misc::JobType::Manage)
.with_alias("agent_hello")
.build()
.unwrap();
lock_db().insert_jobs(&[agent_hello]).ok();
}
fn init() {
env_logger::init();
prefill_jobs();
}
4 years ago
#[tokio::main]
async fn main() {
init();
let infallible_none = |_| async { Ok::<(Option<Uuid>,), std::convert::Infallible>((None,)) };
4 years ago
let get_agents = warp::get()
.and(warp::path("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("upload_jobs"))
4 years ago
.and(get_content::<Vec<JobMeta>>())
.and_then(handlers::upload_jobs);
let get_jobs = warp::get()
.and(warp::path("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("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("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("del"))
4 years ago
.and(warp::path::param::<Uuid>())
.and_then(handlers::del);
4 years ago
4 years ago
let set_jobs = warp::post()
.and(warp::path("set_jobs"))
4 years ago
.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("report"))
.and(get_content::<Vec<ExecResult>>().and_then(handlers::report));
4 years ago
let auth_token = warp::header::exact("authorization", "Bearer 123qwe");
let agent_zone = get_jobs.clone().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() {
}
*/
}
*/