parent
							
								
									4e88f49f96
								
							
						
					
					
						commit
						4bfcdd2b23
					
				
				 10 changed files with 235 additions and 143 deletions
			
			
		| @ -0,0 +1,88 @@ | |||||||
|  | use crate::handlers::Endpoints; | ||||||
|  | use serde::de::DeserializeOwned; | ||||||
|  | use std::env; | ||||||
|  | use u_lib::{ | ||||||
|  |     messaging::{AsMsg, BaseMessage}, | ||||||
|  |     models::*, | ||||||
|  | }; | ||||||
|  | use uuid::Uuid; | ||||||
|  | use warp::{body, Filter, Rejection, Reply}; | ||||||
|  | 
 | ||||||
|  | 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>>()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn make_filters() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone { | ||||||
|  |     let infallible_none = |_| async { Ok::<(Option<Uuid>,), std::convert::Infallible>((None,)) }; | ||||||
|  | 
 | ||||||
|  |     let get_agents = warp::get() | ||||||
|  |         .and(warp::path("get_agents")) | ||||||
|  |         .and( | ||||||
|  |             warp::path::param::<Uuid>() | ||||||
|  |                 .map(Some) | ||||||
|  |                 .or_else(infallible_none), | ||||||
|  |         ) | ||||||
|  |         .and_then(Endpoints::get_agents); | ||||||
|  | 
 | ||||||
|  |     let upload_jobs = warp::post() | ||||||
|  |         .and(warp::path("upload_jobs")) | ||||||
|  |         .and(get_content::<Vec<JobMeta>>()) | ||||||
|  |         .and_then(Endpoints::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(Endpoints::get_jobs); | ||||||
|  | 
 | ||||||
|  |     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| Endpoints::get_agent_jobs(uid, false)); | ||||||
|  | 
 | ||||||
|  |     let get_personal_jobs = warp::get() | ||||||
|  |         .and(warp::path("get_agent_jobs")) | ||||||
|  |         .and(warp::path::param::<Uuid>().map(Some)) | ||||||
|  |         .and_then(|uid| Endpoints::get_agent_jobs(uid, true)); | ||||||
|  | 
 | ||||||
|  |     let del = warp::get() | ||||||
|  |         .and(warp::path("del")) | ||||||
|  |         .and(warp::path::param::<Uuid>()) | ||||||
|  |         .and_then(Endpoints::del); | ||||||
|  | 
 | ||||||
|  |     let set_jobs = warp::post() | ||||||
|  |         .and(warp::path("set_jobs")) | ||||||
|  |         .and(warp::path::param::<Uuid>()) | ||||||
|  |         .and(get_content::<Vec<String>>()) | ||||||
|  |         .and_then(Endpoints::set_jobs); | ||||||
|  | 
 | ||||||
|  |     let report = warp::post() | ||||||
|  |         .and(warp::path("report")) | ||||||
|  |         .and(get_content::<Vec<ExecResult>>().and_then(Endpoints::report)); | ||||||
|  | 
 | ||||||
|  |     let auth_token = format!("Bearer {}", env::var("ADMIN_AUTH_TOKEN").unwrap()).into_boxed_str(); | ||||||
|  |     let auth_header = warp::header::exact("authorization", Box::leak(auth_token)); | ||||||
|  | 
 | ||||||
|  |     let agent_zone = get_jobs.clone().or(get_personal_jobs).or(report); | ||||||
|  | 
 | ||||||
|  |     let auth_zone = auth_header.and( | ||||||
|  |         get_agents | ||||||
|  |             .or(get_jobs) | ||||||
|  |             .or(upload_jobs) | ||||||
|  |             .or(del) | ||||||
|  |             .or(set_jobs) | ||||||
|  |             .or(get_agent_jobs), | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     agent_zone.or(auth_zone) | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue