♻️ 使用 Actix RS 重构 #8

Merged
LittleSheep merged 7 commits from refactor/actix-rs into refactor/rust 2024-02-13 12:39:08 +00:00
5 changed files with 88 additions and 2 deletions
Showing only changes of commit ae3894bea6 - Show all commits

View File

@ -1,6 +1,7 @@
pub mod auth;
mod config;
mod proxies;
mod sideload;
pub mod warden;
use actix_web::{App, HttpServer, web};
@ -51,6 +52,17 @@ async fn main() -> Result<(), std::io::Error> {
.unwrap_or("0.0.0.0:80".to_string())
)?.run();
// Sideload
let sideload_server = HttpServer::new(|| {
App::new().service(sideload::service())
}).bind(
config::C
.read()
.await
.get_string("listen.sideload")
.unwrap_or("0.0.0.0:81".to_string())
)?.run();
// Process manager
{
let mut app = ROAD.lock().await;
@ -61,7 +73,7 @@ async fn main() -> Result<(), std::io::Error> {
app.warden.start().await;
}
tokio::try_join!(proxies_server)?;
tokio::try_join!(proxies_server, sideload_server)?;
Ok(())
}

View File

@ -1,4 +1,3 @@
use futures_util::{SinkExt};
use std::{
ffi::OsStr,
path::{Path, PathBuf},

14
src/sideload/mod.rs Normal file
View File

@ -0,0 +1,14 @@
use actix_web::{Scope, web};
use crate::sideload::overview::get_overview;
use crate::sideload::regions::list_region;
mod overview;
mod regions;
static ROOT: &str = "";
pub fn service() -> Scope {
web::scope("/cgi")
.route(ROOT, web::get().to(get_overview))
.route("/regions", web::get().to(list_region))
}

52
src/sideload/overview.rs Normal file
View File

@ -0,0 +1,52 @@
use actix_web::web;
use serde::Serialize;
use crate::proxies::config::{Destination, Location};
use crate::proxies::metrics::RoadTrace;
use crate::ROAD;
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OverviewData {
regions: usize,
locations: usize,
destinations: usize,
requests_count: u64,
failures_count: u64,
successes_count: u64,
success_rate: f64,
recent_successes: Vec<RoadTrace>,
recent_errors: Vec<RoadTrace>,
}
pub async fn get_overview() -> web::Json<OverviewData> {
let locked_app = ROAD.lock().await;
let regions = locked_app.regions.clone();
let locations = regions
.iter()
.flat_map(|item| item.locations.clone())
.collect::<Vec<Location>>();
let destinations = locations
.iter()
.flat_map(|item| item.destinations.clone())
.collect::<Vec<Destination>>();
web::Json(OverviewData {
regions: regions.len(),
locations: locations.len(),
destinations: destinations.len(),
requests_count: locked_app.metrics.requests_count,
successes_count: locked_app.metrics.requests_count - locked_app.metrics.failures_count,
failures_count: locked_app.metrics.failures_count,
success_rate: locked_app.metrics.get_success_rate(),
recent_successes: locked_app
.metrics
.recent_successes
.clone()
.into_iter()
.collect::<Vec<_>>(),
recent_errors: locked_app
.metrics
.recent_errors
.clone()
.into_iter()
.collect::<Vec<_>>(),
})
}

9
src/sideload/regions.rs Normal file
View File

@ -0,0 +1,9 @@
use actix_web::web;
use crate::proxies::config::Region;
use crate::ROAD;
pub async fn list_region() -> web::Json<Vec<Region>> {
let locked_app = ROAD.lock().await;
web::Json(locked_app.regions.clone())
}