Skip to content

Commit

Permalink
Add instance info (#7)
Browse files Browse the repository at this point in the history
* Add instance info

* remove unused code
  • Loading branch information
dzania authored Sep 20, 2023
1 parent b46fa48 commit bae6245
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src-tauri/src/appstate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::sync::Mutex;

use crate::database::DbPool;
use crate::database::{Connection, DbPool};

#[derive(Default)]
pub struct AppState {
pub db: Mutex<Option<DbPool>>,
pub active_connections: Vec<Connection>,
}
impl AppState {
pub fn get_pool(&self) -> DbPool {
Expand Down
35 changes: 31 additions & 4 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
database::{Instance, Location, WireguardKeys},
database::{models::instance::InstanceInfo, Instance, Location, WireguardKeys},
error::Error,
utils::setup_interface,
AppState,
Expand All @@ -9,6 +9,7 @@ use tauri::State;
use wireguard_rs::netlink::delete_interface;

// Create new wireguard interface
#[tauri::command(async)]
pub async fn connect(location_id: i64, app_state: State<'_, AppState>) -> Result<(), Error> {
if let Some(location) = Location::find_by_id(&app_state.get_pool(), location_id).await? {
setup_interface(location, &app_state.get_pool()).await?;
Expand Down Expand Up @@ -105,10 +106,36 @@ pub async fn save_device_config(
}

#[tauri::command(async)]
pub async fn all_instances(app_state: State<'_, AppState>) -> Result<Vec<Instance>, String> {
Instance::all(&app_state.get_pool())
pub async fn all_instances(app_state: State<'_, AppState>) -> Result<Vec<InstanceInfo>, String> {
let instances = Instance::all(&app_state.get_pool())
.await
.map_err(|err| err.to_string())
.map_err(|err| err.to_string())?;
let mut instance_info: Vec<InstanceInfo> = vec![];
for instance in &instances {
let locations = Location::find_by_instance_id(&app_state.get_pool(), instance.id.unwrap())
.await
.map_err(|err| err.to_string())?;
let connection_ids: Vec<i64> = app_state
.active_connections
.iter()
.map(|connection| connection.location_id)
.collect();
let location_ids: Vec<i64> = locations
.iter()
.map(|location| location.id.unwrap())
.collect();
let connected = connection_ids
.iter()
.any(|item1| location_ids.iter().any(|item2| item1 == item2));
instance_info.push(InstanceInfo {
id: instance.id,
uuid: instance.uuid.clone(),
name: instance.name.clone(),
url: instance.url.clone(),
connected,
})
}
Ok(instance_info)
}

#[tauri::command(async)]
Expand Down

0 comments on commit bae6245

Please sign in to comment.