web speedtest
This commit is contained in:
+36
-2
@@ -1,9 +1,16 @@
|
||||
use axum::{http::StatusCode, response::IntoResponse, routing::post, Json, Router};
|
||||
use axum::{
|
||||
body::Bytes,
|
||||
http::{header, Method, StatusCode},
|
||||
response::IntoResponse,
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use regex::Regex;
|
||||
use reqwest::Client;
|
||||
use rustrict::CensorStr;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::LazyLock;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct CommentPayload {
|
||||
@@ -162,7 +169,20 @@ fn is_spam(input: &str) -> bool {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Router::new().route("/api/comments", post(create_comment));
|
||||
// Настраиваем правила CORS
|
||||
let cors = CorsLayer::new()
|
||||
// Разрешаем запросы только с твоего фронтенда
|
||||
.allow_origin("netrunner-vpn.com") // Для разработки можно Any, для прода лучше конкретный URL
|
||||
.allow_methods([Method::GET, Method::POST])
|
||||
.allow_headers([header::CONTENT_TYPE]);
|
||||
|
||||
let app = Router::new()
|
||||
.route("/api/comments", post(create_comment))
|
||||
.route("/api/speedtest/ping", get(|| async { "pong" }))
|
||||
.route("/api/speedtest/download", get(speedtest_download))
|
||||
.route("/api/speedtest/upload", post(speedtest_upload))
|
||||
// Применяем CORS ко всем роутам
|
||||
.layer(cors);
|
||||
|
||||
println!("🚀 Netrunner Gateway started on 0.0.0.0:3001");
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap();
|
||||
@@ -229,3 +249,17 @@ async fn create_comment(Json(payload): Json<CommentPayload>) -> impl IntoRespons
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn speedtest_download() -> impl IntoResponse {
|
||||
let payload_size = 25 * 1024 * 1024; // 25 MB
|
||||
let data = vec![0u8; payload_size];
|
||||
|
||||
(
|
||||
[(axum::http::header::CONTENT_TYPE, "application/octet-stream")],
|
||||
data,
|
||||
)
|
||||
}
|
||||
|
||||
async fn speedtest_upload(body: Bytes) -> impl IntoResponse {
|
||||
(StatusCode::OK, format!("Received {} bytes", body.len()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user