Skip to content

Commit ddaa345

Browse files
committed
Include static files as resources in library
1 parent bee934b commit ddaa345

12 files changed

+69
-5
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/target/
22
Cargo.lock
3+
*.swp
4+
*.swo

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ name = "rusty_dashed"
33
version = "0.1.0"
44
authors = ["Hugo Freire <[email protected]>"]
55

6+
build = "build.rs"
7+
include = ["public"]
8+
69
[dependencies]
710
mio = "0.6"
811
staticfile = "*"
912
iron = "*"
1013
mount = "*"
14+
phf = "0.7.12"
15+
includedir = "0.2.0"
16+
17+
[build-dependencies]
18+
includedir_codegen = "0.2.0"

build.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern crate includedir_codegen;
2+
3+
use includedir_codegen::Compression;
4+
5+
fn main(){
6+
includedir_codegen::start("PUBLIC")
7+
.dir("public", Compression::Gzip)
8+
.build("public.rs")
9+
.unwrap();
10+
}

examples/.dashboard.rs.swp

-12 KB
Binary file not shown.

examples/dashboard.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
extern crate rusty_dashed;
2-
extern crate staticfile;
32
extern crate iron;
43
extern crate mount;
4+
extern crate includedir;
5+
extern crate phf;
56

67
use std::path::Path;
7-
use iron::Iron;
8-
use staticfile::Static;
8+
use iron::{Iron, Request, Response, IronResult};
9+
use iron::status;
910
use mount::Mount;
11+
use rusty_dashed::Dashboard;
12+
13+
include!(concat!(env!("OUT_DIR"), "/public.rs"));
14+
15+
fn get_static_file(req: &mut Request) -> IronResult<Response>{
16+
let file = format!("public/{}", req.url.path().join("/"));
17+
let file_content = match file.as_ref() {
18+
"public/" => PUBLIC.get("public/index.html").unwrap(),
19+
_ => PUBLIC.get(&file).unwrap()
20+
};
21+
22+
let content = std::str::from_utf8(&file_content).unwrap();
23+
Ok(Response::with((status::Ok, content)))
24+
}
25+
26+
fn send_rusty_dashed_js(req: &mut Request) -> IronResult<Response>{
27+
let mut dashboard = Dashboard::new();
28+
dashboard.add_graph("test/test1");
29+
dashboard.add_graph("test/test2");
30+
Ok(Response::with((status::Ok, dashboard.get_init_script())))
31+
}
1032

1133
fn main() {
1234
let mut mount = Mount::new();
13-
mount.mount("/", Static::new(Path::new("public/")));
35+
mount.mount("/", get_static_file)
36+
.mount("/js/rusty_dashed.js", send_rusty_dashed_js);
1437
Iron::new(mount).http("0.0.0.0:3000").unwrap();
1538
}

public/.index.html.swp

-20 KB
Binary file not shown.

public/.miserables.json.swp

-12 KB
Binary file not shown.

public/js/.main.js.swp

-12 KB
Binary file not shown.

public/js/rusty_dashed.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//this file is overrided dinamicaly

src/dashboard.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub struct Dashboard {
2+
graphs: Vec<String>
3+
}
4+
5+
impl Dashboard {
6+
pub fn new() -> Dashboard {
7+
Dashboard { graphs: vec![] }
8+
}
9+
10+
pub fn add_graph(&mut self, path: &str){
11+
self.graphs.push(path.to_string());
12+
}
13+
14+
pub fn get_init_script(&self) -> String {
15+
let script_lines = self.graphs.iter().map(|graph| format!("RustyDashed.addGraph('{}');", graph)).collect::<Vec<String>>();
16+
script_lines.join("\n")
17+
}
18+
}

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
pub mod rusty_dashed;
1+
pub use self::dashboard::Dashboard;
2+
3+
mod dashboard;

src/rusty_dashed/mod.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)