Commit 20f557c3 authored by Sebastian Dröge's avatar Sebastian Dröge
Browse files

Implement basic web app

Lists all currently available rooms and has some play/pause logic.
WebRTC integration is still missing.
parent 8b780f44
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -7,13 +7,13 @@ use serde::{Deserialize, Serialize};
/// Response of the `rooms` endpoint.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct Rooms(Vec<Room>);
pub struct Rooms(pub Vec<Room>);

/// Information for one `Room
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct Room {
    id: uuid::Uuid,
    name: String,
    description: Option<String>,
    pub id: uuid::Uuid,
    pub name: String,
    pub description: Option<String>,
}

server/src/api.rs

0 → 100644
+43 −0
Original line number Diff line number Diff line
// Copyright (C) 2020 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use crate::rooms::{self, Rooms};

use actix::Addr;
use actix_web::{web, HttpResponse};

use log::{error, trace};

use webrtc_audio_publishing::api;

pub async fn rooms(rooms: web::Data<Addr<Rooms>>) -> Result<HttpResponse, actix_web::Error> {
    let room_addrs = rooms.send(rooms::ListRoomsMessage).await.map_err(|err| {
        error!("Failed to list rooms: {}", err);
        HttpResponse::InternalServerError()
    })?;

    let mut room_descs = Vec::with_capacity(room_addrs.len());
    for room in room_addrs {
        let room_desc = match room.send(rooms::RoomInformationMessage).await {
            Err(err) => {
                error!(
                    "Failed to retrieve room information for {:?}: {:?}",
                    room, err
                );
                continue;
            }
            Ok(desc) => desc,
        };

        room_descs.push(api::Room {
            id: room_desc.id.0,
            name: room_desc.name.clone(),
            description: room_desc.description.clone(),
        });
    }

    trace!("Returning room descriptions {:?}", room_descs);

    Ok(HttpResponse::Ok().json(api::Rooms(room_descs)))
}
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
//
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

mod api;
mod config;
mod publisher;
mod rooms;
+4 −2
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
//
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use crate::api;
use crate::config::Config;
use crate::publisher::Publisher;
use crate::rooms::Rooms;
@@ -9,7 +10,7 @@ use crate::subscriber::Subscriber;

use actix::{Actor, Addr};
use actix_files::NamedFile;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use actix_web_actors::ws;

use log::error;
@@ -30,7 +31,7 @@ async fn ws(
    path: web::Path<String>,
    req: HttpRequest,
    stream: web::Payload,
) -> impl Responder {
) -> Result<HttpResponse, actix_web::Error> {
    match path.as_str() {
        "publish" => {
            let publisher = Publisher::new(
@@ -90,6 +91,7 @@ pub async fn run(cfg: Config) -> Result<(), anyhow::Error> {
            .route("/", web::get().to(index))
            .route("/ws/{mode:(publish|subscribe)}", web::get().to(ws))
            .route("/static/{filename:.*}", web::get().to(static_file))
            .route("/api/rooms", web::get().to(api::rooms))
    });

    let server = if cfg.use_tls {
+3 −2
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ use actix::{Actor, Addr, Handler, Message, StreamHandler, WeakAddr};
use actix_web::dev::ConnectionInfo;
use actix_web_actors::ws;

use log::{debug, trace};
use log::{debug, trace, warn};

/// Actor that represents a WebRTC subscriber.
#[derive(Debug)]
@@ -59,7 +59,8 @@ impl Actor for Subscriber {
}

impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Subscriber {
    fn handle(&mut self, _msg: Result<ws::Message, ws::ProtocolError>, _ctx: &mut Self::Context) {
    fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, _ctx: &mut Self::Context) {
        warn!("received {:?}", msg);
        // TODO
    }
}
Loading