Skip to content

Commit

Permalink
rust: emit progress events through the websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 23, 2024
1 parent 26e2e3a commit 4e902d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 6 additions & 3 deletions rust/agama-dbus-server/src/agama-web-server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use agama_dbus_server::web;
use agama_dbus_server::web::{self, run_monitor};
use agama_lib::connection;
use clap::{Parser, Subcommand};
use tokio::sync::broadcast::channel;
use tracing_subscriber::prelude::*;
use utoipa::OpenApi;

Expand Down Expand Up @@ -35,9 +36,11 @@ async fn serve_command(address: &str) {
.await
.unwrap_or_else(|_| panic!("could not listen on {}", address));

let dbus_connection = connection().await.unwrap();
let (tx, _) = channel(16);
run_monitor(tx.clone()).await;

let config = web::ServiceConfig::load().unwrap();
let service = web::service(config, dbus_connection);
let service = web::service(config, tx);
axum::serve(listener, service)
.await
.expect("could not mount app on listener");
Expand Down
17 changes: 14 additions & 3 deletions rust/agama-dbus-server/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod service;
mod state;
mod ws;

use agama_lib::{connection, progress::ProgressMonitor};
pub use auth::generate_token;
pub use config::ServiceConfig;
pub use docs::ApiDoc;
Expand All @@ -24,14 +25,24 @@ use axum::Router;
use service::MainServiceBuilder;
use tokio::sync::broadcast::channel;

use self::progress::EventsProgressPresenter;

/// Returns a service that implements the web-based Agama API.
///
/// * `config`: service configuration.
/// * `dbus`: D-Bus connection.
pub fn service(config: ServiceConfig, _dbus: zbus::Connection) -> Router {
let (tx, _) = channel(16);
MainServiceBuilder::new(tx.clone())
pub fn service(config: ServiceConfig, events: EventsSender) -> Router {
MainServiceBuilder::new(events)
.add_service("/l10n", l10n_service(tx.clone()))
.with_config(config)
.build()
}

pub async fn run_monitor(events: EventsSender) {
let presenter = EventsProgressPresenter::new(events);
let connection = connection().await.unwrap();
let mut monitor = ProgressMonitor::new(connection).await.unwrap();
tokio::spawn(async move {
_ = monitor.run(presenter).await;
});
}

0 comments on commit 4e902d7

Please sign in to comment.