refactor: Refactor json loading mechanism & more
- Move json loading into separate crate `common` - Add new http route for handling SRTools API - Listen to `freesr-data.json` file change, and sync with client immediately - Move json loading into `PlayerSession`, instead of load it everytime - Implement global buff for Castorice - Implement `GetBigDataAllRecommendCsReq`
This commit is contained in:
+15
-2
@@ -1,8 +1,11 @@
|
||||
use anyhow::Result;
|
||||
use axum::routing::{get, post};
|
||||
use axum::Router;
|
||||
use axum::http::Method;
|
||||
use axum::http::header::CONTENT_TYPE;
|
||||
use axum::routing::{get, post};
|
||||
use logging::init_tracing;
|
||||
use services::{auth, dispatch, errors};
|
||||
use services::{auth, dispatch, errors, sr_tools};
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
use tracing::Level;
|
||||
|
||||
mod config;
|
||||
@@ -40,6 +43,16 @@ async fn main() -> Result<()> {
|
||||
auth::GRANTER_LOGIN_VERIFICATION_ENDPOINT,
|
||||
post(auth::granter_login_verification),
|
||||
)
|
||||
.route(
|
||||
sr_tools::SRTOOLS_UPLOAD_ENDPOINT,
|
||||
post(sr_tools::sr_tool_save),
|
||||
)
|
||||
.layer(
|
||||
CorsLayer::new()
|
||||
.allow_origin(Any)
|
||||
.allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE])
|
||||
.allow_headers([CONTENT_TYPE]),
|
||||
)
|
||||
.fallback(errors::not_found);
|
||||
|
||||
let addr = format!("0.0.0.0:{PORT}");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod auth;
|
||||
pub mod dispatch;
|
||||
pub mod errors;
|
||||
pub mod sr_tools;
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
use axum::Json;
|
||||
use common::sr_tools::FreesrData;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::fs;
|
||||
|
||||
pub const SRTOOLS_UPLOAD_ENDPOINT: &str = "/srtools";
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct SrToolDataReq {
|
||||
#[allow(dead_code)]
|
||||
pub data: Option<FreesrData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct SrToolDataRsp {
|
||||
pub message: String,
|
||||
pub status: u32,
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn sr_tool_save(Json(json): Json<SrToolDataReq>) -> Json<SrToolDataRsp> {
|
||||
let Some(json) = json.data else {
|
||||
return Json(SrToolDataRsp {
|
||||
message: String::from("OK"),
|
||||
status: 200,
|
||||
});
|
||||
};
|
||||
|
||||
let json = match serde_json::to_string_pretty(&json) {
|
||||
Ok(json) => json,
|
||||
Err(err) => {
|
||||
return Json(SrToolDataRsp {
|
||||
message: format!("malformed json: {}", err),
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(err) = fs::write("freesr-data.json", json).await {
|
||||
return Json(SrToolDataRsp {
|
||||
message: format!("failed to write freesr-data.json: {}", err),
|
||||
status: 200,
|
||||
});
|
||||
};
|
||||
|
||||
Json(SrToolDataRsp {
|
||||
message: String::from("OK"),
|
||||
status: 200,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user