diff --git a/src/template/config/config.hbs b/src/template/config/config.hbs index 2e9fadc..a1b180d 100644 --- a/src/template/config/config.hbs +++ b/src/template/config/config.hbs @@ -2,6 +2,7 @@ name = "{{project_name}}" address = "0.0.0.0:5800" ssl = false +cors_allow_origin=["https://salvo.rs"] {{#if need_db_conn}} [database] {{#if is_sea_orm_or_sqlx}} diff --git a/src/template/src/config_template.hbs b/src/template/src/config_template.hbs index c9f2626..d8ed2be 100644 --- a/src/template/src/config_template.hbs +++ b/src/template/src/config_template.hbs @@ -17,6 +17,7 @@ pub struct Configs { pub struct Server { pub name: String, pub address: String, + pub cors_allow_origin: Vec, pub ssl: bool, } {{#if need_db_conn}} diff --git a/src/template/src/middleware/cors.rs b/src/template/src/middleware/cors.rs new file mode 100644 index 0000000..a4b2f81 --- /dev/null +++ b/src/template/src/middleware/cors.rs @@ -0,0 +1,11 @@ +use salvo::cors::{AllowHeaders, AllowMethods, Cors, CorsHandler}; +use crate::config::CFG; + +pub fn cors_middleware() -> CorsHandler { + let cors_handler = Cors::new() + .allow_origin(&CFG.server.cors_allow_origin) + .allow_methods(AllowMethods::any()) + .allow_headers(AllowHeaders::any()) + .into_handler(); + cors_handler +} diff --git a/src/template/src/middleware/jwt.rs b/src/template/src/middleware/jwt.rs index cac61b0..7b9a626 100644 --- a/src/template/src/middleware/jwt.rs +++ b/src/template/src/middleware/jwt.rs @@ -14,7 +14,7 @@ pub struct JwtClaims { } #[allow(dead_code)] -pub fn jwt_hoop() -> JwtAuth { +pub fn jwt_middleware() -> JwtAuth { let auth_handler: JwtAuth = JwtAuth::new(ConstDecoder::from_secret( CFG.jwt.jwt_secret.to_owned().as_bytes(), )) diff --git a/src/template/src/middleware/mod.rs b/src/template/src/middleware/mod.rs index 9cc4bdd..bc79a1a 100644 --- a/src/template/src/middleware/mod.rs +++ b/src/template/src/middleware/mod.rs @@ -1,2 +1,3 @@ pub mod handle_404; pub mod jwt; +pub mod cors; \ No newline at end of file diff --git a/src/template/src/routers/mod.hbs b/src/template/src/routers/mod.hbs index e4c218d..08b1772 100644 --- a/src/template/src/routers/mod.hbs +++ b/src/template/src/routers/mod.hbs @@ -1,6 +1,6 @@ {{#if need_db_conn}} {{#if is_web_site}} -use crate::middleware::jwt::jwt_hoop; +use crate::middleware::{cors::cors_middleware, jwt::jwt_middleware}; use salvo::{ prelude::{CatchPanic, Logger, OpenApi, SwaggerUi}, Router, @@ -23,6 +23,8 @@ pub fn router() -> Router { Router::with_path("/api/login").post(post_login), ]; + let _cors_handler = cors_middleware(); + let mut need_auth_routers = vec![ Router::with_path("users") .get(user_list_page), @@ -37,6 +39,7 @@ pub fn router() -> Router { let static_routers = static_routers::create_static_routers(); no_auth_routers.extend(static_routers); let router = Router::new() + //.hoop(_cors_handler) .hoop(Logger::new()) .hoop(CatchPanic::new()) .get(hello) @@ -44,7 +47,7 @@ pub fn router() -> Router { .push( Router::new() .append(&mut need_auth_routers) - .hoop(jwt_hoop()), + .hoop(jwt_middleware()), ); let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router); router @@ -52,7 +55,7 @@ pub fn router() -> Router { .push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui")) } {{else}} -use crate::middleware::jwt::jwt_hoop; +use crate::middleware::{cors::cors_middleware, jwt::jwt_middleware}; use salvo::{ prelude::{CatchPanic, Logger, OpenApi, SwaggerUi}, Router, @@ -66,12 +69,15 @@ use self::{ }; pub mod demo; pub mod user; +mod static_routers; pub fn router() -> Router { let mut no_auth_routers = vec![ Router::with_path("/api/login").post(post_login), ]; + let _cors_handler = cors_middleware(); + let mut need_auth_routers = vec![ Router::with_path("/api/users").get(get_users) .post(post_add_user) @@ -83,6 +89,7 @@ pub fn router() -> Router { ]; let router = Router::new() + //.hoop(_cors_handler) .hoop(Logger::new()) .hoop(CatchPanic::new()) .get(hello) @@ -90,7 +97,7 @@ pub fn router() -> Router { .push( Router::new() .append(&mut need_auth_routers) - .hoop(jwt_hoop()), + .hoop(jwt_middleware()), ); let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router); router @@ -104,13 +111,20 @@ use salvo::{ prelude::{CatchPanic, Logger, OpenApi, SwaggerUi}, Router, }; +use crate::middleware::cors::cors_middleware; use self::demo::hello; pub mod demo; +mod static_routers; + pub fn router() -> Router { + let _cors_handler = cors_middleware(); + let mut static_routers = static_routers::create_static_routers(); let router = Router::new() + //.hoop(_cors_handler) .hoop(Logger::new()) .hoop(CatchPanic::new()) - .get(hello); + .get(hello) + .append(&mut static_routers); let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router); router .push(doc.into_router("/api-doc/openapi.json")) @@ -121,13 +135,20 @@ use salvo::{ prelude::{CatchPanic, Logger, OpenApi, SwaggerUi}, Router, }; +use crate::middleware::cors::cors_middleware; use self::demo::hello; pub mod demo; +mod static_routers; + pub fn router() -> Router { + let _cors_handler = cors_middleware(); + let mut static_routers = static_routers::create_static_routers(); let router = Router::new() + //.hoop(_cors_handler) .hoop(Logger::new()) .hoop(CatchPanic::new()) - .get(hello); + .get(hello) + .append(&mut static_routers); let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router); router .push(doc.into_router("/api-doc/openapi.json")) diff --git a/src/template/src/routers/static_routers.hbs b/src/template/src/routers/static_routers.hbs index 8800d53..b5a0014 100644 --- a/src/template/src/routers/static_routers.hbs +++ b/src/template/src/routers/static_routers.hbs @@ -1,6 +1,9 @@ use rust_embed::RustEmbed; +{{#if need_db_conn}} use salvo::{Router, serve_static::static_embed, endpoint, Response, http::ResBody, hyper::body::Bytes}; - +{{else}} +use salvo::{Router, endpoint, Response, http::ResBody, hyper::body::Bytes}; +{{/if}} #[derive(RustEmbed)] #[folder = "assets"] struct Assets; diff --git a/src/template/templates/404.hbs b/src/template/templates/404.hbs index 0164311..f0bbb37 100644 --- a/src/template/templates/404.hbs +++ b/src/template/templates/404.hbs @@ -4,7 +4,11 @@ 404 {{page_not_found}} +{{#if need_db_conn}} +{{else}} + +{{/if}} {