diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-10-29 18:23:04 -0700 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-10-29 18:23:04 -0700 |
commit | a9c1b23705fe609bb88adff2103ad5849c9b3f1f (patch) | |
tree | db6549f5a5bb6915ad4fd457cfc2b442ce59e07a /src/bin | |
parent | bb907287a9f923615b9f52dbf3a37d7fea20f58e (diff) | |
download | modelthing-a9c1b23705fe609bb88adff2103ad5849c9b3f1f.tar.gz modelthing-a9c1b23705fe609bb88adff2103ad5849c9b3f1f.zip |
show example models in webface
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/mt-webface.rs | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/src/bin/mt-webface.rs b/src/bin/mt-webface.rs index 6809f57..a11a038 100644 --- a/src/bin/mt-webface.rs +++ b/src/bin/mt-webface.rs @@ -1,15 +1,19 @@ extern crate getopts; extern crate pencil; -#[macro_use] extern crate log; +extern crate modelthing; extern crate env_logger; +#[macro_use] +extern crate log; + use std::env; use std::collections::BTreeMap; use std::process::exit; +use std::path::Path; use getopts::Options; use pencil::Pencil; -use pencil::{Request, PencilResult, Response, HTTPError}; +use pencil::{Request, PencilResult, Response, HTTPError, PencilError}; fn home(r: &mut Request) -> PencilResult { @@ -24,20 +28,35 @@ fn readme(r: &mut Request) -> PencilResult { return r.app.render_template("raw.html", &context); } -fn model_list(_: &mut Request) -> PencilResult { - Ok(Response::from("List of Models")) +fn model_list(r: &mut Request) -> PencilResult { + let paths = modelthing::search_models(Path::new("examples")); + let l: Vec<String> = paths.iter().map(|x| Path::new(x).strip_prefix("examples").unwrap().to_string_lossy().to_string()).collect(); + let mut context = BTreeMap::new(); + context.insert("model_slug_list".to_string(), l); + return r.app.render_template("model_list.html", &context); } fn model_view(r: &mut Request) -> PencilResult { - let model_id = r.view_args.get("model_id").unwrap(); - let mut context = BTreeMap::new(); - context.insert("model_id".to_string(), model_id.to_string()); - return r.app.render_template("model_view.html", &context); + let model_slug = r.view_args.get("model_slug").unwrap(); + let model_path = Path::new("examples").join(model_slug); + match modelthing::load_model_entry(model_path.as_path()) { + Ok(me) => { + let mut context = BTreeMap::new(); + context.insert("model_slug".to_string(), model_slug.to_string()); + context.insert("model_name".to_string(), me.metadata.name_en); + context.insert("model_description".to_string(), me.metadata.description_en.unwrap_or("".to_string())); + //context.insert("metadata".to_string(), me.metadata); + context.insert("markdown".to_string(), me.markdown); + context.insert("modelica".to_string(), format!("{:?}", me.ast)); + r.app.render_template("model_view.html", &context) + }, + Err(_) => Err(PencilError::PenHTTPError(pencil::HTTPError::NotFound)), + } } fn page_not_found(_: HTTPError) -> PencilResult { let mut response = Response::from("404: Not Found"); - response.status_code = 500; + response.status_code = 404; Ok(response) } @@ -97,9 +116,10 @@ fn main() { app.register_template("raw.html"); app.get("/", "home", home); app.get("/readme/", "readme", readme); + app.register_template("model_list.html"); app.get("/model_list/", "model_list", model_list); app.register_template("model_view.html"); - app.get("/model/<model_id:string>", "model", model_view); + app.get("/model/<model_slug:string>/", "model", model_view); let bind = matches.opt_str("bind").unwrap_or("127.0.0.1:5000".to_string()); let bind_str: &str = &bind; |