From 74fcb5b59b2f0fd3ed80aa9266095b2149ffd3e3 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Mon, 17 Feb 2025 02:50:39 +0800 Subject: [PATCH] update: api friendly --- src/service/core.rs | 8 +++----- src/service/mod.rs | 17 ++++++----------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/service/core.rs b/src/service/core.rs index e7920a2..709cea5 100644 --- a/src/service/core.rs +++ b/src/service/core.rs @@ -129,11 +129,9 @@ impl CoreManager { let current_pid = std::process::id() as i32; if is_running_clash && clash_running_pid == current_pid { - println!("Clash is already running with the same PID"); - } else if is_running_clash && clash_running_pid > 0 { - println!("Clash is running with a different PID, stopping it first"); - self.stop_clash()?; - } else if !is_running_clash && clash_running_pid < 1 { + println!("Clash is already running with pid: {}", current_pid); + } + if !is_running_clash && clash_running_pid <= 0 { let current_pid = std::process::id() as i32; println!("Clash is start running with pid: {}", current_pid); self.clash_status.inner.lock().unwrap().running_pid.store(current_pid, Ordering::Relaxed); diff --git a/src/service/mod.rs b/src/service/mod.rs index aaf1448..2935383 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -78,14 +78,6 @@ pub async fn run_service() -> anyhow::Result<()> { .and(warp::path("is_healthy")) .map(move || wrap_response!(COREMANAGER.lock().unwrap().is_healthy())); - let api_stop_mihomo = warp::post() - .and(warp::path("stop_mihomo")) - .map(move || wrap_response!(COREMANAGER.lock().unwrap().stop_mihomo())); - - let api_start_mihomo = warp::post() - .and(warp::path("start_mihomo")) - .map(move || wrap_response!(COREMANAGER.lock().unwrap().start_mihomo())); - let api_start_clash = warp::post() .and(warp::path("start_clash")) .and(warp::body::json()) @@ -93,7 +85,7 @@ pub async fn run_service() -> anyhow::Result<()> { let api_stop_clash = warp::post() .and(warp::path("stop_clash")) - .map(move || wrap_response!(COREMANAGER.lock().unwrap().stop_clash())); + .map(move || wrap_response!(COREMANAGER.lock().unwrap().stop_mihomo())); let api_get_clash = warp::get() .and(warp::path("get_clash")) @@ -103,6 +95,10 @@ pub async fn run_service() -> anyhow::Result<()> { .and(warp::path("stop_service")) .map(|| wrap_response!(stop_service())); + let api_exit_sys = warp::post() + .and(warp::path("exit_sys")) + .map(move || wrap_response!(COREMANAGER.lock().unwrap().stop_clash())); + warp::serve( api_get_version .or(api_is_healthy) @@ -110,8 +106,7 @@ pub async fn run_service() -> anyhow::Result<()> { .or(api_stop_clash) .or(api_stop_service) .or(api_get_clash) - .or(api_start_mihomo) - .or(api_stop_mihomo), + .or(api_exit_sys) ) .run(([127, 0, 0, 1], LISTEN_PORT)) .await;