[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",
"miette",
"once_cell",
"serde",
"sqlx",
"tokio",
"tower-http",

View File

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

View File

@ -12,7 +12,12 @@ mod streams;
mod util;
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)]
pub struct AppState {
@ -55,6 +60,10 @@ async fn main() -> Result<()> {
let app = Router::new()
.route("/api/wish-server/whip", routing::any(handle_whip))
.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(
CorsLayer::new()
@ -75,7 +84,8 @@ async fn main() -> Result<()> {
axum::Server::bind(&bind_addr)
.serve(app.into_make_service())
.await
.unwrap();
.into_diagnostic()
.wrap_err("Server listening failed")?;
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,
};
pub mod channel_info;
pub mod whep;
pub mod whip;