use axum::body::Bytes;
use axum::extract::State;
use axum::http::HeaderMap;
use axum::response::Response;
use axum::{Extension, Json};
use serde_json::{json, Value};
use super::{config_err, parse_config_value, CurrentUser};
use crate::error::ApiError;
use crate::AppState;
const EXPLORER_KEY: &str = "sbolexplorer";
pub async fn update_index(state: State<AppState>) -> Response {
super::reindex(state).await
}
pub async fn get_config(State(state): State<AppState>) -> Result<Json<Value>, ApiError> {
let value = state.app.config_service().get(EXPLORER_KEY).await?;
Ok(Json(value.unwrap_or_else(
|| json!({ "engine": "native", "useSBOLExplorer": false }),
)))
}
pub async fn set_config(
State(state): State<AppState>,
Extension(CurrentUser(user)): Extension<CurrentUser>,
headers: HeaderMap,
body: Bytes,
) -> Result<Json<Value>, ApiError> {
let is_admin = user.as_ref().map(|u| u.is_admin).unwrap_or(false);
let value = parse_config_value(&headers, &body)?;
state
.app
.config_service()
.set(is_admin, EXPLORER_KEY, &value)
.await
.map_err(config_err)?;
Ok(Json(json!({ "status": "ok" })))
}
pub async fn log() -> Json<Value> {
Json(json!({ "engine": "native", "entries": [] }))
}