mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-02 08:28:18 +00:00
aichatbot: handler for current level
This commit is contained in:
1 parent
7566c0d71c
commit
ac5bcbdd16
4 files changed
+64
No files matched your search
@@ -0,0 +1,60 @@
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::{HttpRequest, HttpResponse, ResponseError, web};
|
||||
use crate::shared_state::app_control_state::AppControlState;
|
||||
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
|
||||
use log::error;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct GetCurrentLevelResponse {
|
||||
pub success: bool,
|
||||
pub current_level: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub enum GetCurrentLevelError {
|
||||
ServerError,
|
||||
}
|
||||
|
||||
impl ResponseError for GetCurrentLevelError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
GetCurrentLevelError::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 GetCurrentLevelError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_current_level_handler(
|
||||
_http_request: HttpRequest,
|
||||
control_state: web::Data<Arc<AppControlState>>
|
||||
) -> Result<HttpResponse, GetCurrentLevelError> {
|
||||
|
||||
let current_level = control_state.get_level()
|
||||
.map_err(|err| {
|
||||
error!("Error: {:?}", err);
|
||||
GetCurrentLevelError::ServerError
|
||||
})?;
|
||||
|
||||
let response = GetCurrentLevelResponse {
|
||||
success: true,
|
||||
current_level: current_level.tts_model_token_str().to_string(),
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| GetCurrentLevelError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod get_current_level;
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod level;
|
||||
pub mod misc;
|
||||
pub mod news_stories;
|
||||
@@ -12,6 +12,7 @@ use errors::AnyhowResult;
|
||||
use http_server_common::endpoints::root_index::get_root_index;
|
||||
use std::sync::Arc;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use crate::web_server::handlers::level::get_current_level::get_current_level_handler;
|
||||
use crate::web_server::handlers::news_stories::get_next_news_story_audio_file_handler::get_next_news_story_audio_file_handler;
|
||||
|
||||
pub struct LaunchWebServerArgs {
|
||||
@@ -45,6 +46,7 @@ pub async fn launch_web_server(args: LaunchWebServerArgs) -> AnyhowResult<()> {
|
||||
.add_get("/openai", openai_inference_handler)
|
||||
.add_get("/news_story/next_story", get_next_news_story_handler)
|
||||
.add_get("/news_story/audio", get_next_news_story_audio_file_handler)
|
||||
.add_get("/current_level", get_current_level_handler)
|
||||
.into_app();
|
||||
|
||||
/*app.service(
|
||||
|
||||
Reference in new issue
Block a user