vl-convert-server 2.0.0-rc2

HTTP server for converting Vega-Lite and Vega specifications to static images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
use arc_swap::ArcSwap;
use axum::extract::rejection::JsonRejection;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
use axum::Router;
use serde::Serialize;
use serde_json::json;
use std::sync::Arc;
use utoipa::OpenApi;
use utoipa_axum::{router::OpenApiRouter, routes};
use utoipa_swagger_ui::SwaggerUi;
use vl_convert_rs::converter::{normalize_converter_config, VlcConfig};

use crate::budget::{BudgetStatus, BudgetTracker};
use crate::config::{ApiKey, RuntimeSnapshot};
use crate::health::ReadinessState;
use crate::reconfig::{
    apply_patch, DrainError, PatchRejection, ReconfigCoordinator, ReconfigScopeGuard,
};
use crate::types::{
    ConfigPatch, ConfigReplace, ConfigValidationError, ConfigView, ErrorResponse, FieldError,
    FieldErrorCode, FontDirRequest,
};

/// OpenAPI doc for the admin surface. Published at `/admin/api-doc/openapi.json`;
/// Swagger UI at `/admin/docs`. The main `/api-doc/openapi.json` does not
/// include `/admin/*` paths.
///
/// Admin body schemas are omitted because the DTOs wrap library types and
/// tri-state patch fields that are not represented as OpenAPI schemas here.
/// The spec publishes paths, methods, tags, and response status codes.
#[derive(OpenApi)]
#[openapi(tags(
    (name = "Admin", description = "Admin-only endpoints (config + budget)"),
))]
struct AdminApiDoc;

/// Composite state for the admin router. One `Arc<AdminState>` is cloned
/// into `.with_state(...)` for admin routes.
///
/// The `runtime`, `coordinator`, and `readiness` handles are shared by
/// Arc identity with `AppState`. Reconfig commits are visible to main
/// handlers, and the main router's gate middleware participates in the
/// same drain domain.
pub(crate) struct AdminState {
    /// Atomic holder for the current converter + config. Shared with
    /// `AppState.runtime` by Arc identity.
    pub runtime: Arc<ArcSwap<RuntimeSnapshot>>,
    /// Snapshot-clone of the resolved startup `VlcConfig`. `DELETE
    /// /admin/config` restores this. Immutable for the process lifetime.
    pub baseline: Arc<VlcConfig>,
    /// Reconfig coordinator shared with the gate middleware. Shared
    /// with `AppState.coordinator` by Arc identity.
    pub coordinator: Arc<ReconfigCoordinator>,
    /// Readiness handle shared with `/readyz` on the main listener.
    /// Shared with `AppState.readiness` by Arc identity.
    pub readiness: Arc<ReadinessState>,
    /// Optional bearer-token credential for admin-scope auth. `None`
    /// leaves access to the listener trust boundary: loopback TCP or UDS
    /// filesystem permissions.
    pub admin_api_key: Option<ApiKey>,
    /// Budget tracker shared with the main router's budget middleware.
    /// `/admin/budget` updates are observed by request admission.
    pub tracker: Arc<BudgetTracker>,
    /// Error-opacity toggle. When `true` admin responses omit internal
    /// detail, matching the main listener's `opaque_errors` setting.
    pub opaque_errors: bool,
}

fn admin_openapi_router() -> OpenApiRouter<Arc<AdminState>> {
    OpenApiRouter::with_openapi(AdminApiDoc::openapi())
        .routes(routes!(get_budget))
        .routes(routes!(update_budget))
        .routes(routes!(get_worker_diagnostics))
        .routes(routes!(get_config))
        .routes(routes!(patch_config))
        .routes(routes!(put_config))
        .routes(routes!(delete_config))
        .routes(routes!(get_font_dirs))
        .routes(routes!(put_font_dirs))
        .routes(routes!(post_font_dir))
        .routes(routes!(get_font_cache_size))
        .routes(routes!(put_font_cache_size))
}

/// OpenAPI document for the admin server surface served at
/// `/admin/api-doc/openapi.json`.
pub fn admin_openapi() -> utoipa::openapi::OpenApi {
    admin_openapi_router().into_openapi()
}

pub(crate) fn admin_router(admin_state: Arc<AdminState>) -> Router {
    let (admin_routes, admin_api) = admin_openapi_router().split_for_parts();

    admin_routes
        // Serve admin docs separately from the public API docs.
        .merge(SwaggerUi::new("/admin/docs").url("/admin/api-doc/openapi.json", admin_api))
        // Admin auth is outermost on the admin router. Every admin
        // request (including GETs + the spec endpoint) passes through it.
        // When `admin_api_key` is `None` the middleware is a no-op;
        // listener placement (UDS or TCP loopback) is the trust boundary
        // in that case.
        .layer(axum::middleware::from_fn_with_state(
            admin_state.clone(),
            crate::middleware::admin_auth_middleware,
        ))
        .with_state(admin_state)
}

#[utoipa::path(
    get,
    path = "/admin/budget",
    responses((status = 200, description = "Current budget status")),
    tag = "Admin",
)]
async fn get_budget(State(admin): State<Arc<AdminState>>) -> Json<BudgetStatus> {
    // Mutations here are observed by the main router's budget middleware.
    Json(admin.tracker.status())
}

#[derive(serde::Deserialize, utoipa::ToSchema)]
struct BudgetUpdate {
    per_ip_budget_ms: Option<i64>,
    global_budget_ms: Option<i64>,
    hold_ms: Option<i64>,
}

#[derive(Serialize, utoipa::ToSchema)]
struct WorkerMemoryUsageView {
    worker_index: usize,
    used_heap_size: usize,
    total_heap_size: usize,
    heap_size_limit: usize,
    external_memory: usize,
}

#[derive(Serialize, utoipa::ToSchema)]
struct WorkerDiagnosticsView {
    generation: u64,
    workers: Vec<WorkerMemoryUsageView>,
}

#[utoipa::path(
    post,
    path = "/admin/budget",
    responses(
        (status = 200, description = "Budget updated; response is fresh BudgetStatus"),
        (status = 400, description = "Invalid update body"),
    ),
    tag = "Admin",
)]
async fn update_budget(
    State(admin): State<Arc<AdminState>>,
    Json(update): Json<BudgetUpdate>,
) -> Response {
    let tracker = &admin.tracker;
    if let Some(est) = update.hold_ms {
        if est <= 0 {
            return (
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: "hold_ms must be positive".to_string(),
                }),
            )
                .into_response();
        }
    }
    if let Some(per_ip) = update.per_ip_budget_ms {
        if per_ip < 0 {
            return (
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: "per_ip_budget_ms must be non-negative".to_string(),
                }),
            )
                .into_response();
        }
    }
    if let Some(global) = update.global_budget_ms {
        if global < 0 {
            return (
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: "global_budget_ms must be non-negative".to_string(),
                }),
            )
                .into_response();
        }
    }

    tracker.update_config(update.per_ip_budget_ms, update.global_budget_ms);
    if let Some(est) = update.hold_ms {
        tracker.update_estimate(est);
    }
    Json(tracker.status()).into_response()
}

#[utoipa::path(
    get,
    path = "/admin/diagnostics/workers",
    responses(
        (status = 200, description = "Worker memory usage"),
        (status = 503, description = "Worker diagnostics unavailable"),
    ),
    tag = "Admin",
)]
async fn get_worker_diagnostics(State(admin): State<Arc<AdminState>>) -> Response {
    let snap = admin.runtime.load_full();
    let generation = snap.generation;
    match snap.converter.get_worker_memory_usage().await {
        Ok(workers) => {
            let workers = workers
                .into_iter()
                .map(|worker| WorkerMemoryUsageView {
                    worker_index: worker.worker_index,
                    used_heap_size: worker.used_heap_size,
                    total_heap_size: worker.total_heap_size,
                    heap_size_limit: worker.heap_size_limit,
                    external_memory: worker.external_memory,
                })
                .collect();
            (
                StatusCode::OK,
                Json(WorkerDiagnosticsView {
                    generation,
                    workers,
                }),
            )
                .into_response()
        }
        Err(err) => simple_error_response(
            StatusCode::SERVICE_UNAVAILABLE,
            &format!("worker diagnostics unavailable: {err}"),
            admin.opaque_errors,
        ),
    }
}

// =============================================================================
// /admin/config handlers
// =============================================================================

/// Build the ConfigView response payload from a snapshot + admin state.
fn build_config_view(snapshot: &RuntimeSnapshot, admin: &AdminState) -> ConfigView {
    ConfigView {
        baseline: (*admin.baseline).clone(),
        effective: (*snapshot.config).clone(),
        generation: snapshot.generation,
    }
}

fn validation_error_from_anyhow(err: &vl_convert_rs::anyhow::Error) -> ConfigValidationError {
    let msg = err.to_string();
    ConfigValidationError {
        error: msg.clone(),
        field_errors: vec![FieldError {
            // Cross-field errors are attributed to the root so the response
            // stays structurally well-formed.
            path: "".to_string(),
            code: FieldErrorCode::CrossFieldInvariant,
            message: msg,
        }],
    }
}

/// Render a ConfigValidationError as a 422 response, respecting `opaque_errors`.
fn validation_error_response(err: ConfigValidationError, opaque: bool) -> Response {
    if opaque {
        // Drop specifics but keep the 422 status so the client can still
        // distinguish validation errors from 5xx failures.
        (StatusCode::UNPROCESSABLE_ENTITY, Json(json!({}))).into_response()
    } else {
        (StatusCode::UNPROCESSABLE_ENTITY, Json(err)).into_response()
    }
}

/// Render a non-nullable-field rejection as a 400 response. Shares the
/// ConfigValidationError payload shape with 422 responses so clients can
/// parse field-level errors uniformly.
fn non_nullable_error_response(err: ConfigValidationError, opaque: bool) -> Response {
    if opaque {
        (StatusCode::BAD_REQUEST, Json(json!({}))).into_response()
    } else {
        (StatusCode::BAD_REQUEST, Json(err)).into_response()
    }
}

fn simple_error_response(status: StatusCode, message: &str, opaque: bool) -> Response {
    if opaque {
        status.into_response()
    } else {
        (
            status,
            Json(ErrorResponse {
                error: message.to_string(),
            }),
        )
            .into_response()
    }
}

/// Convert an axum `JsonRejection` into a 400 response. serde failures for
/// unknown fields, bad types, null-on-non-nullable, or NonZero zero values
/// all funnel through here.
fn json_rejection_response(rej: JsonRejection, opaque: bool) -> Response {
    simple_error_response(StatusCode::BAD_REQUEST, &rej.body_text(), opaque)
}

/// GET /admin/config: returns the baseline, current effective config,
/// and the monotonic generation counter.
#[utoipa::path(
    get,
    path = "/admin/config",
    responses((
        status = 200,
        description = "ConfigView { baseline, effective, generation }. \
                       Schema mirrors the Python get_config() shape."
    )),
    tag = "Admin",
)]
async fn get_config(State(admin): State<Arc<AdminState>>) -> Response {
    let snap = admin.runtime.load_full();
    let view = build_config_view(&snap, &admin);
    (StatusCode::OK, Json(view)).into_response()
}

/// PATCH /admin/config: merge a partial patch onto the current config.
#[utoipa::path(
    patch,
    path = "/admin/config",
    responses(
        (status = 200, description = "Commit succeeded; response is fresh ConfigView"),
        (status = 400, description = "Malformed body / unknown field / null on non-nullable / NonZero zero"),
        (status = 422, description = "Config validation failed; response is ConfigValidationError with field_errors"),
        (status = 503, description = "Rebuild failure OR server shutting down during drain"),
        (status = 504, description = "Drain timed out; response includes in_flight count"),
    ),
    tag = "Admin",
)]
async fn patch_config(
    State(admin): State<Arc<AdminState>>,
    body: Result<Json<ConfigPatch>, JsonRejection>,
) -> Response {
    let Json(patch) = match body {
        Ok(b) => b,
        Err(rej) => return json_rejection_response(rej, admin.opaque_errors),
    };

    let coordinator = admin.coordinator.clone();
    let readiness = admin.readiness.clone();

    // Serialize against other admin-mutating requests. The lock guard is
    // held for the whole flow so PUT / DELETE / POST /fonts/directories
    // commits cannot interleave.
    let _lock_guard = coordinator.lock().await;
    let mut scope = ReconfigScopeGuard::new(&coordinator, &readiness);

    let current = admin.runtime.load_full();

    let new_config = match apply_patch(&current.config, &patch) {
        Ok(c) => c,
        Err(PatchRejection::NonNullable(err)) => {
            // Parse-level rejection: a non-nullable field received explicit
            // `null`. Surface as 400 alongside the other body-shape errors
            // produced by serde (unknown field, NonZero zero, etc.).
            return non_nullable_error_response(err, admin.opaque_errors);
        }
        Err(PatchRejection::Invalid(err)) => {
            return validation_error_response(err, admin.opaque_errors);
        }
    };

    run_commit(&admin, &current, new_config, &mut scope).await
}

/// PUT /admin/config: full replacement.
#[utoipa::path(
    put,
    path = "/admin/config",
    responses(
        (status = 200, description = "Commit succeeded; response is fresh ConfigView"),
        (status = 400, description = "Malformed body / missing required field / unknown field / null on non-nullable / NonZero zero"),
        (status = 422, description = "Config validation failed; response is ConfigValidationError"),
        (status = 503, description = "Rebuild failure OR server shutting down"),
        (status = 504, description = "Drain timed out"),
    ),
    tag = "Admin",
)]
async fn put_config(
    State(admin): State<Arc<AdminState>>,
    body: Result<Json<ConfigReplace>, JsonRejection>,
) -> Response {
    let Json(replace) = match body {
        Ok(b) => b,
        Err(rej) => return json_rejection_response(rej, admin.opaque_errors),
    };
    let new_config: VlcConfig = replace.into();

    let coordinator = admin.coordinator.clone();
    let readiness = admin.readiness.clone();
    let _lock_guard = coordinator.lock().await;
    let mut scope = ReconfigScopeGuard::new(&coordinator, &readiness);
    let current = admin.runtime.load_full();
    run_commit(&admin, &current, new_config, &mut scope).await
}

/// DELETE /admin/config: reset to the startup baseline.
#[utoipa::path(
    delete,
    path = "/admin/config",
    responses(
        (status = 200, description = "Reset to baseline; response is fresh ConfigView with effective == baseline"),
        (status = 422, description = "Baseline rejected normalize_converter_config (should be impossible absent a library regression)"),
        (status = 503, description = "Rebuild failure OR server shutting down"),
        (status = 504, description = "Drain timed out"),
    ),
    tag = "Admin",
)]
async fn delete_config(State(admin): State<Arc<AdminState>>) -> Response {
    let coordinator = admin.coordinator.clone();
    let readiness = admin.readiness.clone();
    let _lock_guard = coordinator.lock().await;
    let mut scope = ReconfigScopeGuard::new(&coordinator, &readiness);
    let current = admin.runtime.load_full();
    let new_config = (*admin.baseline).clone();
    run_commit(&admin, &current, new_config, &mut scope).await
}

/// Common post-apply commit pipeline: validate, identity short-circuit,
/// drain + rebuild, swap the snapshot, return the resulting ConfigView.
///
/// Process-global state lives outside the DTO under
/// `/admin/config/fonts/{directories,cache_size}`, so every non-identity
/// `VlcConfig` commit drains and rebuilds. `generation` moves forward by 1 on
/// every successful commit.
async fn run_commit<'a>(
    admin: &AdminState,
    current: &Arc<RuntimeSnapshot>,
    new_config: VlcConfig,
    scope: &mut ReconfigScopeGuard<'a>,
) -> Response {
    // Normalize: validates URLs, V8 heap minimum, resolves plugin files,
    // etc. Any error here is a 422 validation failure.
    let new_config = match normalize_converter_config(new_config) {
        Ok(c) => c,
        Err(err) => {
            return validation_error_response(
                validation_error_from_anyhow(&err),
                admin.opaque_errors,
            );
        }
    };

    if new_config == *current.config {
        let view = build_config_view(current, admin);
        return (StatusCode::OK, Json(view)).into_response();
    }

    let coordinator = admin.coordinator.clone();
    coordinator.close_gate();
    scope.mark_gate_closed();
    match coordinator.drain().await {
        Ok(()) => {}
        Err(DrainError::Cancelled) => {
            return simple_error_response(
                StatusCode::SERVICE_UNAVAILABLE,
                "server shutting down",
                admin.opaque_errors,
            );
        }
        Err(DrainError::Timeout { inflight }) => {
            if admin.opaque_errors {
                return StatusCode::GATEWAY_TIMEOUT.into_response();
            }
            return (
                StatusCode::GATEWAY_TIMEOUT,
                Json(json!({
                    "error": "drain timeout",
                    "in_flight": inflight,
                })),
            )
                .into_response();
        }
    }

    let new_converter = match current.converter.clone().reconfigure(new_config.clone()) {
        Ok(c) => c,
        Err(err) => {
            return validation_error_response(
                validation_error_from_anyhow(&err),
                admin.opaque_errors,
            );
        }
    };

    if let Err(err) = new_converter.warm_up() {
        let msg = format!("warm-up failed: {err}; config not committed");
        return simple_error_response(StatusCode::SERVICE_UNAVAILABLE, &msg, admin.opaque_errors);
    }

    let new_snapshot = Arc::new(RuntimeSnapshot {
        converter: new_converter,
        config: Arc::new(new_config),
        generation: current.generation + 1,
    });
    admin.runtime.store(new_snapshot.clone());

    let view = build_config_view(&new_snapshot, admin);
    (StatusCode::OK, Json(view)).into_response()
}

// =============================================================================
// /admin/config/fonts/directories
// =============================================================================
//
// Font directories are process-global state, not writable `VlcConfig`
// fields. These endpoints serialize against config mutations.

/// Return the currently-registered font directories.
#[utoipa::path(
    get,
    path = "/admin/config/fonts/directories",
    responses((status = 200, description = "Array of absolute filesystem paths")),
    tag = "Admin",
)]
async fn get_font_dirs(State(_admin): State<Arc<AdminState>>) -> Response {
    let dirs: Vec<String> = vl_convert_rs::current_font_directories()
        .into_iter()
        .map(|p| p.to_string_lossy().into_owned())
        .collect();
    (StatusCode::OK, Json(dirs)).into_response()
}

/// Replace the full font-directory list. Pass `{"paths": []}` to clear.
#[utoipa::path(
    put,
    path = "/admin/config/fonts/directories",
    responses(
        (status = 200, description = "Replacement applied; response is the new list"),
        (status = 400, description = "Malformed body or any path is not an existing directory"),
        (status = 503, description = "Library-level set_font_directories failed; registry NOT updated"),
    ),
    tag = "Admin",
)]
async fn put_font_dirs(
    State(admin): State<Arc<AdminState>>,
    body: Result<Json<crate::types::FontDirReplace>, JsonRejection>,
) -> Response {
    let Json(req) = match body {
        Ok(b) => b,
        Err(rej) => return json_rejection_response(rej, admin.opaque_errors),
    };

    for path in &req.paths {
        if !path.is_dir() {
            return simple_error_response(
                StatusCode::BAD_REQUEST,
                &format!("path not found or not a directory: {}", path.display()),
                admin.opaque_errors,
            );
        }
    }

    let _lock = admin.coordinator.lock().await;
    if let Err(err) = vl_convert_rs::set_font_directories(&req.paths) {
        return simple_error_response(
            StatusCode::SERVICE_UNAVAILABLE,
            &format!("failed to set font directories: {err}"),
            admin.opaque_errors,
        );
    }

    let dirs: Vec<String> = req
        .paths
        .iter()
        .map(|p| p.to_string_lossy().into_owned())
        .collect();
    (StatusCode::OK, Json(dirs)).into_response()
}

/// Append a single font directory. Idempotent for already-registered
/// paths.
#[utoipa::path(
    post,
    path = "/admin/config/fonts/directories",
    responses(
        (status = 200, description = "Font directory appended (or already present); response is the new list"),
        (status = 400, description = "Missing path or path not found / not a directory"),
        (status = 503, description = "Library-level register_font_directory failed; registry NOT updated"),
    ),
    tag = "Admin",
)]
async fn post_font_dir(
    State(admin): State<Arc<AdminState>>,
    body: Result<Json<FontDirRequest>, JsonRejection>,
) -> Response {
    let req = match body {
        Ok(Json(r)) => r,
        Err(rej) => return json_rejection_response(rej, admin.opaque_errors),
    };

    if !req.path.is_dir() {
        return simple_error_response(
            StatusCode::BAD_REQUEST,
            &format!("path not found or not a directory: {}", req.path.display()),
            admin.opaque_errors,
        );
    }

    let _lock = admin.coordinator.lock().await;

    // Keep the endpoint idempotent even though the lower-level registry
    // API appends.
    if !vl_convert_rs::current_font_directories().contains(&req.path) {
        let path_str = req.path.to_string_lossy();
        if let Err(err) = vl_convert_rs::text::register_font_directory(&path_str) {
            return simple_error_response(
                StatusCode::SERVICE_UNAVAILABLE,
                &format!("failed to register font directory: {err}"),
                admin.opaque_errors,
            );
        }
    }

    let dirs: Vec<String> = vl_convert_rs::current_font_directories()
        .into_iter()
        .map(|p| p.to_string_lossy().into_owned())
        .collect();
    (StatusCode::OK, Json(dirs)).into_response()
}

// =============================================================================
// /admin/config/fonts/cache_size
// =============================================================================
//
// Process-global Google Fonts LRU cache cap. This is not a writable
// `VlcConfig` field and does not require drain/rebuild.

/// Return the currently-active Google Fonts cache cap in MB.
#[utoipa::path(
    get,
    path = "/admin/config/fonts/cache_size",
    responses((
        status = 200,
        description = "{\"max_size_mb\": <number>}; the resolved cap"
    )),
    tag = "Admin",
)]
async fn get_font_cache_size(State(_admin): State<Arc<AdminState>>) -> Response {
    let mb = vl_convert_rs::current_google_fonts_cache_size_mb().get();
    (StatusCode::OK, Json(json!({ "max_size_mb": mb }))).into_response()
}

/// Set the Google Fonts cache cap. `{"max_size_mb": null}` resets
/// to the library default.
#[utoipa::path(
    put,
    path = "/admin/config/fonts/cache_size",
    responses(
        (status = 200, description = "Cap updated; response is the new resolved cap"),
        (status = 400, description = "Malformed body"),
        (status = 503, description = "Library-level set_google_fonts_cache_size_mb failed"),
    ),
    tag = "Admin",
)]
async fn put_font_cache_size(
    State(admin): State<Arc<AdminState>>,
    body: Result<Json<crate::types::CacheSizeReplace>, JsonRejection>,
) -> Response {
    let Json(req) = match body {
        Ok(b) => b,
        Err(rej) => return json_rejection_response(rej, admin.opaque_errors),
    };

    let _lock = admin.coordinator.lock().await;
    if let Err(err) = vl_convert_rs::set_google_fonts_cache_size_mb(req.max_size_mb) {
        return simple_error_response(
            StatusCode::SERVICE_UNAVAILABLE,
            &format!("failed to set google_fonts_cache_size_mb: {err}"),
            admin.opaque_errors,
        );
    }

    let mb = vl_convert_rs::current_google_fonts_cache_size_mb().get();
    (StatusCode::OK, Json(json!({ "max_size_mb": mb }))).into_response()
}