Skip to content

Commit

Permalink
update: api friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunglies committed Feb 16, 2025
1 parent 86f26e9 commit 74fcb5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/service/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 6 additions & 11 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,14 @@ 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())
.map(move |body: StartBody| wrap_response!(COREMANAGER.lock().unwrap().start_clash(body)));

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"))
Expand All @@ -103,15 +95,18 @@ 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)
.or(api_start_clash)
.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;
Expand Down

0 comments on commit 74fcb5b

Please sign in to comment.