From: Ian Jackson Date: Sun, 17 May 2020 00:21:02 +0000 (+0100) Subject: wip js X-Git-Tag: otter-0.2.0~1617 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=86eb5a95f3042e35748ac47a55a3513de7fad010;p=otter.git wip js --- diff --git a/src/imports.rs b/src/imports.rs index e6dc237e..9950aaab 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -1,4 +1,9 @@ +pub use std::io; + +pub use thiserror::Error; +pub use anyhow::{Context,anyhow}; + pub use serde::Deserialize; pub use serde::Serialize; pub use serde::Serializer; @@ -6,5 +11,9 @@ pub use serde::Serializer; pub use rocket_contrib::helmet::*; pub use rocket_contrib::templates::Template; -pub type E = anyhow::Error; +pub use rocket::State; +pub use rocket::http::{Status,RawStr}; +pub use rocket::request::{FromParam,FromRequest,FromFormValue,LenientForm}; +pub use rocket::response::NamedFile; +pub type E = anyhow::Error; diff --git a/src/main.rs b/src/main.rs index 21d06ccd..2945fcc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ extern crate rocket_contrib; // why do we need this ? extern crate serde; +extern crate thiserror; +extern crate anyhow; mod imports; use imports::*; @@ -20,6 +22,30 @@ fn index() -> Result { Ok(Template::render("test",&c)) } +const RESOURCES : &[&'static str] = &["script.js", "style.css"]; + +#[derive(Debug)] +struct CheckedResourceLeaf { pub safe : &'static str } +#[derive(Error,Debug)] +#[error("not a valid resource path")] +struct UnknownResource{} + +impl<'r> FromParam<'r> for CheckedResourceLeaf { + type Error = UnknownResource; + fn from_param(param: &'r RawStr) -> Result { + for &safe in RESOURCES { + if safe == param.as_str() { return Ok(CheckedResourceLeaf{ safe }) } + } + Err(UnknownResource{}) + } +} + +#[get("/")] +fn resource(leaf : CheckedResourceLeaf) -> io::Result { + let template_dir = "templates"; // xxx + NamedFile::open(format!("{}/{}", template_dir, leaf.safe)) +} + fn main() { let helmet = SpaceHelmet::default() .enable(NoSniff::Enable) @@ -30,7 +56,8 @@ fn main() { .attach(helmet) .attach(Template::fairing()) .mount("/", routes![ - index + index, + resource, ]) .launch(); } diff --git a/templates/script.js b/templates/script.js new file mode 100644 index 00000000..0d76faf0 --- /dev/null +++ b/templates/script.js @@ -0,0 +1,4 @@ +// + +status_node = document.getElementById('spong'); +status_node.innerHTML = 'js-done' diff --git a/templates/test.tera b/templates/test.tera index ab6bdf5b..a7e0fa86 100644 --- a/templates/test.tera +++ b/templates/test.tera @@ -1,4 +1,7 @@

Hi!

+
nothing
+ +