Skip to content

Commit

Permalink
feat(tauri) initial configuration through quasar.conf.js (#16)
Browse files Browse the repository at this point in the history
* feat(proton) configure webview from quasar provided config.json

* feat(config) window config

* feat(config) rename serverless to embeddedServer

* feat(config) embedded server host and port config

* feat(template) q-app inlines assets on output index.html

* feat(proton) move server code to lib

* fix(template) misc fixes
  • Loading branch information
lucasfernog committed Aug 15, 2019
1 parent 824f808 commit 3b272ba
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 51 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ typings/
debug.log
package-lock.json
.vscode/settings.json

# Quasar output
bundle.json
config.json
5 changes: 5 additions & 0 deletions lib/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
File renamed without changes.
5 changes: 1 addition & 4 deletions lib/rust/src/api/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
74 changes: 74 additions & 0 deletions lib/rust/src/config.rs
Original file line number Diff line number Diff line change
@@ -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()
}
11 changes: 5 additions & 6 deletions lib/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +25,7 @@ pub mod salt;
pub mod tcp;
pub mod updater;
pub mod version;
pub mod server;

use tauri_ui::WebView;

Expand All @@ -36,12 +41,6 @@ pub fn spawn<F: FnOnce() -> () + Send + 'static>(what: F) {
});
}

pub fn run_async<F: FnOnce() -> () + Send + 'static>(what: F) {
POOL.with(|thread| {
thread.execute(move || what());
});
}

pub fn execute_promise<T: 'static, F: FnOnce() -> Result<String, String> + Send + 'static>(
webview: &mut WebView<'_, T>,
what: F,
Expand Down
File renamed without changes.
7 changes: 1 addition & 6 deletions templates/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
69 changes: 34 additions & 35 deletions templates/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,24 @@ 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;

#[cfg(feature = "dev")]
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")]
{
Expand Down Expand Up @@ -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#"<style type="text/css">{}</style>"#, s)
}

fn inline_script(s: &str) -> String {
format!(r#"<script type="text/javascript">{}</script>"#, s)
}
let html = format!(r#"<!DOCTYPE html><html><head><meta http-equiv="Content-Security-Policy" content="default-src data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'">{styles}</head><body><div id="q-app"></div>{scripts}</body></html>"#,
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::<u16>().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| {
Expand Down Expand Up @@ -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();
}
});
}
Expand Down

0 comments on commit 3b272ba

Please sign in to comment.