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,92 +1,92 @@
use std::sync::Arc;
use ::uuid::Uuid;
use axum::{
Json,
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
};
use axum_extra::{
TypedHeader,
headers::{Authorization, authorization::Bearer},
};
use serde::Deserialize;
use crate::{
Data,
AppState,
api::v1::auth::check_access_token,
error::Error,
objects::{Channel, Member, Permissions},
utils::{get_auth_header, global_checks, order_by_is_above},
utils::{global_checks, order_by_is_above},
};
use ::uuid::Uuid;
use actix_web::{HttpRequest, HttpResponse, get, post, web};
use serde::Deserialize;
#[derive(Deserialize)]
struct ChannelInfo {
pub struct ChannelInfo {
name: String,
description: Option<String>,
}
#[get("{uuid}/channels")]
pub async fn get(
req: HttpRequest,
path: web::Path<(Uuid,)>,
data: web::Data<Data>,
) -> Result<HttpResponse, Error> {
let headers = req.headers();
State(app_state): State<Arc<AppState>>,
Path(guild_uuid): Path<Uuid>,
TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
) -> Result<impl IntoResponse, Error> {
let mut conn = app_state.pool.get().await?;
let auth_header = get_auth_header(headers)?;
let uuid = check_access_token(auth.token(), &mut conn).await?;
let guild_uuid = path.into_inner().0;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
global_checks(&data, uuid).await?;
global_checks(&app_state, uuid).await?;
Member::check_membership(&mut conn, uuid, guild_uuid).await?;
if let Ok(cache_hit) = data.get_cache_key(format!("{guild_uuid}_channels")).await {
return Ok(HttpResponse::Ok()
.content_type("application/json")
.body(cache_hit));
if let Ok(cache_hit) = app_state
.get_cache_key(format!("{guild_uuid}_channels"))
.await
{
return Ok((StatusCode::OK, Json(cache_hit)).into_response());
}
let channels = Channel::fetch_all(&data.pool, guild_uuid).await?;
let channels = Channel::fetch_all(&app_state.pool, guild_uuid).await?;
let channels_ordered = order_by_is_above(channels).await?;
data.set_cache_key(
format!("{guild_uuid}_channels"),
channels_ordered.clone(),
1800,
)
.await?;
app_state
.set_cache_key(
format!("{guild_uuid}_channels"),
channels_ordered.clone(),
1800,
)
.await?;
Ok(HttpResponse::Ok().json(channels_ordered))
Ok((StatusCode::OK, Json(channels_ordered)).into_response())
}
#[post("{uuid}/channels")]
pub async fn create(
req: HttpRequest,
channel_info: web::Json<ChannelInfo>,
path: web::Path<(Uuid,)>,
data: web::Data<Data>,
) -> Result<HttpResponse, Error> {
let headers = req.headers();
State(app_state): State<Arc<AppState>>,
Path(guild_uuid): Path<Uuid>,
TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
Json(channel_info): Json<ChannelInfo>,
) -> Result<impl IntoResponse, Error> {
let mut conn = app_state.pool.get().await?;
let auth_header = get_auth_header(headers)?;
let uuid = check_access_token(auth.token(), &mut conn).await?;
let guild_uuid = path.into_inner().0;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
global_checks(&data, uuid).await?;
global_checks(&app_state, uuid).await?;
let member = Member::check_membership(&mut conn, uuid, guild_uuid).await?;
member
.check_permission(&data, Permissions::ManageChannel)
.check_permission(&app_state, Permissions::ManageChannel)
.await?;
let channel = Channel::new(
data.clone(),
&app_state,
guild_uuid,
channel_info.name.clone(),
channel_info.description.clone(),
)
.await?;
Ok(HttpResponse::Ok().json(channel))
Ok((StatusCode::OK, Json(channel)))
}