From b9cc35ca39e3d3e56a40619362d7fdd78b27a6cb Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 20 Jul 2019 19:53:11 -0300 Subject: [PATCH] chore(merge) feature/serverless (#7) * feat(template) serverless code for JS and CSS loading * fix(ffi) define struct (#5) Per Boscops self-raised, but never merged issue https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs Basically removes potentially undefined behavior --- bindings/rust/proton-sys/lib.rs | 3 +- templates/rust/Cargo.toml | 1 + templates/rust/src/main.rs | 60 +++++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/bindings/rust/proton-sys/lib.rs b/bindings/rust/proton-sys/lib.rs index 559040b84..5e37fbaf0 100644 --- a/bindings/rust/proton-sys/lib.rs +++ b/bindings/rust/proton-sys/lib.rs @@ -5,7 +5,8 @@ extern crate bitflags; use std::os::raw::*; -pub enum CWebView {} // opaque type, only used in ffi pointers +// https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs +#[repr(C)] pub struct CWebView { _private: [u8; 0] } type ErasedExternalInvokeFn = extern "C" fn(webview: *mut CWebView, arg: *const c_char); type ErasedDispatchFn = extern "C" fn(webview: *mut CWebView, arg: *mut c_void); diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml index 7ebd9e98c..8ef1ebcfa 100755 --- a/templates/rust/Cargo.toml +++ b/templates/rust/Cargo.toml @@ -26,6 +26,7 @@ includedir_codegen = "0.5.0" [features] dev = [] # has no explicit dependencies +serverless = [] # 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 71296fa73..919cc75e9 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -22,12 +22,13 @@ 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; #[cfg(not(feature = "dev"))] { @@ -61,25 +62,22 @@ fn main() { content = proton_ui::Content::Url(matches.value_of("url").unwrap().to_owned()); debug = true; } + #[cfg(not(feature = "dev"))] { - if let Some(available_port) = proton::tcp::get_available_port() { - let server_url = format!("{}:{}", "127.0.0.1", available_port); - content = proton_ui::Content::Url(format!("http://{}", server_url)); - debug = cfg!(debug_assertions); - - thread::spawn(move || { - let server = tiny_http::Server::http(server_url).unwrap(); - 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(); - } - }); - } else { - panic!("Could not find an open port"); + debug = cfg!(debug_assertions); + #[cfg(feature = "serverless")] + { + content = proton_ui::Content::Html(include_str!("../target/compiled-web/index.html")); + } + #[cfg(not(feature = "serverless"))] + { + if let Some(available_port) = proton::tcp::get_available_port() { + _server_url = format!("{}:{}", "127.0.0.1", available_port); + content = proton_ui::Content::Url(format!("http://{}", _server_url)); + } else { + panic!("Could not find an open port"); + } } } @@ -113,5 +111,31 @@ fn main() { .build() .unwrap(); + #[cfg(not(feature = "dev"))] + { + #[cfg(feature = "serverless")] + { + let handle = webview.handle(); + handle.dispatch(move |_webview| { + _webview.inject_css(include_str!("../target/compiled-web/css/app.css")).unwrap(); + _webview.eval(include_str!("../target/compiled-web/js/app.js")) + }).unwrap(); + } + + #[cfg(not(feature = "serverless"))] + { + thread::spawn(move || { + let server = tiny_http::Server::http(_server_url).unwrap(); + 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(); + } + }); + } + } + webview.run().unwrap(); }