feat: add server process control panel to Tauri app
This commit is contained in:
1 parent
818d9f99e6
commit
99672b33d0
7 files changed
+437
-2
No files matched your search
@@ -1,9 +1,79 @@
|
||||
use tauri::Manager;
|
||||
use std::process::{Child, Command};
|
||||
use std::sync::Mutex;
|
||||
use tauri::{Manager, State};
|
||||
|
||||
pub struct ServerState(pub Mutex<Option<Child>>);
|
||||
|
||||
#[tauri::command]
|
||||
fn start_server(
|
||||
state: State<ServerState>,
|
||||
model_path: String,
|
||||
addr: String,
|
||||
gpu_layers: i32,
|
||||
) -> Result<String, String> {
|
||||
let mut guard = state.0.lock().map_err(|e| e.to_string())?;
|
||||
|
||||
if guard.is_some() {
|
||||
return Err("Server is already running".into());
|
||||
}
|
||||
|
||||
// Resolve binary path relative to the app's resource dir or PATH
|
||||
let binary = if cfg!(target_os = "windows") {
|
||||
"localllm.exe"
|
||||
} else {
|
||||
"localllm"
|
||||
};
|
||||
|
||||
let child = Command::new(binary)
|
||||
.env("LOCALLLM_MODEL", &model_path)
|
||||
.env("LOCALLLM_ADDR", &addr)
|
||||
.env("LOCALLLM_GPU_LAYERS", gpu_layers.to_string())
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to start server: {e}"))?;
|
||||
|
||||
let pid = child.id();
|
||||
*guard = Some(child);
|
||||
|
||||
Ok(format!("Server started (pid={pid}) on {addr}"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn stop_server(state: State<ServerState>) -> Result<String, String> {
|
||||
let mut guard = state.0.lock().map_err(|e| e.to_string())?;
|
||||
|
||||
match guard.as_mut() {
|
||||
None => Err("Server is not running".into()),
|
||||
Some(child) => {
|
||||
child
|
||||
.kill()
|
||||
.map_err(|e| format!("Failed to kill server: {e}"))?;
|
||||
let _ = child.wait();
|
||||
*guard = None;
|
||||
Ok("Server stopped".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn server_status(state: State<ServerState>) -> String {
|
||||
let guard = state.0.lock().unwrap();
|
||||
if guard.is_some() {
|
||||
"running".into()
|
||||
} else {
|
||||
"stopped".into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.manage(ServerState(Mutex::new(None)))
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
start_server,
|
||||
stop_server,
|
||||
server_status
|
||||
])
|
||||
.setup(|app| {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user