Initial Commit
This commit is contained in:
1
server/.gitignore
vendored
Normal file
1
server/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
30
server/Cargo.toml
Normal file
30
server/Cargo.toml
Normal file
@@ -0,0 +1,30 @@
|
||||
[package]
|
||||
name = "server"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
# web
|
||||
tower-livereload = "0.9.6"
|
||||
socketioxide-core = "0.16.0"
|
||||
socketioxide = "0.16.0"
|
||||
tower-http = { version = "0.6.2", features = ["cors", "trace", "fs"] }
|
||||
axum = { version = "0.8.1", features = ["macros"] }
|
||||
|
||||
# utility
|
||||
uom = "0.36.0"
|
||||
chrono = "0.4.39"
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
serde = "1.0.217"
|
||||
anyhow = "1.0.95"
|
||||
serde_json = "1.0.137"
|
||||
signal-hook = "0.3.17"
|
||||
log = "0.4.25"
|
||||
env_logger = "0.11.6"
|
||||
tokio = { version = "1.43.0", features = ["rt-multi-thread"] }
|
||||
include_dir = "0.7.4"
|
||||
mime_guess = "2.0.5"
|
||||
open = "5.3.2"
|
||||
bitvec = "1.0.1"
|
||||
lazy_static = "1.5.0"
|
||||
10
server/rust-toolchain.toml
Normal file
10
server/rust-toolchain.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[toolchain]
|
||||
channel = "beta"
|
||||
components = [
|
||||
"rustfmt",
|
||||
"clippy",
|
||||
"rust-src",
|
||||
"rustc-dev",
|
||||
"llvm-tools-preview",
|
||||
"rust-analyzer",
|
||||
]
|
||||
1
server/src/app_state.rs
Normal file
1
server/src/app_state.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub static APP_STATE: LazyLock<Arc<AppState>> = LazyLock::new(|| Arc::new(AppState::new()));
|
||||
15
server/src/main.rs
Normal file
15
server/src/main.rs
Normal 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(())
|
||||
}
|
||||
32
server/src/rest/handlers/mod.rs
Normal file
32
server/src/rest/handlers/mod.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
10
server/src/rest/handlers/my_test.rs
Normal file
10
server/src/rest/handlers/my_test.rs
Normal 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
24
server/src/rest/init.rs
Normal 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
3
server/src/rest/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod handlers;
|
||||
pub mod init;
|
||||
pub mod util;
|
||||
51
server/src/rest/util.rs
Normal file
51
server/src/rest/util.rs
Normal 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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user