server.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. extern crate warp;
  2. use port_scanner::local_port_available;
  3. use tauri::{AppHandle, Manager};
  4. use warp::Filter;
  5. #[cfg(not(feature = "verge-dev"))]
  6. const SERVER_PORT: u16 = 33333;
  7. #[cfg(feature = "verge-dev")]
  8. const SERVER_PORT: u16 = 11233;
  9. /// check whether there is already exists
  10. pub fn check_singleton() -> Result<(), ()> {
  11. if !local_port_available(SERVER_PORT) {
  12. tauri::async_runtime::block_on(async {
  13. let url = format!("http://127.0.0.1:{}/commands/visible", SERVER_PORT);
  14. reqwest::get(url).await.unwrap();
  15. Err(())
  16. })
  17. } else {
  18. Ok(())
  19. }
  20. }
  21. /// The embed server only be used to implement singleton process
  22. /// maybe it can be used as pac server later
  23. pub fn embed_server(app: &AppHandle) {
  24. let window = app.get_window("main").unwrap();
  25. tauri::async_runtime::spawn(async move {
  26. let commands = warp::path!("commands" / "visible").map(move || {
  27. window.show().unwrap();
  28. window.set_focus().unwrap();
  29. return format!("ok");
  30. });
  31. warp::serve(commands)
  32. .bind(([127, 0, 0, 1], SERVER_PORT))
  33. .await;
  34. });
  35. }