pow uses const routes

This commit is contained in:
realaravinth
2021-05-02 17:13:04 +05:30
parent ef778687e0
commit 0829ee1c74
8 changed files with 96 additions and 43 deletions

View File

@@ -17,12 +17,21 @@
#[allow(dead_code)]
pub enum Methods {
/// GET hander
Get,
/// POST handler
Post,
/// Protected GET handler
ProtectGet,
/// Protected POST handler
ProtectPost,
/// CORS allow all orgin GET handler
CorsAllowAllGet,
/// CORS allow all orgin PST handler
CorsAllowAllPost,
}
/// Defines resoures for [Methods]
#[macro_export]
macro_rules! define_resource {
($cfg:expr, $path:expr, Methods::Get, $to:expr) => {
@@ -58,4 +67,29 @@ macro_rules! define_resource {
.to($to),
);
};
($cfg:expr, $path:expr, Methods::CorsAllowAllGet, $cors:expr, $to:expr) => {
$cfg.service(
actix_web::web::resource($path)
.wrap($cors)
.guard(actix_web::guard::Get())
.to($to),
);
};
($cfg:expr, $path:expr, Methods::CorsAllowAllPost, $to:expr) => {
let cors = Cors::default()
.allow_any_origin()
.allowed_methods(vec!["POST"])
.allow_any_header()
.max_age(0)
.send_wildcard();
$cfg.service(
actix_web::web::resource($path)
.wrap(cors)
.guard(actix_web::guard::Post())
.to($to),
);
};
}