slop
This commit is contained in:
+109
-13
@@ -19,28 +19,25 @@ static KEY_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[A-Za-z0-9 _-]{1
|
||||
|
||||
use axum::{
|
||||
Json, Router,
|
||||
body::{Body, Bytes},
|
||||
extract::{Form, FromRequestParts, Path, Query, State},
|
||||
extract::{FromRequestParts, Path, State},
|
||||
http::{
|
||||
HeaderMap, HeaderName, Method, StatusCode,
|
||||
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE, SET_COOKIE},
|
||||
HeaderName, Method, StatusCode,
|
||||
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
|
||||
request::Parts,
|
||||
},
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
routing::{delete, get, post},
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use entity::{auth_session, stream_key, stream_session, users};
|
||||
use sea_orm::{
|
||||
DatabaseConnection, EntityTrait, IntoActiveModel, QueryFilter, prelude::DateTimeUtc,
|
||||
};
|
||||
use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel, QueryFilter};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sysinfo::System;
|
||||
use tokio::{
|
||||
net::TcpListener,
|
||||
sync::{Mutex, mpsc::Sender},
|
||||
};
|
||||
use tower_http::cors::CorsLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
@@ -48,7 +45,7 @@ use crate::{
|
||||
AppState,
|
||||
hash::{hash_password, verify_password},
|
||||
http_error::HttpError,
|
||||
webrtc_ingest::handle_whip_injest,
|
||||
webrtc_ingest::{handle_whip_injest, handle_whip_injest_delete, handle_whip_injest_patch},
|
||||
};
|
||||
|
||||
const MAX_USERNAME_LEN: usize = 32;
|
||||
@@ -58,6 +55,11 @@ pub struct HttpServerConfig {
|
||||
pub signup_code: String,
|
||||
}
|
||||
|
||||
pub struct ServerInfo {
|
||||
pub version: &'static str,
|
||||
pub start_time: std::time::Instant,
|
||||
}
|
||||
|
||||
pub struct HttpServer {
|
||||
pub offer_tx: Sender<(i32, i32, String)>,
|
||||
pub accept_rx: async_broadcast::InactiveReceiver<(i32, Option<String>)>,
|
||||
@@ -65,6 +67,7 @@ pub struct HttpServer {
|
||||
pub request_count: AtomicI32,
|
||||
pub db: DatabaseConnection,
|
||||
pub config: Arc<HttpServerConfig>,
|
||||
pub info: Arc<ServerInfo>,
|
||||
}
|
||||
|
||||
impl HttpServer {
|
||||
@@ -77,7 +80,7 @@ impl HttpServer {
|
||||
];
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
|
||||
.allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE, Method::OPTIONS])
|
||||
.allow_headers([
|
||||
AUTHORIZATION,
|
||||
ACCEPT,
|
||||
@@ -97,16 +100,28 @@ impl HttpServer {
|
||||
.patch(edit_stream_key),
|
||||
)
|
||||
// .route("/api/admin/server_stats", get(todo!()))
|
||||
.route("/api/whip", post(handle_whip_injest))
|
||||
// .route("/api/whip", post(handle_whip_injest))
|
||||
.route("/api/login", post(login_handler))
|
||||
.route("/api/stream/{slug}", post(stream_handler))
|
||||
.route("/stream", post(handle_whip_injest))
|
||||
.route("/stream/{slug}", delete(handle_whip_injest_delete).patch(handle_whip_injest_patch))
|
||||
.route("/api/whip/{slug}", delete(handle_whip_injest_delete).patch(handle_whip_injest_patch))
|
||||
.route("/api/meow", get(meow_handler))
|
||||
.route("/api/health", get(health_handler))
|
||||
.route("/api/uptime", get(uptime_handler))
|
||||
.route("/api/version", get(version_handler))
|
||||
.route("/api/stats", get(stats_handler))
|
||||
.layer(cors)
|
||||
.with_state(state);
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
||||
let listener = TcpListener::bind(addr).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
axum::serve(
|
||||
listener,
|
||||
app.into_make_service_with_connect_info::<SocketAddr>(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,6 +351,7 @@ fn cookie_for_token(token: &str, dev: bool) -> Cookie<'static> {
|
||||
false => Cookie::build(("session", token))
|
||||
.path("/")
|
||||
.http_only(true)
|
||||
// .max_age(Duration::from_hours(999999))
|
||||
.same_site(axum_extra::extract::cookie::SameSite::Lax)
|
||||
.build(),
|
||||
}
|
||||
@@ -488,3 +504,83 @@ async fn stream_handler(
|
||||
async fn meow_handler() -> &'static str {
|
||||
"meow"
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct HealthResponse {
|
||||
status: &'static str,
|
||||
uptime_seconds: u64,
|
||||
version: &'static str,
|
||||
}
|
||||
|
||||
async fn health_handler(
|
||||
State(state): State<Arc<HttpServer>>,
|
||||
) -> Result<impl IntoResponse, HttpError> {
|
||||
let uptime = state.info.start_time.elapsed().as_secs();
|
||||
Ok((
|
||||
StatusCode::OK,
|
||||
Json(HealthResponse {
|
||||
status: "ok",
|
||||
uptime_seconds: uptime,
|
||||
version: state.info.version,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct UptimeResponse {
|
||||
uptime_seconds: u64,
|
||||
}
|
||||
|
||||
async fn uptime_handler(
|
||||
State(state): State<Arc<HttpServer>>,
|
||||
) -> Result<impl IntoResponse, HttpError> {
|
||||
let uptime = state.info.start_time.elapsed().as_secs();
|
||||
Ok((
|
||||
StatusCode::OK,
|
||||
Json(UptimeResponse {
|
||||
uptime_seconds: uptime,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
async fn version_handler(
|
||||
State(state): State<Arc<HttpServer>>,
|
||||
) -> Result<impl IntoResponse, HttpError> {
|
||||
Ok((
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!({ "version": state.info.version })),
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct StatsResponse {
|
||||
version: &'static str,
|
||||
uptime_seconds: u64,
|
||||
cpu_usage_percent: f32,
|
||||
active_streams: usize,
|
||||
request_count: i32,
|
||||
}
|
||||
|
||||
async fn stats_handler(
|
||||
State(state): State<Arc<HttpServer>>,
|
||||
) -> Result<impl IntoResponse, HttpError> {
|
||||
let uptime = state.info.start_time.elapsed().as_secs();
|
||||
let mut system = System::new();
|
||||
system.refresh_cpu_all();
|
||||
let cpu_usage = system.global_cpu_usage();
|
||||
let active_streams = {
|
||||
let app = state.appstate.lock().await;
|
||||
app.stream_sessions.len()
|
||||
};
|
||||
let request_count = state.request_count.load(Ordering::Relaxed);
|
||||
Ok((
|
||||
StatusCode::OK,
|
||||
Json(StatsResponse {
|
||||
version: state.info.version,
|
||||
uptime_seconds: uptime,
|
||||
cpu_usage_percent: cpu_usage,
|
||||
active_streams,
|
||||
request_count,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user