🗑️ Remove unused imports, macros and dead code
- Drop `Path`/``State``` from unused Axum imports in config.rs and registry - Mark `_position_sec` field as `#[allow(dead_code)]`` in PositionUpdateRequest and PlayerStateReport - Remove unused macro rules (`add_action_arg!`, `add_action!``, `` add_var!)`` - Delete unused PlayerReport struct and related handler code
This commit is contained in:
@@ -20,8 +20,8 @@ tracing-subscriber = { workspace = true }
|
||||
futures = "0.3"
|
||||
async-stream = "0.3.6"
|
||||
axum-server = "0.7.2"
|
||||
axum-embed = "0.1.0"
|
||||
rust-embed = "8.7.2"
|
||||
mime_guess = "2"
|
||||
utoipa = { version = "5.4.0", features = ["axum_extras"] }
|
||||
utoipa-swagger-ui = { version = "9.0.2", features = ["axum", "vendored"] }
|
||||
once_cell = "1.19"
|
||||
|
||||
@@ -71,6 +71,7 @@
|
||||
pub mod config_ext;
|
||||
pub mod logs;
|
||||
pub mod server;
|
||||
mod serve_embed;
|
||||
|
||||
pub use config_ext::ConfigExt;
|
||||
pub use logs::{
|
||||
|
||||
90
pmoserver/src/serve_embed.rs
Normal file
90
pmoserver/src/serve_embed.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
//! Remplacement minimal de `axum_embed` compatible avec axum 0.8.
|
||||
//!
|
||||
//! Implémente un service Tower qui sert des fichiers embarqués via `rust_embed`,
|
||||
//! avec support optionnel du mode SPA (fallback vers index.html).
|
||||
|
||||
use axum::body::Body;
|
||||
use axum::http::{Request, StatusCode, header};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use rust_embed::RustEmbed;
|
||||
use std::convert::Infallible;
|
||||
use std::marker::PhantomData;
|
||||
use std::task::{Context, Poll};
|
||||
use tower::Service;
|
||||
|
||||
/// Service Tower servant des fichiers embarqués via `RustEmbed`.
|
||||
///
|
||||
/// - Mode normal (`new`): retourne 404 si le fichier n'existe pas.
|
||||
/// - Mode SPA (`with_spa_fallback`): retourne le fichier de fallback (200) si le fichier n'existe pas.
|
||||
#[derive(Clone)]
|
||||
pub struct ServeEmbed<E> {
|
||||
spa_fallback: Option<String>,
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: RustEmbed> ServeEmbed<E> {
|
||||
/// Mode normal : 404 pour les fichiers manquants.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
spa_fallback: None,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Mode SPA : sert `fallback_file` (avec 200) pour les fichiers manquants.
|
||||
pub fn with_spa_fallback(fallback_file: String) -> Self {
|
||||
Self {
|
||||
spa_fallback: Some(fallback_file),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
fn serve(path: &str) -> Option<Response> {
|
||||
let path = path.trim_start_matches('/');
|
||||
// Essaie le chemin exact, puis index.html pour les répertoires
|
||||
let candidates: &[&str] = if path.is_empty() || path.ends_with('/') {
|
||||
&[&format!("{}index.html", path), path]
|
||||
} else {
|
||||
&[path]
|
||||
};
|
||||
|
||||
for candidate in candidates {
|
||||
if let Some(content) = E::get(candidate) {
|
||||
let mime = mime_guess::from_path(candidate).first_or_octet_stream();
|
||||
return Some(
|
||||
(
|
||||
[(header::CONTENT_TYPE, mime.as_ref())],
|
||||
content.data.into_owned(),
|
||||
)
|
||||
.into_response(),
|
||||
);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Service<Request<Body>> for ServeEmbed<E>
|
||||
where
|
||||
E: RustEmbed + Clone + Send + Sync + 'static,
|
||||
{
|
||||
type Response = Response;
|
||||
type Error = Infallible;
|
||||
type Future = std::future::Ready<Result<Response, Infallible>>;
|
||||
|
||||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Infallible>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
||||
let path = req.uri().path();
|
||||
let response = Self::serve(path).unwrap_or_else(|| {
|
||||
if let Some(ref fallback) = self.spa_fallback {
|
||||
Self::serve(fallback).unwrap_or_else(|| StatusCode::NOT_FOUND.into_response())
|
||||
} else {
|
||||
StatusCode::NOT_FOUND.into_response()
|
||||
}
|
||||
});
|
||||
std::future::ready(Ok(response))
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ use axum::handler::Handler;
|
||||
use axum::response::Redirect;
|
||||
use axum::routing::{any, get, post};
|
||||
use axum::{Json, Router};
|
||||
use axum_embed::ServeEmbed;
|
||||
use crate::serve_embed::ServeEmbed;
|
||||
use pmoconfig::get_config;
|
||||
use rust_embed::RustEmbed;
|
||||
use serde::Serialize;
|
||||
@@ -340,11 +340,7 @@ impl Server {
|
||||
where
|
||||
E: RustEmbed + Clone + Send + Sync + 'static,
|
||||
{
|
||||
let serve = ServeEmbed::<E>::with_parameters(
|
||||
Some("index.html".to_string()),
|
||||
axum_embed::FallbackBehavior::Ok,
|
||||
Some("index.html".to_string()),
|
||||
);
|
||||
let serve = ServeEmbed::<E>::with_spa_fallback("index.html".to_string());
|
||||
|
||||
let mut r = self.router.write().await;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user