diff --git a/.gitignore b/.gitignore index aee53c804f3..7c4ba406d13 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ typings/ debug.log package-lock.json .vscode/settings.json + +# Quasar output +bundle.json +config.json \ No newline at end of file diff --git a/lib/rust/Cargo.toml b/lib/rust/Cargo.toml index 47b4cf36afb..ab116ba2fde 100644 --- a/lib/rust/Cargo.toml +++ b/lib/rust/Cargo.toml @@ -29,6 +29,11 @@ sysinfo = "0.9" webbrowser = "0.5.1" uuid = { version = "0.7", features = ["v4"] } lazy_static = "1.3.0" +includedir = "0.5.0" +tiny_http = "0.6" + +[build-dependencies] +includedir_codegen = "0.5.0" [features] all-api = [] diff --git a/templates/rust/build.rs b/lib/rust/build.rs similarity index 100% rename from templates/rust/build.rs rename to lib/rust/build.rs diff --git a/lib/rust/src/api/cmd.rs b/lib/rust/src/api/cmd.rs index 4257eca6d28..d6655e2ee1d 100644 --- a/lib/rust/src/api/cmd.rs +++ b/lib/rust/src/api/cmd.rs @@ -54,8 +54,5 @@ pub enum Cmd { once: bool, }, #[cfg(any(feature = "all-api", feature = "emit"))] - Emit { - event: String, - payload: String, - }, + Emit { event: String, payload: String }, } diff --git a/lib/rust/src/config.rs b/lib/rust/src/config.rs new file mode 100644 index 00000000000..84408b62a86 --- /dev/null +++ b/lib/rust/src/config.rs @@ -0,0 +1,74 @@ +#[derive(Deserialize)] +#[serde(tag = "window", rename_all = "camelCase")] +pub struct WindowConfig { + #[serde(default = "default_width")] + pub width: i32, + #[serde(default = "default_height")] + pub height: i32, + #[serde(default = "default_resizable")] + pub resizable: bool, + #[serde(default = "default_title")] + pub title: String, +} + +fn default_width() -> i32 { + 800 +} + +fn default_height() -> i32 { + 600 +} + +fn default_resizable() -> bool { + true +} + +fn default_title() -> String { + "Quasar Tauri App".to_string() +} + +fn default_window() -> WindowConfig { + return WindowConfig { + width: default_width(), + height: default_height(), + resizable: default_resizable(), + title: default_title(), + }; +} + +#[derive(Deserialize)] +#[serde(tag = "embeddedServer", rename_all = "camelCase")] +pub struct EmbeddedServerConfig { + #[serde(default = "default_host")] + pub host: String, + #[serde(default = "default_port")] + pub port: String +} + +fn default_host() -> String { + "http://127.0.0.1".to_string() +} + +fn default_port() -> String { + "random".to_string() +} + +fn default_embedded_server() -> EmbeddedServerConfig { + EmbeddedServerConfig { + host: default_host(), + port: default_port() + } +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Config { + #[serde(default = "default_window")] + pub window: WindowConfig, + #[serde(default = "default_embedded_server")] + pub embedded_server: EmbeddedServerConfig +} + +pub fn get() -> Config { + serde_json::from_str(include_str!("../../../config.json")).unwrap() +} diff --git a/lib/rust/src/lib.rs b/lib/rust/src/lib.rs index 88d960588c9..d2ab284b179 100644 --- a/lib/rust/src/lib.rs +++ b/lib/rust/src/lib.rs @@ -7,8 +7,12 @@ mod macros; #[macro_use] extern crate lazy_static; +extern crate includedir; +extern crate phf; + pub mod api; pub mod command; +pub mod config; pub mod dir; pub mod event; pub mod file; @@ -21,6 +25,7 @@ pub mod salt; pub mod tcp; pub mod updater; pub mod version; +pub mod server; use tauri_ui::WebView; @@ -36,12 +41,6 @@ pub fn spawn () + Send + 'static>(what: F) { }); } -pub fn run_async () + Send + 'static>(what: F) { - POOL.with(|thread| { - thread.execute(move || what()); - }); -} - pub fn execute_promise Result + Send + 'static>( webview: &mut WebView<'_, T>, what: F, diff --git a/templates/rust/src/server.rs b/lib/rust/src/server.rs similarity index 100% rename from templates/rust/src/server.rs rename to lib/rust/src/server.rs diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml index f9aad7c1fe6..96c3c1b0434 100755 --- a/templates/rust/Cargo.toml +++ b/templates/rust/Cargo.toml @@ -5,8 +5,6 @@ description = "A Quasar Tauri App" author = [] license = "" repository = "" -build = "build.rs" -include = ["data"] default-run = "app" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -22,12 +20,9 @@ phf = "0.7.21" includedir = "0.5.0" tauri = { path = "../../tauri/lib/rust" } -[build-dependencies] -includedir_codegen = "0.5.0" - [features] dev = [] # has no explicit dependencies -serverless = [] # has no explicit dependencies +embedded-server = [] # has no explicit dependencies [package.metadata.bundle] identifier = "com.quasar.dev" diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index c1b12cb0bc6..c58d340e507 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -5,11 +5,6 @@ extern crate tauri; extern crate tauri_ui; extern crate serde_json; -#[cfg(not(feature = "dev"))] -extern crate includedir; -#[cfg(not(feature = "dev"))] -extern crate phf; - #[cfg(not(feature = "dev"))] extern crate tiny_http; @@ -17,19 +12,17 @@ extern crate tiny_http; use clap::{App, Arg}; #[cfg(not(feature = "dev"))] -#[cfg(not(feature = "serverless"))] +#[cfg(feature = "embedded-server")] use std::thread; mod cmd; -#[cfg(not(feature = "dev"))] -#[cfg(not(feature = "serverless"))] -mod server; - fn main() { let debug; let content; - let _server_url: String; + let config = tauri::config::get(); + #[cfg(feature = "embedded-server")] + let server_url: String; #[cfg(feature = "updater")] { @@ -67,36 +60,42 @@ fn main() { #[cfg(not(feature = "dev"))] { debug = cfg!(debug_assertions); - #[cfg(feature = "serverless")] + #[cfg(not(feature = "embedded-server"))] { - fn inline_style(s: &str) -> String { - format!(r#""#, s) - } - - fn inline_script(s: &str) -> String { - format!(r#""#, s) - } - let html = format!(r#"{styles}
{scripts}"#, - styles = inline_style(include_str!("../target/compiled-web/css/app.css")), - scripts = inline_script(include_str!("../target/compiled-web/js/app.js")), - ); - content = tauri_ui::Content::Html(html); + content = tauri_ui::Content::Html(include_str!("../target/compiled-web/index.html")); } - #[cfg(not(feature = "serverless"))] + #[cfg(feature = "embedded-server")] { - if let Some(available_port) = tauri::tcp::get_available_port() { - _server_url = format!("{}:{}", "127.0.0.1", available_port); - content = tauri_ui::Content::Url(format!("http://{}", _server_url)); + let port; + let port_valid; + if config.embedded_server.port == "random" { + match tauri::tcp::get_available_port() { + Some(available_port) => { + port = available_port.to_string(); + port_valid = true; + } + None => { + port = "0".to_string(); + port_valid = false; + } + } + } else { + port = config.embedded_server.port; + port_valid = tauri::tcp::port_is_available(port.parse::().expect(&format!("Invalid port {}", port))); + } + if port_valid { + server_url = format!("{}:{}", config.embedded_server.host, port); + content = tauri_ui::Content::Url(server_url.clone()); } else { - panic!("Could not find an open port"); + panic!(format!("Port {} is not valid or not open", port)); } } } let webview = tauri_ui::builder() - .title("MyApp - Serverless") - .size(800, 600) // TODO:Resolution is fixed right now, change this later to be dynamic - .resizable(true) + .title(&config.window.title) + .size(config.window.width, config.window.height) + .resizable(config.window.resizable) .debug(debug) .user_data(()) .invoke_handler(|webview, arg| { @@ -163,16 +162,16 @@ fn main() { #[cfg(not(feature = "dev"))] { - #[cfg(not(feature = "serverless"))] + #[cfg(feature = "embedded-server")] { thread::spawn(move || { - let server = tiny_http::Server::http(_server_url).unwrap(); + let server = tiny_http::Server::http(server_url.clone()).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(server::asset_response(&url)).unwrap(); + request.respond(tauri::server::asset_response(&url)).unwrap(); } }); }