1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![allow(
  clippy::module_name_repetitions,
  clippy::cast_possible_truncation,
  clippy::cast_sign_loss
)]
use crate::context::SharedJoinShandle;
use crate::settings::SharedAppSettings;
use factorio_bot_core::miette::Result;
use factorio_bot_core::process::process_control::SharedFactorioInstance;
#[cfg(feature = "restapi")]
use factorio_bot_restapi::webserver;
use tauri::State;

#[allow(unused_variables)]
#[allow(clippy::unused_async)]
#[tauri::command]
pub async fn start_restapi(
  app_settings: State<'_, SharedAppSettings>,
  instance_state: State<'_, SharedFactorioInstance>,
  restapi_handle: State<'_, SharedJoinShandle<Result<()>>>,
) -> Result<(), String> {
  #[cfg(feature = "restapi")]
  {
    if restapi_handle.read().await.is_some() {
      return Err("already started".into());
    }
    let app_settings = app_settings.inner().clone();
    let instance_state = instance_state.inner().clone();
    let app_settings = app_settings.read().await;
    let webserver = webserver::start(app_settings.restapi.clone(), instance_state);
    let handle = tokio::task::spawn(webserver);
    let mut restapi_handle = restapi_handle.write().await;
    *restapi_handle = Some(handle);
    Ok(())
  }
  #[cfg(not(feature = "restapi"))]
  {
    Err("restapi unavailable".into())
  }
}

#[tauri::command]
pub async fn stop_restapi(
  restapi_handle: State<'_, SharedJoinShandle<Result<()>>>,
) -> Result<(), String> {
  if restapi_handle.read().await.is_none() {
    return Err("not started".into());
  }
  let mut restapi_handle = restapi_handle.write().await;
  if let Some(handle) = restapi_handle.take() {
    handle.abort();
  }
  Ok(())
}

#[tauri::command]
pub async fn is_restapi_started(
  restapi_handle: State<'_, SharedJoinShandle<Result<()>>>,
) -> Result<bool, String> {
  Ok(restapi_handle.read().await.is_some())
}