From 010198a44f743530c39330c2d788f0c9cef474f7 Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Wed, 15 Nov 2023 23:02:38 +0800 Subject: [PATCH] add static routers --- {assets => src/template/assets}/favicon.ico | Bin .../template/assets}/js/alpinejs.js | 0 .../template/assets}/js/sweetalert2.js | 0 .../template/assets}/js/tailwindcss.js | 0 src/template/src/routers/mod.hbs | 5 +++ src/template/src/routers/static_routers.hbs | 23 ++++++++++++ src/template/templates/404.hbs | 4 +-- src/template/templates/login.hbs | 6 ++-- src/template/templates/user_list_page.hbs | 6 ++-- src/utils/create_project.rs | 34 +++++++++++++++++- 10 files changed, 69 insertions(+), 9 deletions(-) rename {assets => src/template/assets}/favicon.ico (100%) rename {assets => src/template/assets}/js/alpinejs.js (100%) rename {assets => src/template/assets}/js/sweetalert2.js (100%) rename {assets => src/template/assets}/js/tailwindcss.js (100%) create mode 100644 src/template/src/routers/static_routers.hbs diff --git a/assets/favicon.ico b/src/template/assets/favicon.ico similarity index 100% rename from assets/favicon.ico rename to src/template/assets/favicon.ico diff --git a/assets/js/alpinejs.js b/src/template/assets/js/alpinejs.js similarity index 100% rename from assets/js/alpinejs.js rename to src/template/assets/js/alpinejs.js diff --git a/assets/js/sweetalert2.js b/src/template/assets/js/sweetalert2.js similarity index 100% rename from assets/js/sweetalert2.js rename to src/template/assets/js/sweetalert2.js diff --git a/assets/js/tailwindcss.js b/src/template/assets/js/tailwindcss.js similarity index 100% rename from assets/js/tailwindcss.js rename to src/template/assets/js/tailwindcss.js diff --git a/src/template/src/routers/mod.hbs b/src/template/src/routers/mod.hbs index 39b1a1c..137ef3b 100644 --- a/src/template/src/routers/mod.hbs +++ b/src/template/src/routers/mod.hbs @@ -15,6 +15,7 @@ use self::{ }; pub mod demo; pub mod user; +mod static_routers; pub fn router() -> Router { let mut no_auth_routers = vec![ @@ -33,6 +34,8 @@ pub fn router() -> Router { .delete(delete_user), ), ]; + let static_routers = static_routers::create_static_routers(); + no_auth_routers.extend(static_routers); let router = Router::new() .hoop(Logger::new()) .hoop(CatchPanic::new()) @@ -78,6 +81,8 @@ pub fn router() -> Router { .delete(delete_user), ), ]; + let static_routers = static_routers::create_static_routers(); + no_auth_routers.extend(static_routers); let router = Router::new() .hoop(Logger::new()) .hoop(CatchPanic::new()) diff --git a/src/template/src/routers/static_routers.hbs b/src/template/src/routers/static_routers.hbs new file mode 100644 index 0000000..8800d53 --- /dev/null +++ b/src/template/src/routers/static_routers.hbs @@ -0,0 +1,23 @@ +use rust_embed::RustEmbed; +use salvo::{Router, serve_static::static_embed, endpoint, Response, http::ResBody, hyper::body::Bytes}; + +#[derive(RustEmbed)] +#[folder = "assets"] +struct Assets; + +pub fn create_static_routers() -> Vec { + {{#if need_db_conn}} + let static_router = Router::with_path("assets/<**path>").get(static_embed::()); + let icon_router = Router::with_path("favicon.ico").get(get_icon); + vec![static_router,icon_router] + {{else}} + let icon_router = Router::with_path("favicon.ico").get(get_icon); + vec![icon_router] + {{/if}} +} + +#[endpoint(tags("comm"))] +pub async fn get_icon(res: &mut Response) { + let icon = Assets::get("favicon.ico").unwrap(); + res.body(ResBody::Once(Bytes::from(icon.data.to_vec()))); +} diff --git a/src/template/templates/404.hbs b/src/template/templates/404.hbs index dd97fe6..0164311 100644 --- a/src/template/templates/404.hbs +++ b/src/template/templates/404.hbs @@ -4,8 +4,8 @@ 404 {{page_not_found}} - - + + diff --git a/src/template/templates/login.hbs b/src/template/templates/login.hbs index cfe249d..bc124fa 100644 --- a/src/template/templates/login.hbs +++ b/src/template/templates/login.hbs @@ -80,9 +80,9 @@ - - - + + + - - + + +