Skip to content

Commit

Permalink
refactor(rust): rename the port field into address and support change…
Browse files Browse the repository at this point in the history
… the address and the port

The changes include:
* Replace the Port field name to Address
* We should support the formats Host:Port, IP:Port and Port (defaulting to 127.0.0.1)
* Adapt the app backend to parse the address correctly.

- Closes: build-trust#5685
  • Loading branch information
0x61nas committed Sep 6, 2023
1 parent 5e896e7 commit 7adb614
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ use crate::app::AppState;
use crate::error::Error;
use crate::invitations::commands::create_service_invitation;

const DEFAULT_HOST: &str = "127.0.0.1";

/// Create a TCP outlet within the default node.
#[tauri::command]
pub async fn tcp_outlet_create(
app: AppHandle<Wry>,
service: String,
port: String,
address: String,
email: String,
) -> Result<(), String> {
let email = if email.is_empty() { None } else { Some(email) };
tcp_outlet_create_impl(app, service, port, email)
tcp_outlet_create_impl(app, service, address, email)
.await
.map_err(|e| {
error!("{:?}", e);
Expand All @@ -30,15 +32,19 @@ pub async fn tcp_outlet_create(
async fn tcp_outlet_create_impl(
app: AppHandle<Wry>,
service: String,
port: String,
address: String,
email: Option<String>,
) -> crate::Result<()> {
debug!(%service, %port, "Creating an outlet");
debug!(%service, %address, "Creating an outlet");
let app_state = app.state::<AppState>();
let socket_addr: SocketAddr = format!("127.0.0.1:{port}")
.parse()
.into_diagnostic()
.wrap_err("Invalid port")?;
let socket_addr: SocketAddr = if let Some((host, port)) = address.split_once(':') {
format!("{host}:{port}")
} else {
format!("{DEFAULT_HOST}:{address}")
}
.parse()
.into_diagnostic()
.wrap_err("Invalid port")?;
let worker_addr = extract_address_value(&service).wrap_err("Invalid service address")?;
let node_manager_worker = app_state.node_manager_worker().await;
let mut node_manager = node_manager_worker.inner().write().await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import { invoke } from "@tauri-apps/api/tauri";
let service = "";
let port = "10000";
let address = "127.0.0.1:10000";
let email = "";
let error_message = "";
async function submit() {
error_message = "";
await invoke("plugin:shared_service|tcp_outlet_create", {
service: service,
port: port,
address: address,
email: email,
})
.then(close)
Expand Down Expand Up @@ -47,15 +47,15 @@
</div>
<div class="flex items-start">
<div class="flex-1">
<div class="font-bold">Port</div>
<p class="text-sm text-gray-500">Choose a port for the service</p>
<div class="font-bold">Address</div>
<p class="text-sm text-gray-500">Choose a address for the service</p>
</div>
<div class="flex-1">
<input
type="text"
class="w-full border-none bg-transparent px-4 text-right text-base focus:outline-none"
placeholder={port}
bind:value={port}
placeholder={address}
bind:value={address}
/>
</div>
</div>
Expand Down

0 comments on commit 7adb614

Please sign in to comment.