Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better website #3

Merged
merged 6 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/template/src/dtos/user.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use salvo::prelude::ToSchema;
use salvo::prelude::{ToSchema, Extractible};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Debug, ToSchema, Default)]
Expand All @@ -13,17 +13,15 @@ pub struct UserLoginRequest {
pub password: String,
}

#[derive(Debug, Deserialize, ToSchema, Default)]
#[derive(Debug, Deserialize,Extractible,ToSchema, Default)]
#[salvo(extract(default_source(from = "body", format = "json")))]
pub struct UserUpdateRequest {
#[salvo(extract(source(from = "param")))]
pub id: String,
pub username: String,
pub password: String,
}

#[derive(Debug, Deserialize, ToSchema, Default)]
pub struct UserDeleteRequest {
pub id: String,
}

#[derive(Debug, Serialize, ToSchema, Default)]
pub struct UserResponse {
Expand Down
10 changes: 10 additions & 0 deletions src/template/src/main_template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ async fn main() {
"swagger-ui: https://{}/swagger-ui",
&CFG.server.address.replace("0.0.0.0", "127.0.0.1")
);
{{#if is_web_site}}
{{#if need_db_conn}}
println!("login page: https://{}/login", &CFG.server.address.replace("0.0.0.0", "127.0.0.1"));
{{/if}}
{{/if}}
let config = RustlsConfig::new(
Keycert::new()
.cert(CERT_KEY.cert.clone())
Expand All @@ -68,6 +73,11 @@ async fn main() {
"swagger-ui: http://{}/swagger-ui",
&CFG.server.address.replace("0.0.0.0", "127.0.0.1")
);
{{#if is_web_site}}
{{#if need_db_conn}}
println!("login page: http://{}/login", &CFG.server.address.replace("0.0.0.0", "127.0.0.1"));
{{/if}}
{{/if}}
let acceptor = TcpListener::new(&CFG.server.address).bind().await;
let server = Server::new(acceptor).serve_with_graceful_shutdown(
service,
Expand Down
15 changes: 13 additions & 2 deletions src/template/src/middleware/jwt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use jsonwebtoken::EncodingKey;
use jsonwebtoken::{decode, Algorithm, DecodingKey, EncodingKey, Validation};
use salvo::jwt_auth::{ConstDecoder, CookieFinder, HeaderFinder, QueryFinder};
use salvo::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -35,10 +35,21 @@ pub fn get_token(username: String, user_id: String) -> Result<(String, i64)> {
user_id,
exp: exp.unix_timestamp(),
};
let token = jsonwebtoken::encode(
let token: String = jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
&claim,
&EncodingKey::from_secret(CFG.jwt.jwt_secret.as_bytes()),
)?;
Ok((token, exp.unix_timestamp()))
}

#[allow(dead_code)]
pub fn decode_token(token: &str) -> bool {
let validation = Validation::new(Algorithm::HS256);
decode::<JwtClaims>(
token,
&DecodingKey::from_secret(CFG.jwt.jwt_secret.as_bytes()),
&validation,
)
.is_ok()
}
132 changes: 102 additions & 30 deletions src/template/src/routers/mod.hbs
Original file line number Diff line number Diff line change
@@ -1,61 +1,133 @@
{{#if need_db_conn}}
{{#if is_web_site}}
use crate::middleware::jwt::jwt_hoop;
{{/if}}
use salvo::{
prelude::{CatchPanic, Logger, OpenApi, SwaggerUi},
Router,
};

use self::{
demo::hello,
{{#if need_db_conn}}
{{#if is_web_site}}
user::{
delete_user,login_page, post_add_user, post_login, post_update_user,
delete_user, get_users, login_page, post_add_user, post_login, put_update_user,
user_list_page,
},
{{else}}
};
pub mod demo;
pub mod user;

pub fn router() -> Router {
let mut no_auth_routers = vec![
Router::with_path("login").get(login_page),
Router::with_path("/api/login").post(post_login),
];

let mut need_auth_routers = vec![
Router::with_path("users")
.get(user_list_page),
Router::with_path("/api/users").get(get_users)
.post(post_add_user)
.push(
Router::with_path("<id>")
.put(put_update_user)
.delete(delete_user),
),
];
let router = Router::new()
.hoop(Logger::new())
.hoop(CatchPanic::new())
.get(hello)
.append(&mut no_auth_routers)
.push(
Router::new()
.append(&mut need_auth_routers)
.hoop(jwt_hoop()),
);
let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router);
router
.push(doc.into_router("/api-doc/openapi.json"))
.push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui"))
}
{{else}}
use crate::middleware::jwt::jwt_hoop;
use salvo::{
prelude::{CatchPanic, Logger, OpenApi, SwaggerUi},
Router,
};

use self::{
demo::hello,
user::{
delete_user, get_users, post_add_user, post_login, post_update_user,
delete_user, get_users, post_add_user, post_login, put_update_user,
},
{{/if}}
{{/if}}
};
pub mod demo;
{{#if need_db_conn}}
pub mod user;
{{/if}}

pub fn router() -> Router {
let mut no_auth_routers = vec![
Router::with_path("/api/login").post(post_login),
];

let mut need_auth_routers = vec![
Router::with_path("/api/users").get(get_users)
.post(post_add_user)
.push(
Router::with_path("<id>")
.put(put_update_user)
.delete(delete_user),
),
];
let router = Router::new()
.hoop(Logger::new())
.hoop(CatchPanic::new())
{{#if need_db_conn}}
.get(hello)
{{#if is_web_site}}
.push(Router::with_path("login").get(login_page).post(post_login))
{{else}}
.push(Router::with_path("login").post(post_login))
{{/if}}
.push(user_router().hoop(jwt_hoop()));
{{else}}
.append(&mut no_auth_routers)
.push(
Router::new()
.append(&mut need_auth_routers)
.hoop(jwt_hoop()),
);
let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router);
router
.push(doc.into_router("/api-doc/openapi.json"))
.push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui"))
}
{{/if}}
{{else}}
{{#if is_web_site}}
use salvo::{
prelude::{CatchPanic, Logger, OpenApi, SwaggerUi},
Router,
};
use self::demo::hello;
pub mod demo;
pub fn router() -> Router {
let router = Router::new()
.hoop(Logger::new())
.hoop(CatchPanic::new())
.get(hello);
{{/if}}
let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router);
router
.push(doc.into_router("/api-doc/openapi.json"))
.push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui"))
}
{{#if need_db_conn}}
pub fn user_router() -> Router {
Router::with_path("user")
.post(post_add_user)
.put(post_update_user)
.delete(delete_user)
{{#if is_web_site}}
.get(user_list_page)
{{else}}
.get(get_users)
{{/if}}
{{else}}
use salvo::{
prelude::{CatchPanic, Logger, OpenApi, SwaggerUi},
Router,
};
use self::demo::hello;
pub mod demo;
pub fn router() -> Router {
let router = Router::new()
.hoop(Logger::new())
.hoop(CatchPanic::new())
.get(hello);
let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router);
router
.push(doc.into_router("/api-doc/openapi.json"))
.push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui"))
}
{{/if}}
{{/if}}
Loading