The system design has changed, but the refactor is not over.

In future commits, I will strive to maintain a consistent style.
This commit is contained in:
Shiroyasha 2024-07-01 03:43:06 +03:00
parent 7594e3d615
commit a1f9eba502
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
25 changed files with 410 additions and 367 deletions

22
src/api/v1/auth.rs Normal file
View file

@ -0,0 +1,22 @@
use axum::{extract::State, http::StatusCode, response::{IntoResponse, Response}};
use crate::{auth::Token, AppState};
pub async fn status(
Token(token): Token,
State(state): State<AppState>
) -> Response {
match token {
Some(token) => {
if state.user_manager.is_authenticated(&token) {
(StatusCode::OK, "ok".to_string()).into_response()
} else {
(StatusCode::UNAUTHORIZED, "unauthorized".to_string()).into_response()
}
}
None => {
(StatusCode::BAD_REQUEST, "bad request".to_string()).into_response()
}
}
}

90
src/api/v1/http2ws.rs Normal file
View file

@ -0,0 +1,90 @@
use axum::{
extract::{Query, State},
http::StatusCode,
response::{IntoResponse, Response}
};
use tracing::debug;
use crate::{auth::Token, AppState};
use super::types::UserUuid;
pub(super) async fn verify(
Token(token): Token,
State(state): State<AppState>,
) -> Response {
state.config.lock().await.clone()
.verify_token(&token)
.unwrap_or_else(|x| x)
}
pub(super) async fn raw(
Token(token): Token,
Query(query): Query<UserUuid>,
State(state): State<AppState>,
body: String,
) -> Response {
debug!(body = body);
match state.config.lock().await.clone().verify_token(&token) {
Ok(_) => {},
Err(e) => return e,
}
let payload = match hex::decode(body) {
Ok(v) => v,
Err(_) => return (StatusCode::NOT_ACCEPTABLE, "not raw data".to_string()).into_response(),
};
debug!("{:?}", payload);
match query.uuid {
Some(uuid) => {
// for only one
let tx = match state.session.get(&uuid) {
Some(d) => d,
None => return (StatusCode::NOT_FOUND, "unknown uuid".to_string()).into_response(),
};
match tx.value().send(payload).await {
Ok(_) => return (StatusCode::OK, "ok".to_string()).into_response(),
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "cant send".to_string()).into_response(),
};
},
None => {
// for all
return (StatusCode::NOT_FOUND, "uuid doesnt defined".to_string()).into_response();
},
}
}
pub(super) async fn sub_raw(
Token(token): Token,
Query(query): Query<UserUuid>,
State(state): State<AppState>,
body: String,
) -> Response {
debug!(body = body);
match state.config.lock().await.clone().verify_token(&token) {
Ok(_) => {},
Err(e) => return e,
}
let payload = match hex::decode(body) {
Ok(v) => v,
Err(_) => return (StatusCode::NOT_ACCEPTABLE, "not raw data".to_string()).into_response(),
};
debug!("{:?}", payload);
match query.uuid {
Some(uuid) => {
// for only one
let tx = match state.broadcasts.get(&uuid) {
Some(d) => d,
None => return (StatusCode::NOT_FOUND, "unknown uuid".to_string()).into_response(),
};
match tx.value().send(payload) {
Ok(_) => return (StatusCode::OK, "ok".to_string()).into_response(),
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "cant send".to_string()).into_response(),
};
},
None => {
return (StatusCode::NOT_FOUND, "uuid doesnt defined".to_string()).into_response();
},
}
}

16
src/api/v1/mod.rs Normal file
View file

@ -0,0 +1,16 @@
use axum::{routing::{get, post}, Router};
use crate::AppState;
mod http2ws;
mod users;
mod auth;
mod types;
pub fn router() -> Router<AppState> {
Router::new()
.route("/verify", get(http2ws::verify))
.route("/raw", post(http2ws::raw))
.route("/sub/raw", post(http2ws::sub_raw))
.route("/auth/", get(auth::status))
.route("/users/create", post(users::create_user))
}

7
src/api/v1/types.rs Normal file
View file

@ -0,0 +1,7 @@
use serde::Deserialize;
use uuid::Uuid;
#[derive(Deserialize)]
pub(super) struct UserUuid {
pub uuid: Option<Uuid>,
}

24
src/api/v1/users.rs Normal file
View file

@ -0,0 +1,24 @@
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
Json
};
use tracing::debug;
use crate::{auth::{Token, Userinfo}, AppState};
pub(super) async fn create_user(
Token(token): Token,
State(state): State<AppState>,
Json(json): Json<Userinfo>
) -> Response {
debug!("Json: {json:?}");
match state.config.lock().await.clone().verify_token(&token) {
Ok(_) => {},
Err(e) => return e,
}
state.user_manager.insert_user(json.uuid, json);
(StatusCode::OK, "ok".to_string()).into_response()
}