git init
This commit is contained in:
91
agent/src/main.rs
Normal file
91
agent/src/main.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
mod api;
|
||||
mod command;
|
||||
mod config;
|
||||
mod fail2ban;
|
||||
mod firewall;
|
||||
mod logs;
|
||||
mod network;
|
||||
mod services;
|
||||
mod system;
|
||||
mod wireguard;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use axum_server::{tls_rustls::RustlsConfig, Handle};
|
||||
use config::Config;
|
||||
use std::{env, path::PathBuf, time::Duration};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn main() -> Result<()> {
|
||||
let config_path = parse_args()?;
|
||||
let config = Config::load(&config_path)?;
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
EnvFilter::try_new(&config.logging.level).context("invalid logging filter")?,
|
||||
)
|
||||
.compact()
|
||||
.init();
|
||||
let state = api::AppState::load(config.clone())?;
|
||||
let app = api::router(state);
|
||||
let handle = Handle::new();
|
||||
let shutdown_handle = handle.clone();
|
||||
tokio::spawn(async move {
|
||||
wait_for_shutdown().await;
|
||||
tracing::info!("shutdown signal received");
|
||||
shutdown_handle.graceful_shutdown(Some(Duration::from_secs(10)));
|
||||
});
|
||||
|
||||
tracing::info!(listen = %config.server.listen, tls = config.tls.enabled, version = env!("CARGO_PKG_VERSION"), "vps-agent started");
|
||||
if config.tls.enabled {
|
||||
let tls =
|
||||
RustlsConfig::from_pem_file(&config.tls.certificate_file, &config.tls.private_key_file)
|
||||
.await
|
||||
.context("load TLS certificate/key")?;
|
||||
axum_server::bind_rustls(config.server.listen, tls)
|
||||
.handle(handle)
|
||||
.serve(app.into_make_service())
|
||||
.await?;
|
||||
} else {
|
||||
axum_server::bind(config.server.listen)
|
||||
.handle(handle)
|
||||
.serve(app.into_make_service())
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<PathBuf> {
|
||||
let mut args = env::args().skip(1);
|
||||
let mut config = PathBuf::from("/etc/vps-agent/config.toml");
|
||||
while let Some(argument) = args.next() {
|
||||
match argument.as_str() {
|
||||
"--config" => {
|
||||
config = args
|
||||
.next()
|
||||
.map(PathBuf::from)
|
||||
.context("--config needs a path")?
|
||||
}
|
||||
"--version" | "-V" => {
|
||||
println!("vps-agent {}", env!("CARGO_PKG_VERSION"));
|
||||
std::process::exit(0);
|
||||
}
|
||||
"--help" | "-h" => {
|
||||
println!("Usage: vps-agent [--config PATH] [--version]");
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => anyhow::bail!("unknown argument: {argument}"),
|
||||
}
|
||||
}
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn wait_for_shutdown() {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
let mut terminate = signal(SignalKind::terminate()).expect("install SIGTERM handler");
|
||||
tokio::select! { _ = tokio::signal::ctrl_c() => {}, _ = terminate.recv() => {} }
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
}
|
||||
Reference in New Issue
Block a user