Skip to content

Commit

Permalink
fix(tauri) embedded-server edge case with config host (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored and nothingismagick committed Aug 22, 2019
1 parent c031261 commit 9e4a8c6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
17 changes: 10 additions & 7 deletions lib/rust/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tauri_ui::WebView;
//type FnMut(&mut InvokeHandler<WebView<'_, ()>>, &str) = FnMut(&mut FnMut(&mut InvokeHandler<WebView<'_, ()>>, &str)<WebView<'_, ()>>, &str);

pub struct App {
invoke_handler: Option<Box<dyn FnMut(&mut WebView<'_, ()>, &str)>>
invoke_handler: Option<Box<dyn FnMut(&mut WebView<'_, ()>, &str)>>,
}

impl App {
Expand All @@ -14,33 +14,36 @@ impl App {

pub fn run_invoke_handler(&mut self, webview: &mut WebView<'_, ()>, arg: &str) {
match self.invoke_handler {
Some(ref mut invoke_handler) => {
Some(ref mut invoke_handler) => {
invoke_handler(webview, arg);
},
}
None => {}
}
}
}

pub struct AppBuilder {
invoke_handler: Option<Box<dyn FnMut(&mut WebView<'_, ()>, &str)>>
invoke_handler: Option<Box<dyn FnMut(&mut WebView<'_, ()>, &str)>>,
}

impl AppBuilder {
pub fn new() -> Self {
Self {
invoke_handler: None
invoke_handler: None,
}
}

pub fn invoke_handler<F: FnMut(&mut WebView<'_, ()>, &str) + 'static>(mut self, invoke_handler: F) -> Self {
pub fn invoke_handler<F: FnMut(&mut WebView<'_, ()>, &str) + 'static>(
mut self,
invoke_handler: F,
) -> Self {
self.invoke_handler = Some(Box::new(invoke_handler));
self
}

pub fn build(self) -> App {
App {
invoke_handler: self.invoke_handler
invoke_handler: self.invoke_handler,
}
}
}
31 changes: 25 additions & 6 deletions lib/rust/src/app/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) fn run(application: &mut crate::App) {
let content;
let config = crate::config::get();
#[cfg(feature = "embedded-server")]
let server_url: String;
let mut server_url: String;

#[cfg(feature = "updater")]
{
Expand Down Expand Up @@ -50,7 +50,8 @@ pub(crate) fn run(application: &mut crate::App) {
debug = cfg!(debug_assertions);
#[cfg(not(feature = "embedded-server"))]
{
content = tauri_ui::Content::Html(include_str!(concat!(env!("TAURI_DIST_DIR"), "/index.html")));
content =
tauri_ui::Content::Html(include_str!(concat!(env!("TAURI_DIST_DIR"), "/index.html")));
}
#[cfg(feature = "embedded-server")]
{
Expand All @@ -69,10 +70,17 @@ pub(crate) fn run(application: &mut crate::App) {
}
} else {
port = config.embedded_server.port;
port_valid = crate::tcp::port_is_available(port.parse::<u16>().expect(&format!("Invalid port {}", port)));
port_valid = crate::tcp::port_is_available(
port
.parse::<u16>()
.expect(&format!("Invalid port {}", port)),
);
}
if port_valid {
server_url = format!("{}:{}", config.embedded_server.host, port);
if !server_url.starts_with("http") {
server_url = format!("http://{}", server_url);
}
content = tauri_ui::Content::Url(server_url.clone());
} else {
panic!(format!("Port {} is not valid or not open", port));
Expand Down Expand Up @@ -141,17 +149,28 @@ pub(crate) fn run(application: &mut crate::App) {
#[cfg(feature = "embedded-server")]
{
thread::spawn(move || {
let server = tiny_http::Server::http(server_url.clone()).expect(&format!("Could not start embedded server with the specified url: {}", server_url));
let server = tiny_http::Server::http(
server_url
.clone()
.replace("http://", "")
.replace("https://", ""),
)
.expect(&format!(
"Could not start embedded server with the specified url: {}",
server_url
));
for request in server.incoming_requests() {
let mut url = request.url().to_string();
if url == "/" {
url = "/index.html".to_string();
}
request.respond(crate::server::asset_response(&url)).unwrap();
request
.respond(crate::server::asset_response(&url))
.unwrap();
}
});
}
}

webview.run().unwrap();
}
}
6 changes: 3 additions & 3 deletions lib/rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct EmbeddedServerConfig {
#[serde(default = "default_host")]
pub host: String,
#[serde(default = "default_port")]
pub port: String
pub port: String,
}

fn default_host() -> String {
Expand All @@ -56,7 +56,7 @@ fn default_port() -> String {
fn default_embedded_server() -> EmbeddedServerConfig {
EmbeddedServerConfig {
host: default_host(),
port: default_port()
port: default_port(),
}
}

Expand All @@ -66,7 +66,7 @@ pub struct Config {
#[serde(default = "default_window")]
pub window: WindowConfig,
#[serde(default = "default_embedded_server")]
pub embedded_server: EmbeddedServerConfig
pub embedded_server: EmbeddedServerConfig,
}

pub fn get() -> Config {
Expand Down
2 changes: 1 addition & 1 deletion lib/rust/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tauri_ui::Handle;
use std::boxed::Box;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tauri_ui::Handle;

struct EventHandler {
on_event: Box<dyn FnOnce(String)>,
Expand Down
6 changes: 3 additions & 3 deletions lib/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate includedir;
extern crate phf;

pub mod api;
mod app;
pub mod command;
pub mod config;
pub mod dir;
Expand All @@ -22,12 +23,11 @@ pub mod platform;
pub mod process;
pub mod rpc;
pub mod salt;
#[cfg(feature = "embedded-server")]
pub mod server;
pub mod tcp;
pub mod updater;
pub mod version;
#[cfg(feature = "embedded-server")]
pub mod server;
mod app;
pub use app::*;

use tauri_ui::WebView;
Expand Down
2 changes: 1 addition & 1 deletion lib/rust/src/salt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tauri_ui::WebView;
use std::sync::Mutex;
use tauri_ui::WebView;
use uuid::Uuid;

struct Salt {
Expand Down

0 comments on commit 9e4a8c6

Please sign in to comment.