From db835f42aa6b43f993cafa5aec9c3afebbf96764 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 12 Oct 2017 02:13:55 -0700 Subject: [PATCH] Convert webapp to using included files --- Cargo.toml | 4 ++++ build.rs | 10 ++++++++++ src/main.rs | 4 ++++ src/webapp.rs | 17 ++++++++++++----- 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 240648b..921c6af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,8 @@ serde_json = "1.0.3" rocket = "*" rocket_codegen = "*" rocket_contrib = "*" +phf = "0.7.12" +includedir = "0.2.0" +[build-dependencies] +includedir_codegen = "0.2.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..943ce5c --- /dev/null +++ b/build.rs @@ -0,0 +1,10 @@ +extern crate includedir_codegen; + +use includedir_codegen::Compression; + +fn main() { + includedir_codegen::start("WEBFILES") + .dir("static", Compression::Gzip) + .build("static.rs") + .unwrap(); +} diff --git a/src/main.rs b/src/main.rs index ceafec1..68534ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,8 @@ extern crate serde_derive; extern crate serde_json; extern crate rocket; extern crate rocket_contrib; +extern crate includedir; +extern crate phf; use std::path::Path; use std::fs::File; @@ -30,6 +32,8 @@ use language::{ProgrammingLanguageInterface, EvalOptions, LLVMCodeString}; mod webapp; mod llvm_wrap; +include!(concat!(env!("OUT_DIR"), "/static.rs")); + fn main() { let languages: Vec> = vec![ diff --git a/src/webapp.rs b/src/webapp.rs index f6e850b..e35bac3 100644 --- a/src/webapp.rs +++ b/src/webapp.rs @@ -1,17 +1,24 @@ use rocket; -use rocket::response::NamedFile; +use rocket::response::Content; +use rocket::http::ContentType; use rocket_contrib::Json; use schala_lang; use language::{ProgrammingLanguageInterface, EvalOptions}; +use WEBFILES; + #[get("/")] -fn index() -> Result { - NamedFile::open("static/index.html").map_err(|_| ()) +fn index() -> Content { + let path = "static/index.html"; + let html_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap(); + Content(ContentType::HTML, html_contents) } #[get("/bundle.js")] -fn js_bundle() -> Result { - NamedFile::open("static/bundle.js").map_err(|_| ()) +fn js_bundle() -> Content { + let path = "static/bundle.js"; + let js_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap(); + Content(ContentType::JavaScript, js_contents) } #[derive(Debug, Serialize, Deserialize)]