[wish-server-rs] Add endpoint to see view count

main
Charlotte Som 2023-03-13 17:04:49 +00:00
parent bcf019ea19
commit 11496fff2b
5 changed files with 41 additions and 2 deletions

View File

@ -3147,6 +3147,7 @@ dependencies = [
"dotenv", "dotenv",
"miette", "miette",
"once_cell", "once_cell",
"serde",
"sqlx", "sqlx",
"tokio", "tokio",
"tower-http", "tower-http",

View File

@ -10,6 +10,7 @@ axum = { version = "0.6.11", features = ["macros"] }
dotenv = "0.15.0" dotenv = "0.15.0"
miette = { version = "5.5.0", features = ["fancy"] } miette = { version = "5.5.0", features = ["fancy"] }
once_cell = "1.17.1" once_cell = "1.17.1"
serde = { version = "1.0.155", features = ["derive"] }
sqlx = { version = "0.6.2", features = ["runtime-tokio-rustls", "sqlite"] } sqlx = { version = "0.6.2", features = ["runtime-tokio-rustls", "sqlite"] }
tokio = { version = "1.26.0", features = ["full"] } tokio = { version = "1.26.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["trace", "cors"] } tower-http = { version = "0.4.0", features = ["trace", "cors"] }

View File

@ -12,7 +12,12 @@ mod streams;
mod util; mod util;
mod wish; mod wish;
use crate::wish::{setup_webrtc, whep::handle_whep, whip::handle_whip}; use crate::wish::{
channel_info::{self, get_channel_info},
setup_webrtc,
whep::handle_whep,
whip::handle_whip,
};
#[derive(Clone)] #[derive(Clone)]
pub struct AppState { pub struct AppState {
@ -55,6 +60,10 @@ async fn main() -> Result<()> {
let app = Router::new() let app = Router::new()
.route("/api/wish-server/whip", routing::any(handle_whip)) .route("/api/wish-server/whip", routing::any(handle_whip))
.route("/api/wish-server/whep", routing::any(handle_whep)) .route("/api/wish-server/whep", routing::any(handle_whep))
.route(
"/api/wish-server/stream/:channel",
routing::get(get_channel_info),
)
.layer(TraceLayer::new_for_http()) .layer(TraceLayer::new_for_http())
.layer( .layer(
CorsLayer::new() CorsLayer::new()
@ -75,7 +84,8 @@ async fn main() -> Result<()> {
axum::Server::bind(&bind_addr) axum::Server::bind(&bind_addr)
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await
.unwrap(); .into_diagnostic()
.wrap_err("Server listening failed")?;
Ok(()) Ok(())
} }

View File

@ -0,0 +1,26 @@
use std::sync::atomic::Ordering;
use axum::{
extract::{Path, State},
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde::Serialize;
use crate::{streams::get_ongoing_stream, util::log_http_error};
#[derive(Serialize)]
struct ChannelInfo {
view_count: u64,
}
pub async fn get_channel_info(Path(channel): Path<String>) -> Response {
let channel = match get_ongoing_stream(&channel).await {
Ok(c) => c,
Err(e) => return log_http_error(StatusCode::INTERNAL_SERVER_ERROR, e),
};
let view_count = channel.viewer_count.load(Ordering::Relaxed);
let info = ChannelInfo { view_count };
Json(info).into_response()
}

View File

@ -11,6 +11,7 @@ use webrtc::{
peer_connection::configuration::RTCConfiguration, peer_connection::configuration::RTCConfiguration,
}; };
pub mod channel_info;
pub mod whep; pub mod whep;
pub mod whip; pub mod whip;