Skip to content

Commit

Permalink
move create_init_url to asset_server mod
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Dec 10, 2024
1 parent fe26e6b commit 9e7202b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
33 changes: 32 additions & 1 deletion packages/desktop/src/asset_server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{io::Cursor, path::PathBuf, sync::Arc};
use std::{
io::Cursor,
path::{Path, PathBuf},
sync::Arc,
};

use rocket::{
fs::NamedFile,
Expand Down Expand Up @@ -30,6 +34,33 @@ pub fn setup_asset_server(
});
}

pub async fn create_init_url(
widget_factory: &WidgetFactory,
parent_dir: &Path,
html_path: &Path,
) -> anyhow::Result<tauri::Url> {
let url = tauri::Url::parse_with_params(
"http://127.0.0.1:6124/__zebar/init",
&[
(
"token",
// Generate a unique token to identify requests from the
// widget to the asset server.
&widget_factory.upsert_or_get_token(parent_dir).await,
),
(
"redirect",
&format!(
"/{}",
html_path.strip_prefix(parent_dir)?.to_unicode_string()
),
),
],
)?;

Ok(url)
}

#[get("/__zebar/init?<token>&<redirect>")]
pub fn init(
token: String,
Expand Down
28 changes: 6 additions & 22 deletions packages/desktop/src/widget_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use base64::prelude::*;
use serde::Serialize;
use tauri::{
path::BaseDirectory, AppHandle, Manager, PhysicalPosition, PhysicalSize,
Url, WebviewUrl, WebviewWindowBuilder, WindowEvent,
WebviewUrl, WebviewWindowBuilder, WindowEvent,
};
use tokio::{
sync::{broadcast, Mutex},
Expand All @@ -26,6 +26,7 @@ use crate::common::macos::WindowExtMacOs;
#[cfg(target_os = "windows")]
use crate::common::windows::{remove_app_bar, WindowExtWindows};
use crate::{
asset_server::create_init_url,
common::PathExt,
config::{
AnchorPoint, Config, DockConfig, DockEdge, WidgetConfig,
Expand Down Expand Up @@ -257,26 +258,9 @@ impl WidgetFactory {
)
}

let url = Url::parse_with_params(
"http://127.0.0.1:6124/__zebar/init",
&[
(
"token",
// Generate a unique token to identify requests from the
// widget to the asset server.
&self.upsert_or_get_token(parent_dir).await,
),
(
"redirect",
&format!(
"/{}",
html_path.strip_prefix(parent_dir)?.to_unicode_string()
),
),
],
)?;

let webview_url = WebviewUrl::External(url);
let webview_url = WebviewUrl::External(
create_init_url(self, &parent_dir, &html_path).await?,
);

let mut state = WidgetState {
id: widget_id.clone(),
Expand Down Expand Up @@ -832,7 +816,7 @@ impl WidgetFactory {
///
/// If the directory does not have an existing token, a new one is
/// generated and inserted.
async fn upsert_or_get_token(&self, directory: &Path) -> String {
pub async fn upsert_or_get_token(&self, directory: &Path) -> String {
let mut asset_server_tokens = self.asset_server_tokens.lock().await;

// Find existing token for this path.
Expand Down

0 comments on commit 9e7202b

Please sign in to comment.