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
This commit is contained in:
Lucas Fernandes Nogueira 2019-07-20 19:53:11 -03:00 committed by GitHub
parent 19c5d410de
commit b9cc35ca39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 19 deletions

View File

@ -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);

View File

@ -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"

View File

@ -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();
}