Skip to content

Commit

Permalink
add static routers
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Nov 15, 2023
1 parent 03b3180 commit 010198a
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 9 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions src/template/src/routers/mod.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
23 changes: 23 additions & 0 deletions src/template/src/routers/static_routers.hbs
Original file line number Diff line number Diff line change
@@ -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<Router> {
{{#if need_db_conn}}
let static_router = Router::with_path("assets/<**path>").get(static_embed::<Assets>());
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())));
}
4 changes: 2 additions & 2 deletions src/template/templates/404.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>404 {{page_not_found}}</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<script src="assets/js/tailwindcss.js"></script>
</head>
<body
class="grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8"
>
Expand Down
6 changes: 3 additions & 3 deletions src/template/templates/login.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
</template>
</div>
</body>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="//unpkg.com/alpinejs" defer></script>
<script src="assets/js/tailwindcss.js"></script>
<script src="assets/js/sweetalert2.js"></script>
<script src="assets/js/alpinejs.js" defer></script>
<script>
function loginForm() {
return {
Expand Down
6 changes: 3 additions & 3 deletions src/template/templates/user_list_page.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<title>salvo</title>
</head>
{% include "user_list.html" %}
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="//unpkg.com/alpinejs" defer></script>
<script src="assets/js/tailwindcss.js"></script>
<script src="assets/js/sweetalert2.js"></script>
<script src="assets/js/alpinejs.js" defer></script>
<script>
function userForm() {
return {
Expand Down
34 changes: 33 additions & 1 deletion src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn write_project_file(
let mut dependencies = data["dependencies"].clone();
handle_dependencies(
&mut dependencies,
is_web_site,
need_db_conn,
user_selected.db_type,
user_selected.db_conn_type,
Expand All @@ -193,8 +194,28 @@ fn write_project_file(
handlebars.render_template(handle_404_template, &data)?;
let mut handle_404_file = File::create(template_path.join("handle_404.html"))?;
handle_404_file.write_all(handle_404_template_rendered.as_bytes())?;

//assets
let assets_path = project_path.join("assets");
//assets/favicon.ico
let favicon_bytes = include_bytes!("../template/assets/favicon.ico");
let mut favicon_file = File::create(assets_path.join("favicon.ico"))?;
favicon_file.write_all(favicon_bytes)?;
if need_db_conn {
//assets/js
let js_path = assets_path.join("js");
std::fs::create_dir_all(&js_path)?;
//assets/js/alpinejs.js
let alpinejs_bytes = include_bytes!("../template/assets/js/alpinejs.js");
let mut alpinejs_file = File::create(js_path.join("alpinejs.js"))?;
alpinejs_file.write_all(alpinejs_bytes)?;
//assets/js/sweetalert2.js
let sweetalert2_bytes = include_bytes!("../template/assets/js/sweetalert2.js");
let mut sweetalert2_file = File::create(js_path.join("sweetalert2.js"))?;
sweetalert2_file.write_all(sweetalert2_bytes)?;
//assets/js/tailwindcss.js
let tailwindcss_bytes = include_bytes!("../template/assets/js/tailwindcss.js");
let mut tailwindcss_file = File::create(js_path.join("tailwindcss.js"))?;
tailwindcss_file.write_all(tailwindcss_bytes)?;
//template/login.html
let login_template = include_str!("../template/templates/login.hbs");
let login_template_rendered = handlebars.render_template(login_template, &data)?;
Expand Down Expand Up @@ -627,10 +648,21 @@ fn create_basic_file(

fn handle_dependencies(
dependencies: &mut serde_json::Value,
is_website: bool,
need_db_conn: bool,
db_type: DbType,
conn_type: DbConnectionType,
) {
if is_website {
dependencies["rust-embed"] = json!({
"version": "8.0.0",
});
if let Some(salvo) = dependencies["salvo"].as_object_mut() {
if let Some(features) = salvo["features"].as_array_mut() {
features.push(serde_json::json!("serve-static"));
}
}
}
if need_db_conn {
match (conn_type, db_type) {
(DbConnectionType::Sqlx, DbType::Mysql) => {
Expand Down

0 comments on commit 010198a

Please sign in to comment.