changeset 11:659c0f81f60c

Serve static content and create charts metadata
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 10 Jul 2022 13:17:50 -0700
parents d7b0a2a2e011
children 064a22ca49a7
files Cargo.toml Rocket.toml src/main.rs
diffstat 3 files changed, 53 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Cargo.toml	Sun Jul 10 11:24:06 2022 -0700
+++ b/Cargo.toml	Sun Jul 10 13:17:50 2022 -0700
@@ -10,7 +10,7 @@
 either = "1.7.0"
 env_logger = "0.9.0"
 log = "0.4.17"
-rocket = { version = "0.5.0-rc.2", features = ["secrets"] }
+rocket = { version = "0.5.0-rc.2", features = ["secrets", "json"] }
 rocket_db_pools = { version = "0.1.0-rc.2" }
 rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["handlebars"] }
 sha256 = "1.0.3"
--- a/Rocket.toml	Sun Jul 10 11:24:06 2022 -0700
+++ b/Rocket.toml	Sun Jul 10 13:17:50 2022 -0700
@@ -15,4 +15,4 @@
 port = 8000
 secret_key = "edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb"
 template_dir = "assets"
-asset_path = "assets"
+asset_path = "assets/static"
--- a/src/main.rs	Sun Jul 10 11:24:06 2022 -0700
+++ b/src/main.rs	Sun Jul 10 13:17:50 2022 -0700
@@ -1,16 +1,15 @@
 use anyhow::{self, Error};
 use either::Either;
-use log::{debug, error, info, Level};
+use log::{debug, error, info, warn, Level};
 
 use rocket::form::Form;
 use rocket::fs::NamedFile;
 use rocket::http::{Cookie, CookieJar, HeaderMap, Status};
 use rocket::request::{self, FlashMessage, FromRequest, Outcome, Request};
 use rocket::response::{self, Flash, Redirect, Responder};
+use rocket::serde::json::{json, Value};
 
-use rocket_db_pools::sqlx::{
-    self, Executor, Row, Sqlite, SqlitePool,
-};
+use rocket_db_pools::sqlx::{self, Executor, Row, Sqlite, SqlitePool};
 use rocket_db_pools::{Connection, Database, Pool};
 
 use rocket_dyn_templates::{context, Template};
@@ -343,6 +342,53 @@
     }
 }
 
+#[rocket::get("/static/<path>")]
+async fn route_static(
+    cc: &rocket::State<CustomConfig>,
+    path: &str,
+) -> Either<NamedFile, (Status, String)> {
+    match NamedFile::open(Path::new(&cc.asset_path).join(path)).await {
+        Ok(f) => Either::Left(f),
+        Err(e) => {
+            warn!("Static file not found: {}", path);
+            Either::Right((
+                Status::NotFound,
+                format!("Error loading file at {}: {}", path, e),
+            ))
+        }
+    }
+}
+
+fn create_linechart<
+    S1: std::string::ToString,
+    I1: Iterator<Item = S1>,
+    S2: std::string::ToString,
+    I2: Iterator<Item = S2>,
+>(
+    labels: I1,
+    values: I2,
+    name: String,
+) -> Value {
+    let labels = labels.map(|s| s.to_string()).collect::<Vec<String>>();
+    let values = values.map(|s| s.to_string()).collect::<Vec<String>>();
+
+    let inner = json!({
+        "type": "line",
+        "data": {
+            "labels": labels,
+            "datasets": [{
+                "label": name,
+                "data": values,
+                "borderColor": "red",
+            }],
+        },
+        "options": {
+            "scales": { "y": { "beginAtZero": true }},
+        },
+    });
+    inner
+}
+
 #[derive(rocket::serde::Deserialize)]
 #[serde(crate = "rocket::serde")]
 struct CustomConfig {
@@ -361,6 +407,7 @@
         .mount(
             "/",
             rocket::routes![
+                route_static,
                 route_index_loggedin,
                 route_index_loggedout,
                 route_logout,