This commit is contained in:
Mateusz Gruszczyński
2026-08-30 13:39:29 +02:00
parent 3e950ab5fa
commit 5c05eddb8f
83 changed files with 10130 additions and 9954 deletions
+61
View File
@@ -0,0 +1,61 @@
async fn get_debug(State(state): State<AppState>) -> Json<DebugSettings> {
Json(state.settings.read().await.debug.clone())
}
async fn update_debug(State(state): State<AppState>, Json(input): Json<DebugSettings>) -> Result<Json<DebugSettings>, AppError> {
let mut settings = state.settings.write().await;
settings.debug = input.clone();
state.db.save_runtime_settings(&settings)?;
state.debug_gree_frames.store(input.gree_frames, Ordering::Relaxed);
state.broadcast("debug.settings", serde_json::to_value(&input)?);
Ok(Json(input))
}
#[derive(Debug, Deserialize)]
struct CreateAccessTokenRequest {
name: Option<String>,
}
async fn list_access_tokens(State(state): State<AppState>) -> Result<Json<Vec<ApiTokenInfo>>, AppError> {
Ok(Json(state.db.list_api_tokens()?))
}
async fn create_access_token(
State(state): State<AppState>,
Json(input): Json<CreateAccessTokenRequest>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let name = input.name.unwrap_or_else(|| "Home Assistant".into()).trim().to_string();
if name.is_empty() || name.len() > 80 {
return Err(AppError::BadRequest("token name must contain 1 to 80 characters".into()));
}
let secret = generate_access_token();
let item = ApiTokenInfo {
id: Uuid::new_v4().to_string(),
name,
token_prefix: format!("{}...", secret.chars().take(24).collect::<String>()),
created_at: Utc::now(),
};
state.db.save_api_token(&item, &hash_token(&secret))?;
state.log(
"info",
"access_token.created",
"Created a Home Assistant access token",
json!({"token_id": item.id.clone(), "name": item.name.clone()}),
);
Ok((StatusCode::CREATED, Json(json!({"token": secret, "item": item}))))
}
async fn delete_access_token(State(state): State<AppState>, Path(id): Path<String>) -> Result<StatusCode, AppError> {
if !state.db.delete_api_token(&id)? {
return Err(AppError::NotFound(format!("access token {id}")));
}
state.log(
"info",
"access_token.revoked",
"Revoked a Home Assistant access token",
json!({"token_id": id}),
);
Ok(StatusCode::NO_CONTENT)
}