Initial Implementation of ResourceGuard Macro
This commit is contained in:
16
Cargo.lock
generated
16
Cargo.lock
generated
@@ -497,14 +497,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
|
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "message-hideyoshi_com"
|
name = "message-hideyoshi-com"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"proc-utils",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_with",
|
"serde_with",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"utils",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -627,6 +629,14 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.35"
|
version = "1.0.35"
|
||||||
@@ -912,6 +922,10 @@ version = "1.0.12"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uuid"
|
name = "uuid"
|
||||||
version = "1.7.0"
|
version = "1.7.0"
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
|
workspace = { members = ["proc-utils", "utils"] }
|
||||||
[package]
|
[package]
|
||||||
name = "message-hideyoshi_com"
|
name = "message-hideyoshi-com"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
proc-utils = { path = "proc-utils" }
|
||||||
|
utils = { path = "utils" }
|
||||||
|
|
||||||
axum = "0.7.4"
|
axum = "0.7.4"
|
||||||
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
|
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
|
||||||
uuid = "1.7.0"
|
uuid = "1.7.0"
|
||||||
|
|||||||
13
proc-utils/Cargo.toml
Normal file
13
proc-utils/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "proc-utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
syn = "2.0.50"
|
||||||
|
quote = "1.0.35"
|
||||||
11
proc-utils/src/lib.rs
Normal file
11
proc-utils/src/lib.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
use syn::{parse_macro_input, ItemFn};
|
||||||
|
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn guard_resource(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
let input = parse_macro_input!(item as ItemFn);
|
||||||
|
println!("{} defined", input.sig.ident);
|
||||||
|
println!("Args received: {}", _attr.to_string());
|
||||||
|
return TokenStream::from(quote!(#input));
|
||||||
|
}
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
use axum::{
|
use crate::model::generic_response::GenericResponse;
|
||||||
http::StatusCode,
|
use axum::{http::StatusCode, response::IntoResponse, Json};
|
||||||
response::IntoResponse,
|
use proc_utils::guard_resource;
|
||||||
Json,
|
|
||||||
};
|
|
||||||
use crate::model::generic_response::{GenericResponse};
|
|
||||||
|
|
||||||
|
#[guard_resource(ResourceType::OPEN)]
|
||||||
pub async fn health_check() -> impl IntoResponse {
|
pub async fn health_check() -> impl IntoResponse {
|
||||||
const MESSAGE: &str = "Server is running";
|
const MESSAGE: &str = "Server is running";
|
||||||
let response = GenericResponse {
|
let response = GenericResponse {
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
use axum::{
|
use crate::model::send_message::SendMessage;
|
||||||
http::StatusCode,
|
use axum::{http::StatusCode, response::IntoResponse, Json};
|
||||||
response::IntoResponse,
|
use proc_utils::guard_resource;
|
||||||
Json,
|
|
||||||
};
|
|
||||||
use crate::model::send_message::{SendMessage};
|
|
||||||
|
|
||||||
|
#[guard_resource(ResourceType::OPEN)]
|
||||||
pub async fn send_message(Json(payload): Json<SendMessage>) -> impl IntoResponse {
|
pub async fn send_message(Json(payload): Json<SendMessage>) -> impl IntoResponse {
|
||||||
(StatusCode::OK, Json(payload))
|
(StatusCode::OK, Json(payload))
|
||||||
}
|
}
|
||||||
1
src/interceptor/mod.rs
Normal file
1
src/interceptor/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
10
src/main.rs
10
src/main.rs
@@ -1,11 +1,7 @@
|
|||||||
mod route;
|
|
||||||
mod model;
|
|
||||||
mod handler;
|
mod handler;
|
||||||
|
mod interceptor;
|
||||||
use axum::http::{
|
mod model;
|
||||||
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
|
mod route;
|
||||||
HeaderValue, Method
|
|
||||||
};
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct GenericResponse {
|
pub struct GenericResponse {
|
||||||
pub status: String,
|
pub status: String,
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
|
use chrono::naive::serde::ts_seconds::deserialize as from_ts;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_with::serde_as;
|
use serde_with::serde_as;
|
||||||
use chrono::naive::serde::ts_seconds::deserialize as from_ts;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct MessageAuthor {
|
pub struct MessageAuthor {
|
||||||
name: String,
|
name: String,
|
||||||
username: String,
|
username: String,
|
||||||
email: String
|
email: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct SendMessage {
|
pub struct SendMessage {
|
||||||
@@ -23,5 +20,5 @@ pub struct SendMessage {
|
|||||||
message: String,
|
message: String,
|
||||||
|
|
||||||
#[serde(deserialize_with = "from_ts")]
|
#[serde(deserialize_with = "from_ts")]
|
||||||
timestamp: NaiveDateTime
|
timestamp: NaiveDateTime,
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
|
use crate::handler::health::health_check;
|
||||||
|
use crate::handler::message::send_message;
|
||||||
use axum::{
|
use axum::{
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Router
|
Router,
|
||||||
};
|
};
|
||||||
use crate::handler::health::{health_check};
|
|
||||||
use crate::handler::message::{send_message};
|
|
||||||
|
|
||||||
|
|
||||||
pub fn create_route() -> Router {
|
pub fn create_route() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
|
|||||||
8
utils/Cargo.toml
Normal file
8
utils/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
24
utils/src/lib.rs
Normal file
24
utils/src/lib.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
pub enum ResourceType {
|
||||||
|
OPEN,
|
||||||
|
USER,
|
||||||
|
ADMIN,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResourceType {
|
||||||
|
fn as_str(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
ResourceType::OPEN => "OPEN",
|
||||||
|
ResourceType::USER => "USER",
|
||||||
|
ResourceType::ADMIN => "ADMIN",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> ResourceType {
|
||||||
|
match s {
|
||||||
|
"OPEN" => ResourceType::OPEN,
|
||||||
|
"USER" => ResourceType::USER,
|
||||||
|
"ADMIN" => ResourceType::ADMIN,
|
||||||
|
_ => panic!("Invalid resource type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user