chiark / gitweb /
wip js
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 17 May 2020 00:21:02 +0000 (01:21 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 17 May 2020 00:21:02 +0000 (01:21 +0100)
src/imports.rs
src/main.rs
templates/script.js [new file with mode: 0644]
templates/test.tera

index e6dc237edede0dab6f4cdc38325259324a78a9bb..9950aaab502eb91e171be42b53b3fa40b3b1a390 100644 (file)
@@ -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;
index 21d06ccdfb1a9ec5fef88dbd5c50a3720ceffe1a..2945fcc6f8b3a98cbea6163e60abbfb9979798cf 100644 (file)
@@ -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<Template,RE> {
   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<Self, Self::Error> {
+    for &safe in RESOURCES {
+      if safe == param.as_str() { return Ok(CheckedResourceLeaf{ safe }) }
+    }
+    Err(UnknownResource{})
+  }
+}
+
+#[get("/<leaf>")]
+fn resource(leaf : CheckedResourceLeaf) -> io::Result<NamedFile> {
+  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 (file)
index 0000000..0d76faf
--- /dev/null
@@ -0,0 +1,4 @@
+//
+
+status_node = document.getElementById('spong');
+status_node.innerHTML = 'js-done'
index ab6bdf5bfc41e6603ab8f660f58359da336dad15..a7e0fa869d1074ae416e38294d415b309a7fb478 100644 (file)
@@ -1,4 +1,7 @@
 <body>
 <h1>Hi!</h1>
 
+<div id="spong">nothing</div>
+
 </body>
+<script src="script.js"></script>