Initial Commit

This commit is contained in:
Damian Wessels
2025-09-08 11:01:51 +02:00
commit fd33b5ceba
81 changed files with 17432 additions and 0 deletions

1
server/src/app_state.rs Normal file
View File

@@ -0,0 +1 @@
pub static APP_STATE: LazyLock<Arc<AppState>> = LazyLock::new(|| Arc::new(AppState::new()));

15
server/src/main.rs Normal file
View File

@@ -0,0 +1,15 @@
use anyhow::{Error, Result};
//use app_state::APP_STATE;
use env_logger::Env;
use rest::init::init_api;
//use socketio::init::init_socketio;
pub mod rest;
#[tokio::main]
async fn main() -> Result<(), Error> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
init_api().await?;
Ok(())
}

View File

@@ -0,0 +1,32 @@
use axum::body::Body;
use serde::Serialize;
pub mod my_test;
#[derive(Debug, Serialize, Clone)]
pub struct MutationResponse {
pub success: bool,
pub error: Option<String>,
}
impl MutationResponse {
pub fn success() -> Self {
Self {
success: true,
error: None,
}
}
pub fn error(error: String) -> Self {
Self {
success: false,
error: Some(error),
}
}
}
impl From<MutationResponse> for Body {
fn from(mutation_response: MutationResponse) -> Self {
let body = serde_json::to_string(&mutation_response).unwrap();
Body::from(body)
}
}

View File

@@ -0,0 +1,10 @@
use super::MutationResponse;
use crate::rest::util::ResponseUtil;
use axum::{body::Body, extract::State, http::Response, Json};
use std::sync::Arc;
#[axum::debug_handler]
pub async fn post_my_test() -> Response<Body> {
ResponseUtil::ok(MutationResponse::success())
}

24
server/src/rest/init.rs Normal file
View File

@@ -0,0 +1,24 @@
use super::handlers::my_test::post_my_test;
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use std::io::Error;
use axum::routing::post;
pub async fn init_api(
) -> Result<(), Error> {
let cors = CorsLayer::permissive();
let app = axum::Router::new()
.route(
"/api/v1/test",
post(post_my_test)
)
.layer(cors)
.layer(TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await?;
axum::serve(listener, app).await?;
open::that("http://localhost:3001")?;
Ok(())
}

3
server/src/rest/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod handlers;
pub mod init;
pub mod util;

51
server/src/rest/util.rs Normal file
View File

@@ -0,0 +1,51 @@
use axum::{
body::Body,
http::{Response, StatusCode},
};
use serde_json::json;
pub struct ResponseUtil {}
impl ResponseUtil {
pub fn error(message: &str) -> Response<Body> {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header("Content-Type", "application/json")
.body(Body::from(
serde_json::to_string(&json!({ "error": message })).unwrap(),
))
.unwrap()
}
pub fn ok<T: serde::Serialize>(data: T) -> Response<Body> {
Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(Body::from(serde_json::to_string(&data).unwrap()))
.unwrap()
}
pub fn not_found(message: &str) -> Response<Body> {
Response::builder()
.status(StatusCode::NOT_FOUND)
.header("Content-Type", "application/json")
.body(Body::from(
serde_json::to_string(&json!({ "error": message })).unwrap(),
))
.unwrap()
}
}
pub enum ResponseUtilError {
Error(anyhow::Error),
NotFound(anyhow::Error),
}
impl From<ResponseUtilError> for Response<Body> {
fn from(error: ResponseUtilError) -> Self {
match error {
ResponseUtilError::Error(e) => ResponseUtil::error(&e.to_string()),
ResponseUtilError::NotFound(e) => ResponseUtil::not_found(&e.to_string()),
}
}
}