91 lines
2.8 KiB
Rust
91 lines
2.8 KiB
Rust
use axum::{
|
|
extract::State,
|
|
http::StatusCode,
|
|
response::Json,
|
|
routing::{get, post, put},
|
|
Router,
|
|
};
|
|
use serde_json::{json, Value};
|
|
use std::sync::Arc;
|
|
|
|
use crate::api::types::AppState;
|
|
use crate::core::admin_config::{PartialAdminConfig, VlmPrompt};
|
|
|
|
pub fn admin_config_routes() -> Router<AppState> {
|
|
Router::new()
|
|
.route("/api/v1/admin/config", get(get_config))
|
|
.route("/api/v1/admin/config", post(update_config))
|
|
.route("/api/v1/admin/config/vlm_prompts", get(get_vlm_prompts))
|
|
.route("/api/v1/admin/config/vlm_prompts/:key", get(get_vlm_prompt))
|
|
.route(
|
|
"/api/v1/admin/config/vlm_prompts/:key",
|
|
put(update_vlm_prompt),
|
|
)
|
|
.route("/api/v1/admin/config/reload", post(reload_config))
|
|
}
|
|
|
|
pub async fn get_config(State(state): State<AppState>) -> Result<Json<Value>, StatusCode> {
|
|
let config = state.admin_config_manager.get_full_config().await;
|
|
Ok(Json(serde_json::to_value(config).unwrap_or_default()))
|
|
}
|
|
|
|
pub async fn update_config(
|
|
State(state): State<AppState>,
|
|
Json(partial): Json<PartialAdminConfig>,
|
|
) -> Result<Json<Value>, StatusCode> {
|
|
match state
|
|
.admin_config_manager
|
|
.update_partial_config(partial)
|
|
.await
|
|
{
|
|
Ok(_) => Ok(Json(json!({
|
|
"success": true,
|
|
"message": "Config updated"
|
|
}))),
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|
|
|
|
pub async fn get_vlm_prompts(State(state): State<AppState>) -> Result<Json<Value>, StatusCode> {
|
|
let prompts = state.admin_config_manager.get_all_vlm_prompts().await;
|
|
Ok(Json(serde_json::to_value(prompts).unwrap_or_default()))
|
|
}
|
|
|
|
pub async fn get_vlm_prompt(
|
|
State(state): State<AppState>,
|
|
axum::extract::Path(key): axum::extract::Path<String>,
|
|
) -> Result<Json<Value>, StatusCode> {
|
|
match state.admin_config_manager.get_vlm_prompt(&key).await {
|
|
Some(prompt) => Ok(Json(serde_json::to_value(prompt).unwrap_or_default())),
|
|
None => Err(StatusCode::NOT_FOUND),
|
|
}
|
|
}
|
|
|
|
pub async fn update_vlm_prompt(
|
|
State(state): State<AppState>,
|
|
axum::extract::Path(key): axum::extract::Path<String>,
|
|
Json(prompt): Json<VlmPrompt>,
|
|
) -> Result<Json<Value>, StatusCode> {
|
|
match state
|
|
.admin_config_manager
|
|
.update_vlm_prompt(&key, prompt)
|
|
.await
|
|
{
|
|
Ok(_) => Ok(Json(json!({
|
|
"success": true,
|
|
"message": format!("Prompt '{}' updated", key)
|
|
}))),
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|
|
|
|
pub async fn reload_config(State(state): State<AppState>) -> Result<Json<Value>, StatusCode> {
|
|
match state.admin_config_manager.reload().await {
|
|
Ok(_) => Ok(Json(json!({
|
|
"success": true,
|
|
"message": "Config reloaded"
|
|
}))),
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|