egui wants to run on main thread; so does actix. ugh

This commit is contained in:
Brandon Thomas committed 2023-02-02 04:38:43 -05:00
1 parent 7ba765b89c
commit 2cd6a98259
8 files changed
+159 -8

No files matched your search

Generated
+4
View File
@@ -726,7 +726,11 @@ dependencies = [
"eframe",
"egui",
"errors",
"http_server_common",
"log",
"serde",
"serde_derive",
"serde_json",
"tokio 1.16.1",
]
@@ -16,6 +16,7 @@ path = "src/main.rs"
# Internal
errors = { path = "../../lib/errors" }
http_server_common = { path = "../../lib/http_server_common" }
# External
clap = "2.34.0" # TODO: This is really out of date, but has conflicts when updated
@@ -39,3 +40,8 @@ actix-service = "2.0.1"
actix-utils = "3.0.0"
actix-web = "4.0.0-beta.10"
actix-web-actors = "4.0.0-beta.7"
# Serialization
serde = "1.0.125"
serde_derive = "1.0.125"
serde_json = "1.0.64"
@@ -1,4 +1,5 @@
use eframe::egui;
use log::info;
pub struct AppGui {
filename: String,
+72 -4
View File
@@ -8,14 +8,24 @@ pub mod main_loop;
pub mod state;
pub mod web_server;
#[macro_use] extern crate serde_derive;
use std::thread;
use std::time::Duration;
use tokio::runtime::Runtime;
//use actix_rt::Runtime;
use actix_web::{HttpResponse, HttpServer, web};
use clap::{App, Arg};
use crate::main_loop::main_loop;
use errors::AnyhowResult;
use log::info;
use crate::gui::launch_gui::launch_gui;
use crate::web_server::get_next_audio_file_handler::get_next_audio_file_handler;
use crate::web_server::launch_web_server::launch_web_server;
#[tokio::main]
//#[tokio::main]
#[actix_web::main]
pub async fn main() -> AnyhowResult<()> {
easyenv::init_all_with_default_logging(Some("info"));
@@ -34,11 +44,69 @@ pub async fn main() -> AnyhowResult<()> {
// .takes_value(true))
// .get_matches();
info!("starting main loop...");
let _r = launch_gui()?;
info!("Get runtime...");
let _r = main_loop().await?;
let tokio_runtime = Runtime::new()?;
//info!("Starting main loop...");
tokio_runtime.spawn(async {
info!("STARTING MAIN LOOP...");
let _r = main_loop().await;
});
info!("Launching GUI...");
thread::spawn(|| {
info!("LAUNCHING GUI...");
let _r = launch_gui();
});
//tokio_runtime.spawn(async {
// info!("LAUNCHING GUI...");
// let _r = launch_gui();
//});
info!("Starting web server...");
//tokio_runtime.spawn(async move {
// info!("LAUNCHING WEBSERVER...");
// //let _r = launch_gui();
// let _r = launch_web_server().await;
//});
//thread::spawn(move || {
// info!("LAUNCHING WEBSERVER...");
// //tokio::runtime::Runtime::new().unwrap().block_on(async {
// tokio_runtime.block_on(async {
// let _r = launch_web_server().await;
// });
//});
// HttpServer::new(move || {
// let app = actix_web::App::new();
// //.app_data(web::Data::new(server_state_arc.firehose_publisher.clone()))
//
// app.service(
// web::resource("/next")
// .route(web::get().to(get_next_audio_file_handler))
// .route(web::head().to(|| HttpResponse::Ok()))
// )
// })
// .bind("localhost:23333")?
// .workers(8)
// .run()
// .await?;
let _r = launch_web_server().await;
loop {
info!("Sleep");
thread::sleep(Duration::from_secs(5));
}
Ok(())
}
@@ -8,6 +8,4 @@ pub async fn main_loop() -> AnyhowResult<()> {
info!("iter");
thread::sleep(Duration::from_millis(5000));
}
Ok(())
}
}
@@ -0,0 +1,51 @@
use actix_http::StatusCode;
use actix_web::{HttpRequest, HttpResponse, ResponseError, web};
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
#[derive(Serialize)]
pub struct GetNextAudioFileResponse {
pub success: bool,
pub is_paused: bool,
}
#[derive(Debug, Serialize)]
pub enum GetNextAudioFileError {
ServerError,
}
impl ResponseError for GetNextAudioFileError {
fn status_code(&self) -> StatusCode {
match *self {
GetNextAudioFileError::ServerError => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> HttpResponse {
serialize_as_json_error(self)
}
}
// NB: Not using derive_more::Display since Clion doesn't understand it.
impl std::fmt::Display for GetNextAudioFileError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
pub async fn get_next_audio_file_handler(
_http_request: HttpRequest,
//_server_state: web::Data<Arc<ServerState>>
) -> Result<HttpResponse, GetNextAudioFileError> {
let response = GetNextAudioFileResponse {
success: true,
is_paused: false,
};
let body = serde_json::to_string(&response)
.map_err(|e| GetNextAudioFileError::ServerError)?;
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(body))
}
@@ -0,0 +1,22 @@
use actix_web::{App, HttpResponse, HttpServer, web};
use crate::web_server::get_next_audio_file_handler::get_next_audio_file_handler;
use errors::AnyhowResult;
pub async fn launch_web_server() -> AnyhowResult<()> {
HttpServer::new(move || {
let app = App::new();
//.app_data(web::Data::new(server_state_arc.firehose_publisher.clone()))
app.service(
web::resource("/next")
.route(web::get().to(get_next_audio_file_handler))
.route(web::head().to(|| HttpResponse::Ok()))
)
})
.bind("localhost:23333")?
.workers(8)
.run()
.await?;
Ok(())
}
@@ -1 +1,2 @@
pub mod get_next_audio_file_handler;
pub mod get_next_audio_file_handler;
pub mod launch_web_server;