|
|
|
@ -13,56 +13,27 @@ extern crate diesel; |
|
|
|
|
// in this block
|
|
|
|
|
|
|
|
|
|
mod db; |
|
|
|
|
mod filters; |
|
|
|
|
mod handlers; |
|
|
|
|
mod init; |
|
|
|
|
|
|
|
|
|
use db::UDB; |
|
|
|
|
use filters::make_filters; |
|
|
|
|
use init::*; |
|
|
|
|
use serde::Deserialize; |
|
|
|
|
use std::path::PathBuf; |
|
|
|
|
use u_lib::{config::MASTER_PORT, models::*, utils::init_env}; |
|
|
|
|
use u_lib::{config::MASTER_PORT, utils::Env}; |
|
|
|
|
use warp::Filter; |
|
|
|
|
|
|
|
|
|
const LOGFILE: &str = "u_server.log"; |
|
|
|
|
|
|
|
|
|
fn prefill_jobs() { |
|
|
|
|
let agent_hello = JobMeta::builder() |
|
|
|
|
.with_type(misc::JobType::Manage) |
|
|
|
|
.with_alias("agent_hello") |
|
|
|
|
.build() |
|
|
|
|
.unwrap(); |
|
|
|
|
UDB::lock_db().insert_jobs(&[agent_hello]).unwrap(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn init_logger() { |
|
|
|
|
use simplelog::*; |
|
|
|
|
use std::fs::OpenOptions; |
|
|
|
|
let log_cfg = ConfigBuilder::new() |
|
|
|
|
.set_time_format_str("%x %X") |
|
|
|
|
.set_time_to_local(true) |
|
|
|
|
.build(); |
|
|
|
|
let logfile = OpenOptions::new() |
|
|
|
|
.append(true) |
|
|
|
|
.create(true) |
|
|
|
|
.open(PathBuf::from("logs").join(LOGFILE)) |
|
|
|
|
.unwrap(); |
|
|
|
|
let level = LevelFilter::Info; |
|
|
|
|
let loggers = vec![ |
|
|
|
|
WriteLogger::new(level, log_cfg.clone(), logfile) as Box<dyn SharedLogger>, |
|
|
|
|
TermLogger::new(level, log_cfg, TerminalMode::Stderr, ColorChoice::Auto), |
|
|
|
|
]; |
|
|
|
|
CombinedLogger::init(loggers).unwrap(); |
|
|
|
|
#[derive(Deserialize)] |
|
|
|
|
struct ServEnv { |
|
|
|
|
admin_auth_token: String, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn init_all() { |
|
|
|
|
//TODO: tracing-subscriber
|
|
|
|
|
pub async fn serve() -> Result<(), String> { |
|
|
|
|
init_logger(); |
|
|
|
|
init_env(); |
|
|
|
|
prefill_jobs(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//TODO: tracing-subscriber
|
|
|
|
|
pub async fn serve() { |
|
|
|
|
init_all(); |
|
|
|
|
let routes = make_filters(); |
|
|
|
|
let env = Env::<ServEnv>::init().map_err(|e| e.to_string())?; |
|
|
|
|
let routes = init_filters(&env.inner.admin_auth_token); |
|
|
|
|
let certs_dir = PathBuf::from("certs"); |
|
|
|
|
warp::serve(routes.with(warp::log("warp"))) |
|
|
|
|
.tls() |
|
|
|
@ -71,6 +42,7 @@ pub async fn serve() { |
|
|
|
|
.client_auth_required_path(certs_dir.join("ca.crt")) |
|
|
|
|
.run(([0, 0, 0, 0], MASTER_PORT)) |
|
|
|
|
.await; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
@ -97,7 +69,7 @@ mod tests { |
|
|
|
|
uid.map(|u| u.simple().to_string()).unwrap_or(String::new()) |
|
|
|
|
)) |
|
|
|
|
.method("GET") |
|
|
|
|
.filter(&make_filters()) |
|
|
|
|
.filter(&init_filters("")) |
|
|
|
|
.await |
|
|
|
|
.unwrap(); |
|
|
|
|
mock.checkpoint(); |
|
|
|
@ -113,7 +85,7 @@ mod tests { |
|
|
|
|
.path("/report/") |
|
|
|
|
.method("POST") |
|
|
|
|
.json(&vec![Reportable::Dummy].as_message()) |
|
|
|
|
.filter(&make_filters()) |
|
|
|
|
.filter(&init_filters("")) |
|
|
|
|
.await |
|
|
|
|
.unwrap(); |
|
|
|
|
mock.checkpoint(); |
|
|
|
|