refactor: rewrite entire codebase in axum instead of actix

Replaces actix with axum for web, allows us to use socket.io and gives us access to the tower ecosystem of middleware

breaks compatibility with our current websocket implementation, needs to be reimplemented for socket.io
This commit is contained in:
Radical 2025-07-16 16:36:22 +02:00
parent 3647086adb
commit 324137ce8b
47 changed files with 1381 additions and 1129 deletions

View file

@ -1,19 +1,33 @@
//! `/api/v1/users` Contains endpoints related to all users
use actix_web::{HttpRequest, HttpResponse, Scope, get, web};
use std::sync::Arc;
use axum::{
Json, Router,
extract::{Query, State},
http::StatusCode,
response::IntoResponse,
routing::get,
};
use axum_extra::{
TypedHeader,
headers::{Authorization, authorization::Bearer},
};
use crate::{
Data,
AppState,
api::v1::auth::check_access_token,
error::Error,
objects::{StartAmountQuery, User},
utils::{get_auth_header, global_checks},
utils::global_checks,
};
mod uuid;
pub fn web() -> Scope {
web::scope("/users").service(get).service(uuid::get)
pub fn router() -> Router<Arc<AppState>> {
Router::new()
.route("/", get(users))
.route("/{uuid}", get(uuid::get))
}
/// `GET /api/v1/users` Returns all users on this instance
@ -46,31 +60,26 @@ pub fn web() -> Scope {
/// ]);
/// ```
/// NOTE: UUIDs in this response are made using `uuidgen`, UUIDs made by the actual backend will be UUIDv7 and have extractable timestamps
#[get("")]
pub async fn get(
req: HttpRequest,
request_query: web::Query<StartAmountQuery>,
data: web::Data<Data>,
) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
pub async fn users(
State(app_state): State<Arc<AppState>>,
Query(request_query): Query<StartAmountQuery>,
TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
) -> Result<impl IntoResponse, Error> {
let start = request_query.start.unwrap_or(0);
let amount = request_query.amount.unwrap_or(10);
if amount > 100 {
return Ok(HttpResponse::BadRequest().finish());
return Ok(StatusCode::BAD_REQUEST.into_response());
}
let mut conn = data.pool.get().await?;
let mut conn = app_state.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
let uuid = check_access_token(auth.token(), &mut conn).await?;
global_checks(&data, uuid).await?;
global_checks(&app_state, uuid).await?;
let users = User::fetch_amount(&mut conn, start, amount).await?;
Ok(HttpResponse::Ok().json(users))
Ok((StatusCode::OK, Json(users)).into_response())
}