changeset 23:1f33922b29f2

Use FileServer to serve static assets
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 12 Jul 2022 12:02:47 -0700
parents af328a6c7d9f
children 758a4f6c160f
files assets/index.html.hbs src/main.rs
diffstat 2 files changed, 13 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/assets/index.html.hbs	Mon Jul 11 20:36:58 2022 -0700
+++ b/assets/index.html.hbs	Tue Jul 12 12:02:47 2022 -0700
@@ -4,6 +4,9 @@
         <title>Analyrics</title>
         <script src="static/chart.min.js" type="application/javascript"></script>
 
+        <script type="application/javascript">
+        </script>
+
         <style>
             #logo { color: #22bb22; font-size: 20pt; font-style: bold; }
             #header { color: #bb2222; }
--- a/src/main.rs	Mon Jul 11 20:36:58 2022 -0700
+++ b/src/main.rs	Tue Jul 12 12:02:47 2022 -0700
@@ -4,7 +4,7 @@
 use time::{Duration, OffsetDateTime};
 
 use rocket::form::Form;
-use rocket::fs::NamedFile;
+use rocket::fs::{relative, FileServer, NamedFile};
 use rocket::futures::StreamExt;
 use rocket::http::{Cookie, CookieJar, HeaderMap, Status};
 use rocket::request::{self, FlashMessage, FromRequest, Outcome, Request};
@@ -264,9 +264,7 @@
 }
 
 #[rocket::post("/logout")]
-fn route_logout(
-    cc: &rocket::State<CustomConfig>,
-    cookies: &CookieJar<'_>) -> Flash<Redirect> {
+fn route_logout(cc: &rocket::State<CustomConfig>, cookies: &CookieJar<'_>) -> Flash<Redirect> {
     if let Some(cookie) = cookies.get_private(USER_ID_COOKIE_KEY) {
         cookies.remove_private(cookie);
     }
@@ -298,7 +296,10 @@
         Ok(true) => {
             let c = Cookie::new(USER_ID_COOKIE_KEY, login.username.clone());
             cookies.add_private(c);
-            Flash::success(Redirect::to(cc.deploy_path.clone()), "Successfully logged in.")
+            Flash::success(
+                Redirect::to(cc.deploy_path.clone()),
+                "Successfully logged in.",
+            )
         }
         Ok(false) => Flash::error(
             Redirect::to(format!("{}/login", cc.deploy_path)),
@@ -524,15 +525,15 @@
 fn rocketmain() -> _ {
     env_logger::init();
 
-    rocket::build()
+    let r = rocket::build()
         .attach(ConfigDB::init())
         .attach(LogsDB::init())
         .attach(Template::fairing())
         .attach(rocket::fairing::AdHoc::config::<CustomConfig>())
+        .mount("/static", FileServer::from(relative!("assets/static")))
         .mount(
             "/",
             rocket::routes![
-                route_static,
                 route_index_loggedin,
                 route_index_loggedout,
                 route_logout,
@@ -541,5 +542,6 @@
                 route_log,
                 route_testchart,
             ],
-        )
+        );
+    r
 }