mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-02 00:18:17 +00:00
cursor
This commit is contained in:
1 parent
4140d07924
commit
63bb0e7253
5 files changed
+81
-5
No files matched your search
@@ -13,6 +13,7 @@ pub mod web_server;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
use actix_web::{HttpResponse, HttpServer, web};
|
||||
use async_openai::Client;
|
||||
use clap::{App, Arg};
|
||||
use crate::gui::launch_gui::launch_gui;
|
||||
use crate::main_loop::main_loop;
|
||||
@@ -24,7 +25,6 @@ use log::info;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use async_openai::Client;
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
#[actix_web::main]
|
||||
|
||||
+15
-2
@@ -1,14 +1,21 @@
|
||||
use std::sync::Arc;
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{HttpRequest, HttpResponse, ResponseError, web};
|
||||
use log::error;
|
||||
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;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GetNextAudioQuery {
|
||||
pub cursor: u64,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct GetNextAudioFileResponse {
|
||||
pub success: bool,
|
||||
pub is_paused: bool,
|
||||
pub next_cursor: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -37,18 +44,24 @@ 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<ControlState>>,
|
||||
request: Query<GetNextAudioQuery>,
|
||||
) -> Result<HttpResponse, GetNextAudioFileError> {
|
||||
|
||||
info!("Requested cursor: {}", request.cursor);
|
||||
|
||||
let is_paused = control_state.is_paused()
|
||||
.map_err(|err| {
|
||||
error!("Error: {:?}", err);
|
||||
GetNextAudioFileError::ServerError
|
||||
})?;
|
||||
|
||||
let next_cursor = request.cursor + 1;
|
||||
|
||||
let response = GetNextAudioFileResponse {
|
||||
success: true,
|
||||
is_paused,
|
||||
next_cursor,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod get_next_audio_file_handler;
|
||||
pub mod index_handler;
|
||||
pub mod next_audio_file_handler;
|
||||
pub mod openai_inference_handler;
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
use std::sync::Arc;
|
||||
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;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct NextAudioFileResponse {
|
||||
pub success: bool,
|
||||
pub is_paused: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub enum NextAudioFileError {
|
||||
ServerError,
|
||||
}
|
||||
|
||||
impl ResponseError for NextAudioFileError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
NextAudioFileError::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 NextAudioFileError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn next_audio_file_handler(
|
||||
_http_request: HttpRequest,
|
||||
control_state: web::Data<Arc<ControlState>>
|
||||
) -> Result<HttpResponse, NextAudioFileError> {
|
||||
|
||||
let is_paused = control_state.is_paused()
|
||||
.map_err(|err| {
|
||||
error!("Error: {:?}", err);
|
||||
NextAudioFileError::ServerError
|
||||
})?;
|
||||
|
||||
let response = NextAudioFileResponse {
|
||||
success: true,
|
||||
is_paused,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| NextAudioFileError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}
|
||||
@@ -3,11 +3,12 @@ use actix_web::{App, HttpResponse, HttpServer, web};
|
||||
use async_openai::Client;
|
||||
use crate::shared_state::control_state::ControlState;
|
||||
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;
|
||||
use crate::web_server::server_state::ServerState;
|
||||
use errors::AnyhowResult;
|
||||
use http_server_common::endpoints::root_index::get_root_index;
|
||||
use std::sync::Arc;
|
||||
use crate::web_server::handlers::openai_inference_handler::openai_inference_handler;
|
||||
|
||||
pub async fn launch_web_server(
|
||||
control_state: Arc<ControlState>,
|
||||
@@ -29,7 +30,8 @@ pub async fn launch_web_server(
|
||||
|
||||
let app = route_builder
|
||||
.add_get("/", get_root_index)
|
||||
.add_get("/next", get_next_audio_file_handler)
|
||||
.add_get("/get_next_audio", get_next_audio_file_handler)
|
||||
.add_post("/next_audio", next_audio_file_handler)
|
||||
.add_get("/openai", openai_inference_handler)
|
||||
.into_app();
|
||||
|
||||
|
||||
Reference in new issue
Block a user