mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-19 08:05:20 +00:00
refactor sidecar, save dir
This commit is contained in:
1 parent
63bb0e7253
commit
74c289b338
15 files changed
+118
-53
No files matched your search
Generated
+19
@@ -760,6 +760,7 @@ dependencies = [
|
||||
"imgui-glium-renderer",
|
||||
"imgui-winit-support",
|
||||
"log",
|
||||
"path-absolutize",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
@@ -4705,6 +4706,24 @@ version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5"
|
||||
|
||||
[[package]]
|
||||
name = "path-absolutize"
|
||||
version = "3.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f1d4993b16f7325d90c18c3c6a3327db7808752db8d208cea0acee0abd52c52"
|
||||
dependencies = [
|
||||
"path-dedot",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "path-dedot"
|
||||
version = "3.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a81540d94551664b72b72829b12bd167c73c9d25fbac0e04fafa8023f7e4901"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pem-rfc7468"
|
||||
version = "0.3.1"
|
||||
|
||||
@@ -24,6 +24,7 @@ clap = "2.34.0" # TODO: This is really out of date, but has conflicts when updat
|
||||
easyenv = "0.0.6"
|
||||
log = "0.4.14"
|
||||
dotenv = "0.15.0"
|
||||
path-absolutize = "3.0.14" # Absolute paths have not stabilized in Rust yet
|
||||
|
||||
# Async Runtime
|
||||
tokio = { version = "1.6.0", features = ["macros"] }
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
use eframe::egui;
|
||||
use log::{error, info};
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
|
||||
/// Initial state for the GUI
|
||||
pub struct AppGuiArgs {
|
||||
@@ -9,7 +9,7 @@ pub struct AppGuiArgs {
|
||||
pub save_directory: String,
|
||||
|
||||
/// Shared state with the HTTP server and workers.
|
||||
pub control_state: Arc<ControlState>,
|
||||
pub control_state: Arc<AppControlState>,
|
||||
}
|
||||
|
||||
pub struct AppGui {
|
||||
@@ -20,7 +20,7 @@ pub struct AppGui {
|
||||
is_paused: bool,
|
||||
|
||||
/// Shared state with the HTTP server and workers.
|
||||
control_state: Arc<ControlState>,
|
||||
control_state: Arc<AppControlState>,
|
||||
}
|
||||
|
||||
impl AppGui {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::gui::gui::{AppGui, AppGuiArgs};
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
use crate::startup_args::StartupArgs;
|
||||
use eframe::egui;
|
||||
use errors::AnyhowResult;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn launch_gui(startup_args: StartupArgs, control_state: Arc<ControlState>) -> AnyhowResult<()> {
|
||||
pub fn launch_gui(startup_args: StartupArgs, control_state: Arc<AppControlState>) -> AnyhowResult<()> {
|
||||
// Log to stdout (if you run with `RUST_LOG=debug`).
|
||||
//tracing_subscriber::fmt::init();
|
||||
|
||||
|
||||
@@ -17,15 +17,16 @@ use async_openai::Client;
|
||||
use clap::{App, Arg};
|
||||
use crate::gui::launch_gui::launch_gui;
|
||||
use crate::main_loop::main_loop;
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
use crate::startup_args::get_startup_args;
|
||||
use crate::web_server::launch_web_server::launch_web_server;
|
||||
use crate::web_server::launch_web_server::{launch_web_server, LaunchWebServerArgs};
|
||||
use errors::AnyhowResult;
|
||||
use log::info;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use tokio::runtime::Runtime;
|
||||
use crate::persistence::save_directory::SaveDirectory;
|
||||
|
||||
#[actix_web::main]
|
||||
pub async fn main() -> AnyhowResult<()> {
|
||||
@@ -37,14 +38,15 @@ pub async fn main() -> AnyhowResult<()> {
|
||||
|
||||
let startup_args = get_startup_args()?;
|
||||
|
||||
let control_state = Arc::new(ControlState::new());
|
||||
let app_control_state = Arc::new(AppControlState::new());
|
||||
|
||||
let openai_client = Arc::new(Client::new()
|
||||
.with_api_key(startup_args.openai_secret_key.clone()));
|
||||
|
||||
|
||||
let tokio_runtime = Runtime::new()?;
|
||||
|
||||
let save_directory = SaveDirectory::new(&startup_args.save_directory);
|
||||
|
||||
info!("Starting async processes...");
|
||||
|
||||
tokio_runtime.spawn(async {
|
||||
@@ -53,20 +55,21 @@ pub async fn main() -> AnyhowResult<()> {
|
||||
|
||||
info!("Starting web server...");
|
||||
|
||||
let control_state2 = control_state.clone();
|
||||
let app_control_state2 = app_control_state.clone();
|
||||
let openai_client2 = openai_client.clone();
|
||||
|
||||
thread::spawn(move || {
|
||||
let server_future = launch_web_server(
|
||||
control_state2,
|
||||
openai_client2,
|
||||
);
|
||||
let server_future = launch_web_server(LaunchWebServerArgs {
|
||||
app_control_state: app_control_state2,
|
||||
openai_client: openai_client2,
|
||||
save_directory,
|
||||
});
|
||||
actix_web::rt::System::new().block_on(server_future)
|
||||
});
|
||||
|
||||
info!("Starting GUI ...");
|
||||
|
||||
let _r = launch_gui(startup_args.clone(), control_state.clone());
|
||||
let _r = launch_gui(startup_args.clone(), app_control_state.clone());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,8 +1,21 @@
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SaveDirectory {
|
||||
directory: PathBuf,
|
||||
}
|
||||
|
||||
impl SaveDirectory {
|
||||
|
||||
pub fn new<P: AsRef<Path>>(directory: P) -> Self {
|
||||
Self {
|
||||
directory: directory.as_ref().to_path_buf()
|
||||
}
|
||||
}
|
||||
|
||||
/// This is just the first directory structure, which is sequential audio files.
|
||||
/// We'll be using a database and well-formed filesystem layout later.
|
||||
pub fn get_audio_files_dir_v1(&self) -> PathBuf {
|
||||
self.directory.join("./audio_files")
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -1,16 +1,17 @@
|
||||
use std::sync::{Arc, LockResult, RwLock};
|
||||
use errors::{anyhow, AnyhowResult};
|
||||
|
||||
pub struct ControlState {
|
||||
/// User-controlled parameters that determine how the app behaves at runtime.
|
||||
pub struct AppControlState {
|
||||
/// Whether the playback should be paused
|
||||
/// Unreal Engine will get this over HTTP and know whether to no-op.
|
||||
is_paused: Arc<RwLock<bool>>,
|
||||
}
|
||||
|
||||
impl ControlState {
|
||||
impl AppControlState {
|
||||
|
||||
pub fn new() -> Self {
|
||||
ControlState {
|
||||
AppControlState {
|
||||
is_paused: Arc::new(RwLock::new(false)),
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
pub mod control_state;
|
||||
pub mod app_control_state;
|
||||
pub mod job_state;
|
||||
@@ -1,5 +1,8 @@
|
||||
use clap::{App, Arg};
|
||||
use errors::AnyhowResult;
|
||||
use std::path::PathBuf;
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
use log::info;
|
||||
use path_absolutize::Absolutize;
|
||||
use errors::{anyhow, AnyhowResult};
|
||||
|
||||
const DEFAULT_SAVE_DIRECTORY : &'static str = "./data";
|
||||
|
||||
@@ -23,25 +26,43 @@ pub fn get_startup_args() -> AnyhowResult<StartupArgs> {
|
||||
.required(false))
|
||||
.get_matches();
|
||||
|
||||
let mut maybe_save_directory = matches.value_of("directory")
|
||||
.map(|val| val.trim().to_string());
|
||||
|
||||
if maybe_save_directory.is_none() {
|
||||
maybe_save_directory = std::env::current_dir()
|
||||
.ok()
|
||||
.map(|pathbuf| pathbuf.join(DEFAULT_SAVE_DIRECTORY))
|
||||
.map(|pathbuf| std::fs::canonicalize(pathbuf).ok())
|
||||
.flatten()
|
||||
.map(|pathbuf| pathbuf.to_str().map(|s| s.to_string()))
|
||||
.flatten();
|
||||
}
|
||||
|
||||
let openai_secret_key = easyenv::get_env_string_required("OPENAI_SECRET_KEY")?;
|
||||
|
||||
let save_directory = maybe_save_directory.unwrap_or_else(|| DEFAULT_SAVE_DIRECTORY.to_string());
|
||||
let save_directory = get_save_directory(&matches)?;
|
||||
|
||||
Ok(StartupArgs {
|
||||
save_directory,
|
||||
openai_secret_key,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fn get_save_directory(matches: &ArgMatches) -> AnyhowResult<String> {
|
||||
let mut maybe_save_directory = matches.value_of("directory")
|
||||
.map(|val| val.trim().to_string())
|
||||
.map(|val| PathBuf::from(val));
|
||||
|
||||
if maybe_save_directory.is_none() {
|
||||
// Use the current directory
|
||||
maybe_save_directory = std::env::current_dir()
|
||||
.ok()
|
||||
.map(|pathbuf| pathbuf.join(DEFAULT_SAVE_DIRECTORY))
|
||||
.map(|pathbuf| std::fs::canonicalize(pathbuf).ok())
|
||||
.flatten();
|
||||
}
|
||||
|
||||
let save_directory = maybe_save_directory
|
||||
.unwrap_or_else(|| PathBuf::from(DEFAULT_SAVE_DIRECTORY));
|
||||
|
||||
info!("Possible save directory: {:?}", save_directory);
|
||||
|
||||
let save_directory= std::fs::canonicalize(save_directory)
|
||||
.ok()
|
||||
.map(|pathbuf| pathbuf.absolutize().ok().map(|inner| inner.to_path_buf()))
|
||||
.flatten()
|
||||
.map(|pathbuf| pathbuf.to_str().map(|s| s.to_string()))
|
||||
.flatten()
|
||||
.ok_or(anyhow!("could not construct path"))?;
|
||||
|
||||
Ok(save_directory)
|
||||
}
|
||||
+2
-2
@@ -4,7 +4,7 @@ use actix_web::{HttpRequest, HttpResponse, ResponseError, web};
|
||||
use actix_web::web::Query;
|
||||
use log::{error, info};
|
||||
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GetNextAudioQuery {
|
||||
@@ -44,7 +44,7 @@ impl std::fmt::Display for GetNextAudioFileError {
|
||||
|
||||
pub async fn get_next_audio_file_handler(
|
||||
_http_request: HttpRequest,
|
||||
control_state: web::Data<Arc<ControlState>>,
|
||||
control_state: web::Data<Arc<AppControlState>>,
|
||||
request: Query<GetNextAudioQuery>,
|
||||
) -> Result<HttpResponse, GetNextAudioFileError> {
|
||||
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ use actix_http::StatusCode;
|
||||
use actix_web::{HttpRequest, HttpResponse, ResponseError, web};
|
||||
use log::error;
|
||||
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct NextAudioFileResponse {
|
||||
@@ -37,7 +37,7 @@ impl std::fmt::Display for NextAudioFileError {
|
||||
|
||||
pub async fn next_audio_file_handler(
|
||||
_http_request: HttpRequest,
|
||||
control_state: web::Data<Arc<ControlState>>
|
||||
control_state: web::Data<Arc<AppControlState>>
|
||||
) -> Result<HttpResponse, NextAudioFileError> {
|
||||
|
||||
let is_paused = control_state.is_paused()
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ use async_openai::Client;
|
||||
use async_openai::types::CreateCompletionRequestArgs;
|
||||
use log::{error, info};
|
||||
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct OpenAiInferenceResponse {
|
||||
@@ -39,7 +39,7 @@ impl std::fmt::Display for OpenAiInferenceError {
|
||||
|
||||
pub async fn openai_inference_handler(
|
||||
_http_request: HttpRequest,
|
||||
control_state: web::Data<Arc<ControlState>>,
|
||||
control_state: web::Data<Arc<AppControlState>>,
|
||||
openai_client: web::Data<Arc<Client>>,
|
||||
) -> Result<HttpResponse, OpenAiInferenceError> {
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use actix_helpers::route_builder::RouteBuilder;
|
||||
use actix_web::{App, HttpResponse, HttpServer, web};
|
||||
use async_openai::Client;
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::persistence::save_directory::SaveDirectory;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
use crate::web_server::handlers::get_next_audio_file_handler::get_next_audio_file_handler;
|
||||
use crate::web_server::handlers::next_audio_file_handler::next_audio_file_handler;
|
||||
use crate::web_server::handlers::openai_inference_handler::openai_inference_handler;
|
||||
@@ -10,20 +11,24 @@ use errors::AnyhowResult;
|
||||
use http_server_common::endpoints::root_index::get_root_index;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn launch_web_server(
|
||||
control_state: Arc<ControlState>,
|
||||
openai_client: Arc<Client>
|
||||
) -> AnyhowResult<()> {
|
||||
pub struct LaunchWebServerArgs {
|
||||
pub app_control_state: Arc<AppControlState>,
|
||||
pub openai_client: Arc<Client>,
|
||||
pub save_directory: SaveDirectory,
|
||||
}
|
||||
|
||||
pub async fn launch_web_server(args: LaunchWebServerArgs) -> AnyhowResult<()> {
|
||||
|
||||
let server_state = Arc::new(ServerState {
|
||||
control_state: control_state.clone(),
|
||||
openai_client: openai_client.clone(),
|
||||
app_control_state: args.app_control_state.clone(),
|
||||
openai_client: args.openai_client.clone(),
|
||||
save_directory: args.save_directory,
|
||||
});
|
||||
|
||||
HttpServer::new(move || {
|
||||
let app = App::new()
|
||||
.app_data(web::Data::new(control_state.clone()))
|
||||
.app_data(web::Data::new(openai_client.clone()))
|
||||
.app_data(web::Data::new(args.app_control_state.clone()))
|
||||
.app_data(web::Data::new(args.openai_client.clone()))
|
||||
.app_data(web::Data::new(server_state.clone()));
|
||||
|
||||
let mut route_builder = RouteBuilder::from_app(app);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use async_openai::Client;
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
use crate::persistence::save_directory::SaveDirectory;
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ServerState {
|
||||
pub control_state: Arc<ControlState>,
|
||||
pub app_control_state: Arc<AppControlState>,
|
||||
pub openai_client: Arc<Client>,
|
||||
pub save_directory: SaveDirectory,
|
||||
}
|
||||
Whitespace-only changes.
Reference in new issue
Block a user