Skip to content

Commit

Permalink
Added long polling to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
CEbbinghaus committed Oct 25, 2023
1 parent ddca40d commit 8676f96
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
11 changes: 10 additions & 1 deletion backend/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use crate::{
use actix_web::{delete, get, post, web, HttpResponse, Responder, Result};
use serde::Deserialize;
use std::{ops::Deref, sync::Arc};
use tokio::sync::broadcast::Sender;

pub(crate) fn config(cfg: &mut web::ServiceConfig) {
cfg.service(get_current_card)
cfg.service(listen)
.service(get_current_card)
.service(get_current_card_id)
.service(get_current_card_and_games)
.service(create_card)
Expand All @@ -35,6 +37,13 @@ pub(crate) async fn health() -> impl Responder {
HttpResponse::Ok()
}

#[get("/listen")]
pub(crate) async fn listen(sender: web::Data<Sender<()>>) -> Result<impl Responder> {
sender.subscribe().recv().await.map_err(|_| Error::from_str("Unable to retrieve update"))?;
Ok(HttpResponse::Ok())
}


#[get("/ListCardsWithGames")]
pub(crate) async fn list_cards_with_games(datastore: web::Data<Arc<Store>>) -> impl Responder {
web::Json(datastore.list_cards_with_games())
Expand Down
11 changes: 8 additions & 3 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use err::Error;
use futures::{pin_mut, select, FutureExt};
use once_cell::sync::Lazy;
use simplelog::LevelFilter;
use tokio::sync::broadcast::{self, Sender};
use std::path::PathBuf;
use std::process::exit;
use std::sync::Arc;
Expand All @@ -39,7 +40,7 @@ pub fn init() -> Result<(), ::log::SetLoggerError> {

type MainResult = Result<(), Error>;

async fn run_server(datastore: Arc<Store>) -> MainResult {
async fn run_server(datastore: Arc<Store>, sender: Sender<()>) -> MainResult {
// let log_filepath = format!("/tmp/{}.log", PACKAGE_NAME);
// WriteLogger::init(
// #[cfg(debug_assertions)]
Expand All @@ -58,6 +59,7 @@ async fn run_server(datastore: Arc<Store>) -> MainResult {
info!("Starting HTTP server...");

HttpServer::new(move || {

let cors = Cors::default()
.allow_any_header()
.allow_any_method()
Expand All @@ -68,6 +70,7 @@ async fn run_server(datastore: Arc<Store>) -> MainResult {
.wrap(cors)
// .app_data(web::Data::new(api::AppState{datastore: datastore.clone()}))
.app_data(web::Data::new(datastore.clone()))
.app_data(web::Data::new(sender.clone()))
.configure(config)
})
.workers(1)
Expand Down Expand Up @@ -119,9 +122,11 @@ async fn main() {

info!("Starting Program...");

let server_future = run_server(store.clone()).fuse();
let (txtx, _) = broadcast::channel::<()>(1);

let server_future = run_server(store.clone(), txtx.clone()).fuse();

let watch_future = start_watch(store.clone()).fuse();
let watch_future = start_watch(store.clone(), txtx.clone()).fuse();

pin_mut!(server_future, watch_future);

Expand Down
5 changes: 4 additions & 1 deletion backend/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fs::DirEntry;
use std::hash::{Hash, Hasher};
use std::{borrow::Borrow, collections::HashMap, fs, sync::Arc, time::Duration};
use tokio::time::interval;
use tokio::sync::broadcast::Sender;

const STEAM_LIB_FILE: &'static str = "/run/media/mmcblk0p1/libraryfolder.vdf";
const STEAM_LIB_FOLDER: &'static str = "/run/media/mmcblk0p1/steamapps/";
Expand Down Expand Up @@ -120,7 +121,7 @@ fn read_msd_directory(datastore: &Store) -> Result<(), Error> {
Ok(())
}

pub async fn start_watch(datastore: Arc<Store>) -> Result<(), Error> {
pub async fn start_watch(datastore: Arc<Store>, sender: Sender<()>) -> Result<(), Error> {
let mut interval = interval(Duration::from_secs(5));

let mut changeset = ChangeSet::new();
Expand Down Expand Up @@ -162,5 +163,7 @@ pub async fn start_watch(datastore: Arc<Store>) -> Result<(), Error> {

// commit update
changeset.update(&cid, hash);

let _ = sender.send(());
}
}

0 comments on commit 8676f96

Please sign in to comment.