feat: ✨ token extractor middleware
This commit is contained in:
parent
96199f71ef
commit
4f874d8789
9 changed files with 247 additions and 32 deletions
|
@ -1,28 +1,45 @@
|
|||
use std::sync::{Arc, LazyLock, RwLock};
|
||||
|
||||
use axum::{
|
||||
Json, Router,
|
||||
Extension, Json, Router,
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::{get, post},
|
||||
};
|
||||
use axum_macros::debug_handler;
|
||||
use protocol::Signal;
|
||||
use protocol::{Signal, User, UserAndSignal};
|
||||
use tokio::net::TcpListener;
|
||||
use tower_http::{cors::CorsLayer, trace::TraceLayer};
|
||||
|
||||
static SIGNALS: LazyLock<Arc<RwLock<Vec<Signal>>>> =
|
||||
LazyLock::new(|| Arc::new(RwLock::new(vec![])));
|
||||
use crate::middleware::{
|
||||
UserAndExpectedSignal, verify_then_get_user, verify_then_get_user_and_expected_signal,
|
||||
};
|
||||
|
||||
static USERS_AND_SIGNALS: LazyLock<RwLock<Vec<UserAndSignal>>> =
|
||||
LazyLock::new(|| RwLock::new(vec![]));
|
||||
|
||||
pub async fn start_signalling() {
|
||||
let route = route();
|
||||
let listener = TcpListener::bind("0.0.0.0:4546").await.unwrap();
|
||||
let route = route()
|
||||
.layer(CorsLayer::permissive())
|
||||
.layer(TraceLayer::new_for_http());
|
||||
let listener = TcpListener::bind("192.168.1.3:4546").await.unwrap();
|
||||
println!("http://192.168.1.3:4546");
|
||||
axum::serve(listener, route).await.unwrap();
|
||||
}
|
||||
|
||||
fn route() -> Router {
|
||||
Router::new()
|
||||
.route("/alive", get(alive))
|
||||
.route("/", post(signal))
|
||||
.route(
|
||||
"/",
|
||||
get(read_signal).route_layer(axum::middleware::from_fn(
|
||||
verify_then_get_user_and_expected_signal,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/",
|
||||
post(create_signal).route_layer(axum::middleware::from_fn(verify_then_get_user)),
|
||||
)
|
||||
}
|
||||
|
||||
async fn alive() -> impl IntoResponse {
|
||||
|
@ -30,7 +47,33 @@ async fn alive() -> impl IntoResponse {
|
|||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn signal(Json(signal): Json<Signal>) -> impl IntoResponse {
|
||||
SIGNALS.write().unwrap().push(signal);
|
||||
async fn create_signal(
|
||||
Extension(user): Extension<Arc<User>>,
|
||||
Json(signal): Json<Signal>,
|
||||
) -> impl IntoResponse {
|
||||
let user = (*user).clone();
|
||||
let user_and_signal = UserAndSignal::new(user, signal).await;
|
||||
USERS_AND_SIGNALS.write().unwrap().push(user_and_signal);
|
||||
StatusCode::OK
|
||||
}
|
||||
|
||||
async fn read_signal(
|
||||
Extension(user_and_expected_signal): Extension<Arc<UserAndExpectedSignal>>,
|
||||
) -> impl IntoResponse {
|
||||
let mut target_index = None;
|
||||
let mut json_body = serde_json::json!("");
|
||||
for (index, user_and_signal) in USERS_AND_SIGNALS.read().unwrap().iter().enumerate() {
|
||||
if user_and_signal.signal.get_signal_type() == user_and_expected_signal.expected_signal {
|
||||
json_body = serde_json::json!(user_and_signal);
|
||||
target_index = Some(index);
|
||||
}
|
||||
}
|
||||
|
||||
match target_index {
|
||||
Some(target_index) => {
|
||||
USERS_AND_SIGNALS.write().unwrap().remove(target_index);
|
||||
(StatusCode::OK, Json(json_body)).into_response()
|
||||
}
|
||||
None => StatusCode::BAD_REQUEST.into_response(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue