mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-02 00:18:17 +00:00
better response type typing, less boilerplate
This commit is contained in:
1 parent
705013f5e0
commit
7686eff0c1
5 files changed
+23
-54
No files matched your search
+4
-11
@@ -4,7 +4,7 @@ use std::sync::Arc;
|
||||
use actix_web::{HttpRequest, HttpResponse, web};
|
||||
use actix_web::error::ResponseError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::web::Path;
|
||||
use actix_web::web::{Json, Path};
|
||||
use log::{error, warn};
|
||||
use r2d2_redis::redis::transaction;
|
||||
use sqlx::Acquire;
|
||||
@@ -100,7 +100,7 @@ pub async fn get_is_featured_item_handler(
|
||||
http_request: HttpRequest,
|
||||
path: Path<GetIsFeaturedItemPathInfo>,
|
||||
server_state: web::Data<Arc<ServerState>>,
|
||||
) -> Result<HttpResponse, GetIsFeaturedItemError>
|
||||
) -> Result<Json<GetIsFeaturedItemSuccessResponse>, GetIsFeaturedItemError>
|
||||
{
|
||||
// NB(bt,2023-12-14): Kasisnu found that we're getting entity type mismatches in production. Apart from
|
||||
// querying the database for entity existence, this is the next best way to prevent incorrect comment
|
||||
@@ -127,15 +127,8 @@ pub async fn get_is_featured_item_handler(
|
||||
GetIsFeaturedItemError::ServerError
|
||||
})?;
|
||||
|
||||
let response = GetIsFeaturedItemSuccessResponse {
|
||||
Ok(Json(GetIsFeaturedItemSuccessResponse {
|
||||
success: true,
|
||||
is_featured: is_featured.is_featured,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|_e| GetIsFeaturedItemError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}))
|
||||
}
|
||||
+4
-11
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||
|
||||
use actix_web::{HttpRequest, HttpResponse, ResponseError, web};
|
||||
use actix_web::http::StatusCode;
|
||||
|
||||
use actix_web::web::Json;
|
||||
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
|
||||
|
||||
use crate::state::server_state::ServerState;
|
||||
@@ -41,18 +41,11 @@ impl std::fmt::Display for PublicInfoError {
|
||||
pub async fn get_public_info_handler(
|
||||
_http_request: HttpRequest,
|
||||
server_state: web::Data<Arc<ServerState>>
|
||||
) -> Result<HttpResponse, PublicInfoError> {
|
||||
) -> Result<Json<PublicInfoResponse>, PublicInfoError> {
|
||||
|
||||
let response = PublicInfoResponse {
|
||||
Ok(Json(PublicInfoResponse {
|
||||
success: true,
|
||||
server_build_sha: server_state.server_info.build_sha.clone(),
|
||||
server_hostname: server_state.hostname.clone(),
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| PublicInfoError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}))
|
||||
}
|
||||
+4
-10
@@ -3,6 +3,7 @@ use std::time::Duration;
|
||||
|
||||
use actix_web::{HttpRequest, HttpResponse, ResponseError, web};
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::web::Json;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
|
||||
@@ -91,7 +92,7 @@ impl std::fmt::Display for StatusAlertError {
|
||||
pub async fn status_alert_handler(
|
||||
_http_request: HttpRequest,
|
||||
server_state: web::Data<Arc<ServerState>>
|
||||
) -> Result<HttpResponse, StatusAlertError> {
|
||||
) -> Result<Json<StatusAlertResponse>, StatusAlertError> {
|
||||
|
||||
let maybe_category = server_state
|
||||
.flags
|
||||
@@ -114,18 +115,11 @@ pub async fn status_alert_handler(
|
||||
}),
|
||||
};
|
||||
|
||||
let response = StatusAlertResponse {
|
||||
Ok(Json(StatusAlertResponse {
|
||||
success: true,
|
||||
maybe_alert,
|
||||
refresh_interval_millis: REFRESH_INTERVAL.as_millis() as u64,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|e| StatusAlertError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}))
|
||||
}
|
||||
|
||||
fn category_to_enum(category: &str) -> Option<StatusAlertCategory> {
|
||||
|
||||
+5
-11
@@ -9,6 +9,7 @@ use std::sync::Arc;
|
||||
use actix_web::{HttpRequest, HttpResponse, web};
|
||||
use actix_web::error::ResponseError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::web::Json;
|
||||
use log::{info, warn};
|
||||
use r2d2_redis::redis::Commands;
|
||||
use rand::Rng;
|
||||
@@ -150,8 +151,8 @@ impl fmt::Display for InferTtsError {
|
||||
)]
|
||||
pub async fn enqueue_infer_tts_handler(
|
||||
http_request: HttpRequest,
|
||||
request: web::Json<InferTtsRequest>,
|
||||
server_state: web::Data<Arc<ServerState>>) -> Result<HttpResponse, InferTtsError>
|
||||
request: Json<InferTtsRequest>,
|
||||
server_state: web::Data<Arc<ServerState>>) -> Result<Json<InferTtsSuccessResponse>, InferTtsError>
|
||||
{
|
||||
let mut is_from_api = false;
|
||||
let mut maybe_user_token : Option<String> = None;
|
||||
@@ -439,18 +440,11 @@ pub async fn enqueue_infer_tts_handler(
|
||||
InferTtsError::ServerError
|
||||
})?;
|
||||
|
||||
let response = InferTtsSuccessResponse {
|
||||
Ok(Json(InferTtsSuccessResponse {
|
||||
success: true,
|
||||
inference_job_token: job_token,
|
||||
inference_job_token_type: InferenceJobTokenType::Generic,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|_e| InferTtsError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
|
||||
+6
-11
@@ -9,6 +9,7 @@ use std::sync::Arc;
|
||||
use actix_web::{HttpRequest, HttpResponse, web};
|
||||
use actix_web::error::ResponseError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::web::Json;
|
||||
use log::{error, info, warn};
|
||||
use r2d2_redis::redis::Commands;
|
||||
use sqlx::MySql;
|
||||
@@ -150,8 +151,9 @@ impl fmt::Display for EnqueueVoiceConversionInferenceError {
|
||||
)]
|
||||
pub async fn enqueue_voice_conversion_inference_handler(
|
||||
http_request: HttpRequest,
|
||||
request: web::Json<EnqueueVoiceConversionInferenceRequest>,
|
||||
server_state: web::Data<Arc<ServerState>>) -> Result<HttpResponse, EnqueueVoiceConversionInferenceError>
|
||||
request: Json<EnqueueVoiceConversionInferenceRequest>,
|
||||
server_state: web::Data<Arc<ServerState>>
|
||||
) -> Result<Json<EnqueueVoiceConversionInferenceSuccessResponse>, EnqueueVoiceConversionInferenceError>
|
||||
{
|
||||
let mut maybe_user_token : Option<UserToken> = None;
|
||||
let mut priority_level ;
|
||||
@@ -399,17 +401,10 @@ pub async fn enqueue_voice_conversion_inference_handler(
|
||||
EnqueueVoiceConversionInferenceError::ServerError
|
||||
})?;
|
||||
|
||||
let response = EnqueueVoiceConversionInferenceSuccessResponse {
|
||||
Ok(Json(EnqueueVoiceConversionInferenceSuccessResponse {
|
||||
success: true,
|
||||
inference_job_token: job_token,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
.map_err(|_e| EnqueueVoiceConversionInferenceError::ServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body))
|
||||
}))
|
||||
}
|
||||
|
||||
async fn lookup_model_info(
|
||||
|
||||
Reference in new issue
Block a user