get periphery version route

This commit is contained in:
mbecker20
2023-01-09 03:54:16 +00:00
parent cffe3b1428
commit e26769a787
5 changed files with 41 additions and 18 deletions
+1
View File
@@ -10,6 +10,7 @@ use helpers::*;
fn cli() -> Command {
Command::new("monitor")
.about("\na cli to set up monitor components, like the periphery client")
.version(env!("CARGO_PKG_VERSION"))
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
+4 -2
View File
@@ -3,15 +3,17 @@ use axum::{routing::post, Json, Router};
use helpers::{handle_anyhow_error, run_monitor_command};
use types::Command;
use crate::HomeDirExtension;
pub fn router() -> Router {
Router::new().route(
"/",
post(|Json(Command { path, command })| async move {
post(|home_dir: HomeDirExtension, Json(Command { path, command })| async move {
tokio::spawn(async move {
let command = if path.is_empty() {
command
} else {
let path = path.replace("~", &std::env::var("HOME").unwrap());
let path = path.replace("~", &home_dir);
format!("cd {path} && {command}")
};
let log = run_monitor_command("run command", command).await;
+4 -2
View File
@@ -13,7 +13,7 @@ use helpers::docker::DockerClient;
use serde_json::Value;
use types::{monitor_timestamp, PeripheryConfig};
use crate::PeripheryConfigExtension;
use crate::{PeripheryConfigExtension, HomeDirExtension};
mod accounts;
mod build;
@@ -24,9 +24,10 @@ mod image;
mod network;
mod stats;
pub fn router(config: PeripheryConfigExtension) -> Router {
pub fn router(config: PeripheryConfigExtension, home_dir: HomeDirExtension) -> Router {
Router::new()
.route("/health", get(|| async {}))
.route("/version", get(|| async { env!("CARGO_PKG_VERSION") }))
.route("/accounts/:account_type", get(accounts::get_accounts))
.nest("/command", command::router())
.nest("/container", container::router())
@@ -41,6 +42,7 @@ pub fn router(config: PeripheryConfigExtension) -> Router {
.layer(DockerClient::extension())
.layer(middleware::from_fn(guard_request))
.layer(config)
.layer(home_dir)
}
async fn guard_request(
+23 -4
View File
@@ -7,7 +7,7 @@ use helpers::parse_config_file;
use serde::Deserialize;
use types::PeripheryConfig;
use crate::PeripheryConfigExtension;
use crate::{PeripheryConfigExtension, HomeDirExtension};
#[derive(Parser)]
#[command(author = "mbecker20 <becker.maxh@gmail.com>")]
@@ -28,6 +28,12 @@ pub struct Args {
/// Sets the path of config file to use
#[arg(short, long)]
pub config_path: Option<String>,
#[arg(short, long)]
pub home_dir: Option<String>,
#[arg(short, long)]
version: bool,
}
#[derive(Deserialize)]
@@ -36,20 +42,25 @@ struct Env {
config_path: String,
}
pub fn load() -> (Args, u16, PeripheryConfigExtension) {
pub fn load() -> (Args, u16, PeripheryConfigExtension, HomeDirExtension) {
dotenv().ok();
let env: Env = envy::from_env().expect("failed to parse env");
let args = Args::parse();
if args.version {
println!("v{}", env!("CARGO_PKG_VERSION"));
std::process::exit(0)
}
let home_dir = get_home_dir(&args.home_dir);
let config_path = args
.config_path
.as_ref()
.unwrap_or(&env.config_path)
.replace("~", &std::env::var("HOME").unwrap());
.replace("~", &home_dir);
let config =
parse_config_file::<PeripheryConfig>(&config_path).expect("failed to parse config file");
let _ = std::fs::create_dir(&config.repo_dir);
print_startup_log(&config_path, &args, &config);
(args, config.port, Extension(Arc::new(config)))
(args, config.port, Extension(Arc::new(config)), Extension(Arc::new(home_dir)))
}
fn print_startup_log(config_path: &str, args: &Args, config: &PeripheryConfig) {
@@ -80,3 +91,11 @@ fn print_startup_log(config_path: &str, args: &Args, config: &PeripheryConfig) {
fn default_config_path() -> String {
"/config/periphery.config.toml".to_string()
}
fn get_home_dir(home_dir_arg: &Option<String>) -> String {
match home_dir_arg {
Some(home_dir) => home_dir.to_string(),
None => std::env::var("$HOME")
.expect("did not find $HOME env var, should pass home dir with arg --home-dir"),
}
}
+9 -10
View File
@@ -1,6 +1,6 @@
// #![allow(unused)]
use std::{env, fs::File, net::SocketAddr, sync::Arc};
use std::{fs::File, net::SocketAddr, sync::Arc};
use ::helpers::get_socket_addr;
use axum::Extension;
@@ -12,28 +12,27 @@ mod config;
mod helpers;
type PeripheryConfigExtension = Extension<Arc<PeripheryConfig>>;
type HomeDirExtension = Extension<Arc<String>>;
fn main() {
let (args, port, config) = config::load();
let home = env::var("HOME").unwrap();
let (args, port, config, home_dir) = config::load();
if args.daemon {
let stdout = File::create(args.stdout.replace("~", &home)).unwrap();
let stderr = File::create(args.stderr.replace("~", &home)).unwrap();
let stdout = File::create(args.stdout.replace("~", &home_dir)).expect("failed to create stdout log file");
let stderr = File::create(args.stderr.replace("~", &home_dir)).expect("failed to create stderr log file");
let daemon = Daemonize::new().stdout(stdout).stderr(stderr);
match daemon.start() {
Ok(_) => println!("process sucessfully started"),
Ok(_) => println!("monitor periphery"),
Err(e) => eprintln!("Error, {}", e),
}
}
run_periphery_server(port, config)
run_periphery_server(port, config, home_dir)
}
#[tokio::main]
async fn run_periphery_server(port: u16, config: PeripheryConfigExtension) {
let app = api::router(config);
async fn run_periphery_server(port: u16, config: PeripheryConfigExtension, home_dir: HomeDirExtension) {
let app = api::router(config, home_dir);
axum::Server::bind(&get_socket_addr(port))
.serve(app.into_make_service_with_connect_info::<SocketAddr>())