feat: 完成登录与栅格下载桌面端
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
use std::net::{SocketAddr, TcpStream};
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
static BACKEND: Mutex<Option<Child>> = Mutex::new(None);
|
||||
|
||||
fn backend_up() -> bool {
|
||||
let address: SocketAddr = "127.0.0.1:8765".parse().expect("address");
|
||||
TcpStream::connect_timeout(&address, Duration::from_millis(200)).is_ok()
|
||||
}
|
||||
|
||||
fn start_backend() {
|
||||
if backend_up() {
|
||||
return;
|
||||
}
|
||||
let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let Some(repo) = manifest.parent().and_then(|path| path.parent()) else {
|
||||
return;
|
||||
};
|
||||
let python = repo
|
||||
.join("backend")
|
||||
.join(".venv")
|
||||
.join("Scripts")
|
||||
.join("python.exe");
|
||||
if !python.exists() {
|
||||
return;
|
||||
}
|
||||
let log_dir = repo.join("data");
|
||||
let _ = std::fs::create_dir_all(&log_dir);
|
||||
let mut command = Command::new(python);
|
||||
command
|
||||
.args([
|
||||
"-m",
|
||||
"uvicorn",
|
||||
"app.main:app",
|
||||
"--host",
|
||||
"127.0.0.1",
|
||||
"--port",
|
||||
"8765",
|
||||
])
|
||||
.current_dir(repo.join("backend"))
|
||||
.stdin(Stdio::null());
|
||||
if let Ok(log) = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(log_dir.join("backend.log"))
|
||||
{
|
||||
if let Ok(error_log) = log.try_clone() {
|
||||
command.stdout(log).stderr(error_log);
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
command.creation_flags(0x0800_0000);
|
||||
}
|
||||
if let Ok(child) = command.spawn() {
|
||||
*BACKEND.lock().expect("backend lock") = Some(child);
|
||||
}
|
||||
}
|
||||
|
||||
fn stop_backend() {
|
||||
if let Ok(mut slot) = BACKEND.lock() {
|
||||
if let Some(child) = slot.as_mut() {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
}
|
||||
*slot = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
start_backend();
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building tauri application")
|
||||
.run(|_app, event| {
|
||||
if matches!(event, tauri::RunEvent::Exit) {
|
||||
stop_backend();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user