添加宏路由

This commit is contained in:
李运家 2024-10-03 09:03:30 +08:00
parent 1c12563baa
commit f8a25bae5e
8 changed files with 40 additions and 68 deletions

View File

@ -17,6 +17,7 @@ pub struct Server {
pub name: String, pub name: String,
pub prefix_url: String, pub prefix_url: String,
pub port: u32, pub port: u32,
pub debug: bool,
} }
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]

View File

@ -27,9 +27,7 @@ where
type State = S; type State = S;
fn typed_route(self, handler: TypedHandler<Self::State>) -> Self { fn typed_route(self, handler: TypedHandler<Self::State>) -> Self {
tracing::info!("start add router");
let (path, method_router) = handler(); let (path, method_router) = handler();
tracing::info!("adding typed route: {} - {:?}", path, method_router);
self.route(path, method_router) self.route(path, method_router)
} }
} }

View File

@ -5,7 +5,7 @@ extern crate syn;
extern crate proc_macro; extern crate proc_macro;
use proc_macro::TokenStream; // 用于生成代码 use proc_macro::TokenStream; // 用于生成代码
use syn::{parse_macro_input, parse_quote, DeriveInput}; // 用于解析宏输入 use syn::{parse_macro_input, DeriveInput}; // 用于解析宏输入
mod responsable; mod responsable;
mod route; mod route;
@ -21,18 +21,27 @@ pub fn responsable(input: TokenStream) -> TokenStream {
responsable::gen_responsable(input) responsable::gen_responsable(input)
} }
// #[proc_macro_attribute]
// pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
// route::gen_route(attr, item)
// // item
// }
#[proc_macro_attribute]
pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
route::gen_route(attr, item)
}
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn get(attr: TokenStream, item: TokenStream) -> TokenStream { pub fn get(attr: TokenStream, item: TokenStream) -> TokenStream {
route::gen_get_route(attr, item) route::gen_get_route(attr, item)
}
#[proc_macro_attribute]
pub fn post(attr: TokenStream, item: TokenStream) -> TokenStream {
route::gen_post_route(attr, item)
}
#[proc_macro_attribute]
pub fn put(attr: TokenStream, item: TokenStream) -> TokenStream {
route::gen_put_route(attr, item)
}
#[proc_macro_attribute]
pub fn delete(attr: TokenStream, item: TokenStream) -> TokenStream {
route::gen_delete_route(attr, item)
}
#[proc_macro_attribute]
pub fn option(attr: TokenStream, item: TokenStream) -> TokenStream {
route::gen_option_route(attr, item)
} }

View File

@ -34,7 +34,7 @@ impl Args {
)) ))
} }
} }
pub fn get_route(&self) -> syn::Result<syn::Expr> { pub fn get_route(&self) -> syn::Result<syn::Expr> {
match self.vars.get(0) { match self.vars.get(0) {
Some(var) => Ok(var.clone()), Some(var) => Ok(var.clone()),
@ -46,6 +46,7 @@ impl Args {
} }
} }
#[allow(dead_code)]
pub fn gen_route(attr: TokenStream, item: TokenStream) -> TokenStream { pub fn gen_route(attr: TokenStream, item: TokenStream) -> TokenStream {
let args = parse_macro_input!(attr as Args); let args = parse_macro_input!(attr as Args);
let func = parse_macro_input!(item as ItemFn); let func = parse_macro_input!(item as ItemFn);

View File

@ -1,11 +1,11 @@
use domain::{dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword, RefreshToken}, vo::account::{LoginAccount, RefreshTokenResult}}; use domain::{dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword, RefreshToken}, vo::account::{LoginAccount, RefreshTokenResult}};
use library::{context::Context, model::response::ResResult, extractor::body_extractor::JsonBody}; use library::{context::Context, model::response::ResResult, extractor::body_extractor::JsonBody};
use macros::post;
use crate::service; use crate::service;
/// post: /account/google
///
/// google账号登录 /// google账号登录
#[post("/account/google")]
pub async fn authenticate_google( pub async fn authenticate_google(
context: Context, context: Context,
JsonBody(req): JsonBody<AuthenticateGooleAccountReq> JsonBody(req): JsonBody<AuthenticateGooleAccountReq>
@ -13,9 +13,8 @@ pub async fn authenticate_google(
service::account_service::authenticate_google(context, req).await service::account_service::authenticate_google(context, req).await
} }
/// post: /account/sys
///
/// 账号密码登录 /// 账号密码登录
#[post("/account/sys")]
pub async fn authenticate_with_password( pub async fn authenticate_with_password(
context: Context, context: Context,
JsonBody(req): JsonBody<AuthenticateWithPassword> JsonBody(req): JsonBody<AuthenticateWithPassword>
@ -23,9 +22,8 @@ pub async fn authenticate_with_password(
service::sys_account_service::authenticate_with_password(context, req).await service::sys_account_service::authenticate_with_password(context, req).await
} }
/// post: /account/refresh-token
///
/// 刷新token /// 刷新token
#[post("/account/refresh-token")]
pub async fn refresh_token( pub async fn refresh_token(
context: Context, context: Context,
JsonBody(refresh_token): JsonBody<RefreshToken> JsonBody(refresh_token): JsonBody<RefreshToken>

View File

@ -8,13 +8,12 @@ use library::extractor::path_extractor::PathVar;
use library::model::response::ResResult; use library::model::response::ResResult;
use library::extractor::body_extractor::JsonBody; use library::extractor::body_extractor::JsonBody;
use library::extractor::query_extractor::QueryParams; use library::extractor::query_extractor::QueryParams;
use macros::get; use macros::{get, post};
use crate::service; use crate::service;
/// post: /feedback
///
/// 添加反馈信息 /// 添加反馈信息
#[post("/feedback")]
pub async fn add_feedback( pub async fn add_feedback(
context: Context, context: Context,
JsonBody(req): JsonBody<FeedbackAdd>, JsonBody(req): JsonBody<FeedbackAdd>,
@ -22,9 +21,8 @@ pub async fn add_feedback(
service::feedback_service::add_feedback(context, req).await service::feedback_service::add_feedback(context, req).await
} }
/// get: /feedback
///
/// 获取反馈信息列表 /// 获取反馈信息列表
#[get("/feedback")]
pub async fn get_feedback_list_by_page( pub async fn get_feedback_list_by_page(
context: Context, context: Context,
QueryParams(page_params): QueryParams<PageParams>, QueryParams(page_params): QueryParams<PageParams>,
@ -37,7 +35,6 @@ pub async fn get_feedback_list_by_page(
.await .await
} }
// #[route(get, "/feedback/:page/:pageSize")]
#[get("/feedback/:page/:pageSize")] #[get("/feedback/:page/:pageSize")]
pub async fn get_feedback_list( pub async fn get_feedback_list(
context: Context, context: Context,

View File

@ -1,48 +1,16 @@
use axum::{routing::{get, post, MethodRouter}, Router}; use axum::Router;
use library::typed_router::TypedRouter; use library::typed_router::TypedRouter;
pub mod account_controller; pub mod account_controller;
pub mod feedback_controller; pub mod feedback_controller;
pub fn init() -> Router { pub fn init() -> Router {
// let a = feedback_controller::get_feedback_list; Router::new()
.typed_route(account_controller::authenticate_google)
// axum::routing::method_routing::MethodRouter::new() .typed_route(account_controller::authenticate_with_password)
.typed_route(account_controller::refresh_token)
.typed_route(feedback_controller::add_feedback)
let router = Router::new() .typed_route(feedback_controller::get_feedback_list_by_page)
.route( .typed_route(feedback_controller::get_feedback_list)
"/account/google",
post(account_controller::authenticate_google),
)
.route(
"/account/sys",
post(account_controller::authenticate_with_password),
)
.route(
"/account/refresh-token",
post(account_controller::refresh_token)
)
.route(
"/feedback",
post(feedback_controller::add_feedback)
.get(feedback_controller::get_feedback_list_by_page)
)
.typed_route(route)
// .typed_route(feedback_controller::get_feedback_list)
// .merge(feedback_controller::get_feedback_list::route())
// .typed_route(route)
// .route(
// "/feedback/:page/:pageSize",
// get(feedback_controller::get_feedback_list)
// )
;
return router;
} }
fn route() -> (&'static str, MethodRouter) {
pub async fn route() -> String {
return "hello world".to_string();
}
("/feedback1", axum::routing::get(route))
}

View File

@ -7,6 +7,6 @@ pub fn get_tasks() -> Vec<Task> {
vec!(Task { vec!(Task {
name: "google_iap".to_string(), name: "google_iap".to_string(),
job: Box::new(google_tasks::xxx_task), job: Box::new(google_tasks::xxx_task),
trigger: "0 0/1 * * * ?".to_string(), trigger: "0 0 0/1 * * ?".to_string(),
}) })
} }