diff --git a/Makefile b/Makefile index 0148920..b01683a 100644 --- a/Makefile +++ b/Makefile @@ -31,19 +31,19 @@ help: .PHONY: build run test bench doc examples graphs clean build: - $(CARGO) build + cargo build run: - $(CARGO) run + cargo run clean: - $(CARGO) clean + cargo clean doc: - $(CARGO) doc + cargo doc test: - $(CARGO) test + cargo test graphs-png: $(GRAPH_IMG) design/%.png: design/%.gv diff --git a/src/handlers.rs b/src/handlers.rs index 81486e2..3df3536 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,11 +1,57 @@ -use handlebars_iron as hbi; - +use handlebars::{Handlebars, no_escape}; +use handlebars_iron::{DirectorySource, HandlebarsEngine}; +use handlebars_iron::handlebars::to_json; +use handlebars_iron::Template; use iron::prelude::{Request, Response}; use iron::{IronResult, Set, status}; use mount::Mount; use serde::ser::Serialize as ToJson; +use serde_json::value::Map; use staticfile::Static; +use std::error::Error; use std::path::Path; +use std::sync::Arc; + +pub fn template_engine() -> Arc { + let views_ext = ".hbs"; + let views_path = "./src/views"; + + let mut hbs = Handlebars::new(); + + // @TODO Find better solution for rendering included template file + hbs.register_escape_fn(no_escape); + + + let mut hbse = HandlebarsEngine::from(hbs); + hbse.add(Box::new( + DirectorySource::new(views_path, views_ext) + )); + + if let Err(r) = hbse.reload() { + panic!("{:?}", r.description()); + } + + let hbse_ref = Arc::new(hbse); + if cfg!(debug_assertions) { + println!("Templates are being watched."); + use handlebars_iron::Watchable; + hbse_ref.watch(views_path); + } + + hbse_ref +} + +pub fn render_page(name: &str, template_data: T) -> Template { + let template_engine = template_engine().clone(); + let template_registry = template_engine.handlebars_mut(); + + let page = template_registry.render(name, &template_data).unwrap(); + + let mut data = Map::new(); + data.insert("page".to_string(), to_json(page)); + + Template::new("layout/base", data) +} pub struct AssetsHandler; @@ -18,15 +64,14 @@ impl AssetsHandler { } } -fn render_page(name: &str, value: T) -> hbi::Template { - unimplemented!(); -} - pub fn hello_world (_request: &mut Request) -> IronResult { - use handlebars_iron::Template; + use serde_json::value::{Map, Value}; + + let mut data = Map::new(); + data.insert("title".to_string(), to_json(&"Media Collection CRUD".to_owned())); let mut response = Response::new(); - response.set_mut(Template::new("index","".to_string())) + response.set_mut(render_page("index", data)) .set_mut(status::Ok); Ok(response) diff --git a/src/lib.rs b/src/lib.rs index 4c0b7f0..7fe987a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,5 +2,5 @@ pub mod chain; pub mod db; pub mod handlers; pub mod models; +// pub mod schema; -// pub mod schema; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0f38205..a042743 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,46 +2,9 @@ #[macro_use] extern crate mime; use env_logger; +use iron::prelude::Iron; -use handlebars as hbs; -use handlebars_iron as hbi; - -use hbs::Handlebars; -use hbi::{DirectorySource, HandlebarsEngine}; - -use iron::prelude::{Chain, Iron}; -use std::error::Error; -use std::sync::Arc; - -use media_collection_crud::{chain, db}; - -fn init_templating() -> Chain { - let views_ext = ".hbs"; - let views_path = "./src/views"; - - let hbs = Handlebars::new(); - let mut hbse = HandlebarsEngine::from(hbs); - // TODO: Investigate serving the templates out of the binary using include_string! - hbse.add(Box::new( - DirectorySource::new(views_path, views_ext) - )); - - if let Err(r) = hbse.reload() { - panic!("{:?}", r.description()); - } - - let hbse_ref = Arc::new(hbse); - if cfg!(debug_assertions) { - println!("Templates are being watched."); - use hbi::Watchable; - hbse_ref.watch(views_path); - } - - let mut chain = chain::init(); - chain.link_after(hbse_ref); - - chain -} +use media_collection_crud::{chain, handlers, db}; fn main() { env_logger::init(); @@ -50,7 +13,11 @@ fn main() { let port = 8000; let bind_addr = format!("localhost:{}", port); - let _server_guard = Iron::new(init_templating()) + let mut chain = chain::init(); + let templating_engine = handlers::template_engine().clone(); + chain.link_after(templating_engine); + + let _server_guard = Iron::new(chain) .http(bind_addr.as_str()) .unwrap(); } \ No newline at end of file diff --git a/src/models.rs b/src/models.rs index 8bc5196..7bf6b2e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -2,7 +2,7 @@ use diesel::Queryable; // use crate::schema::*; -#[derive(Queryable)] +#[derive(Debug, Queryable)] // #[table_name="media"] pub struct Media { pub id: u32, @@ -10,7 +10,7 @@ pub struct Media { pub notes: String, } -#[derive(Queryable)] +#[derive(Debug, Queryable)] // #[table_name="media_type"] pub struct MediaType { pub id: u32, @@ -18,7 +18,7 @@ pub struct MediaType { pub description: String, } -#[derive(Queryable)] +#[derive(Debug, Queryable)] // #[table_name="media_format"] pub struct MediaFormat { pub id: u32, @@ -27,7 +27,7 @@ pub struct MediaFormat { pub description: String, } -#[derive(Queryable)] +#[derive(Debug, Queryable)] pub struct MediaTypeFormatLink { pub media_type_id: i32, pub media_format_id: i32, diff --git a/src/views/footer.hbs b/src/views/footer.hbs new file mode 100644 index 0000000..d5cf9e8 --- /dev/null +++ b/src/views/footer.hbs @@ -0,0 +1,3 @@ +
+

© Timothy J. Warren

+
\ No newline at end of file diff --git a/src/views/header.hbs b/src/views/header.hbs new file mode 100644 index 0000000..7876987 --- /dev/null +++ b/src/views/header.hbs @@ -0,0 +1,3 @@ +
+

Media Collection CRUD

+
\ No newline at end of file diff --git a/src/views/index.hbs b/src/views/index.hbs index 45a45cb..2323256 100644 --- a/src/views/index.hbs +++ b/src/views/index.hbs @@ -1,13 +1,4 @@ - - - - - Rust web server - - - -
-

Hello, world!

-
- - \ No newline at end of file +
+

Index Page

+

Testing rendering of internal content.

+
\ No newline at end of file diff --git a/src/views/layout/base.hbs b/src/views/layout/base.hbs index e80032f..a80aef0 100644 --- a/src/views/layout/base.hbs +++ b/src/views/layout/base.hbs @@ -1,15 +1,5 @@ - - - - - {{#title}}Rust web server{{/title}} - - - +{{> layout/top}} {{> header}} - {{#block "content"}} - Some default content - {{/block}} + {{~ page}} {{> footer}} - - \ No newline at end of file +{{> layout/bottom}} \ No newline at end of file diff --git a/src/views/layout/bottom.hbs b/src/views/layout/bottom.hbs new file mode 100644 index 0000000..691287b --- /dev/null +++ b/src/views/layout/bottom.hbs @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/views/layout/top.hbs b/src/views/layout/top.hbs new file mode 100644 index 0000000..cd3dd75 --- /dev/null +++ b/src/views/layout/top.hbs @@ -0,0 +1,8 @@ + + + + + {{title}} + + + \ No newline at end of file