oxipage-core 0.2.0

Oxipage 코어 라이브러리 — Axum 서버 부트스트랩, SQLite 마이그레이션, 인증, Extension trait, SSR 스냅샷, 레이트리밋
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
//! 첫 부팅 UX 설정 마법사 API (doc/13).
//!
//! 모든 핸들러는 `setup_gate` 미들웨어가 보호:
//! - loopback(127.0.0.1/::1) 외 403
//! - setup 완료 후 410 Gone

use crate::auth;
use crate::error::ApiError;
use crate::extension::DataEnvelope;
use crate::state::{AppState, SiteOverride};
use argon2::Argon2;
use axum::extract::{ConnectInfo, Request, State};
use axum::http::StatusCode;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use axum::{Json, Router};
use axum::routing::{get, post};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

// ─── Constants ───

const SAMPLE_POST_TITLE: &str = "환영합니다";
const SAMPLE_POST_SLUG: &str = "환영합니다";
const SAMPLE_POST_BODY: &str = r#"# 환영합니다!

Oxipage 설치가 완료되었습니다.

이 글은 설정 마법사가 생성한 샘플 글입니다.
삭제하거나 수정해도 됩니다.

## 다음 단계

- **CLI**로 글 쓰기: `oxipage blog new "제목" --file draft.md`
- **관리 콘솔**에서 콘텐츠 관리: 헤더의 ⚙️ 버튼
- **프로젝트** 추가: `oxipage project add --title-ko "..." --title-en "..."`

즐거운 블로그 생활 되세요!
"#;

const THEMES: &[ThemeEntry] = &[
    ThemeEntry { id: "paper", name_ko: "종이", name_en: "Paper", mode: "light",
        description_ko: "따뜻한 종이 배경", description_en: "Warm paper background",
        preview_colors: ["#fafaf5", "#f5f2ed", "#2d2934", "#5e3cbd"] },
    ThemeEntry { id: "midnight", name_ko: "한밤", name_en: "Midnight", mode: "dark",
        description_ko: "깊은 밤하늘", description_en: "Deep night sky",
        preview_colors: ["#1a1a2e", "#16213e", "#e0e0e0", "#4fc3f7"] },
    ThemeEntry { id: "sepia", name_ko: "세피아", name_en: "Sepia", mode: "light",
        description_ko: "오래된 책장", description_en: "Old bookshelf",
        preview_colors: ["#f5f0e8", "#ede0d4", "#3d3529", "#b8860b"] },
    ThemeEntry { id: "forest", name_ko: "", name_en: "Forest", mode: "dark",
        description_ko: "이끼 낀 숲", description_en: "Mossy forest",
        preview_colors: ["#1b2b1b", "#243624", "#e0e8e0", "#2ecc71"] },
];

// ─── Input / Output types ───

#[derive(Deserialize)]
pub struct SiteInput {
    pub name: String,
    pub base_url: Option<String>,
}

#[derive(Deserialize)]
pub struct AdminInput {
    pub password: String,
}

#[derive(Serialize, Deserialize)]
pub struct ExtensionsInput {
    pub enabled: Vec<String>,
}

#[derive(Deserialize)]
pub struct ProfileInput {
    pub display_name: Option<String>,
    pub tagline_ko: Option<String>,
    pub tagline_en: Option<String>,
    pub github_username: Option<String>,
    pub bio_ko: Option<String>,
    pub bio_en: Option<String>,
}

#[derive(Deserialize)]
pub struct ThemeInput {
    pub theme_id: String,
    pub lobby_mode: Option<String>,
}

#[derive(Deserialize)]
pub struct ContentInput {
    #[serde(default = "default_true")]
    pub sample_post: bool,
    pub tmdb_key: Option<String>,
    pub aladin_key: Option<String>,
}

fn default_true() -> bool { true }

#[derive(Serialize)]
pub struct CompleteResult {
    pub ok: bool,
    pub token: String,
    pub token_label: String,
    pub message: String,
}

#[derive(Serialize)]
pub struct StatusResult {
    pub setup_mode: bool,
    pub completed_steps: Vec<String>,
    pub available_extensions: Vec<ExtInfo>,
    pub available_themes: Vec<ThemeEntry>,
}

#[derive(Serialize)]
pub struct ExtInfo {
    pub id: String,
    pub display_name: ExtDisplayName,
}

#[derive(Serialize)]
pub struct ExtDisplayName {
    pub ko: String,
    pub en: String,
}

#[derive(Serialize, Clone)]
pub struct ThemeEntry {
    pub id: &'static str,
    pub name_ko: &'static str,
    pub name_en: &'static str,
    pub mode: &'static str,
    pub description_ko: &'static str,
    pub description_en: &'static str,
    pub preview_colors: [&'static str; 4],
}

#[derive(Serialize)]
pub struct SimpleOk {
    pub ok: bool,
}

// ─── Helpers ───

/// 완료된 step 목록 조회.
async fn get_completed_steps(state: &AppState) -> Result<Vec<String>, ApiError> {
    let mut steps = Vec::new();

    // site: site_name이 설정되었는가
    if let Ok((Some(_),)) = sqlx::query_as::<_, (Option<String>,)>(
        "SELECT site_name FROM setup_state WHERE id = 1",
    )
    .fetch_one(&state.db)
    .await
    {
        steps.push("site".into());
    }

    // admin: admin_password_hash가 설정되었는가
    if let Ok((Some(_),)) = sqlx::query_as::<_, (Option<String>,)>(
        "SELECT admin_password_hash FROM setup_state WHERE id = 1",
    )
    .fetch_one(&state.db)
    .await
    {
        steps.push("admin".into());
    }

    // extensions: enabled가 1개 이상인가
    match sqlx::query_as::<_, (i64,)>(
        "SELECT COUNT(*) FROM extension_state WHERE enabled = 1",
    )
    .fetch_one(&state.db)
    .await
    {
        Ok((count,)) if count > 0 => steps.push("extensions".into()),
        _ => {}
    }

    // profile: tagline이나 github가 설정되었는가
    match sqlx::query_as::<_, (i64,)>(
        "SELECT COUNT(*) FROM profile WHERE id = 1 AND (tagline_ko IS NOT NULL OR github_username IS NOT NULL)",
    )
    .fetch_one(&state.db)
    .await
    {
        Ok((count,)) if count > 0 => steps.push("profile".into()),
        _ => {}
    }

    // content: 샘플 글이 생성되었거나, API 키가 설정되었는가
    match sqlx::query_as::<_, (i64,)>(
        "SELECT COUNT(*) FROM blog_post WHERE slug = ?1",
    )
    .bind(SAMPLE_POST_SLUG)
    .fetch_one(&state.db)
    .await
    {
        Ok((count,)) if count > 0 => {
            steps.push("content".into());
        }
        _ => {}
    }

    // theme은 항상 통과 (step 5는 필수)
    steps.push("theme".into());

    Ok(steps)
}

/// argon2id 해싱 (doc/13 §13.6.2)
fn hash_password(password: &str) -> anyhow::Result<String> {
    use base64::Engine;
    let salt = crate::auth::generate_plain_token();
    let mut hash = vec![0u8; 32];
    Argon2::default()
        .hash_password_into(password.as_bytes(), salt.as_bytes(), &mut hash)
        .map_err(|e| anyhow::anyhow!("argon2 hash failed: {e}"))?;
    Ok(format!(
        "argon2id$v=19$m=19456,t=2,p=1${}${}",
        base64::engine::general_purpose::STANDARD.encode(salt.as_bytes()),
        base64::engine::general_purpose::STANDARD.encode(&hash),
    ))
}

/// TOML 파일 갱신 (site name)
fn update_toml_site(name: &str, base_url: &str) -> anyhow::Result<()> {
    let config_path = std::env::var("OXIPAGE_CONFIG")
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|_| std::path::PathBuf::from("oxipage.toml"));

    if !config_path.exists() {
        return Ok(());
    }

    let content = std::fs::read_to_string(&config_path)?;
    let mut toml_val: toml::Value = content.parse::<toml::Value>()?;

    if let Some(table) = toml_val.get_mut("site").and_then(|s| s.as_table_mut()) {
        table.insert("name".into(), toml::Value::String(name.into()));
        table.insert("base_url".into(), toml::Value::String(base_url.into()));
    }

    let out = toml::to_string(&toml_val)?;
    std::fs::write(&config_path, out)?;
    Ok(())
}

/// extension_state.config JSON 업데이트
async fn set_extension_config(state: &AppState, ext_id: &str, key: &str, value: &str) -> Result<(), ApiError> {
    if value.is_empty() {
        return Ok(());
    }
    let config_json = serde_json::json!({ key: value }).to_string();
    sqlx::query("UPDATE extension_state SET config = ?1 WHERE extension_id = ?2")
        .bind(&config_json)
        .bind(ext_id)
        .execute(&state.db)
        .await
        .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;
    Ok(())
}

/// PAT를 credentials 파일에 쓰기 (best-effort)
fn write_credentials(token: &str) {
    use std::path::PathBuf;
    let creds_path = directories::ProjectDirs::from("", "", "oxipage")
        .map(|d| d.config_dir().join("credentials"))
        .unwrap_or_else(|| PathBuf::from(".oxipage-credentials"));

    if let Some(parent) = creds_path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    if std::fs::write(&creds_path, token).is_ok() {
        #[cfg(unix)]
        let _ = std::fs::set_permissions(&creds_path, std::os::unix::fs::PermissionsExt::from_mode(0o600));
        tracing::info!("wrote PAT to credentials file: {}", creds_path.display());
    }
}

// ─── Route builder ───

/// Setup 라우트를 api Router에 추가한다.
/// setup_gate 미들웨어는 http.rs에서 별도 적용.
pub fn setup_routes(api: Router<AppState>) -> Router<AppState> {
    api.route("/setup/status", get(setup_status_handler))
        .route("/setup/site", post(setup_site_handler))
        .route("/setup/admin", post(setup_admin_handler))
        .route("/setup/extensions", post(setup_extensions_handler))
        .route("/setup/profile", post(setup_profile_handler))
        .route("/setup/theme", post(setup_theme_handler))
        .route("/setup/content", post(setup_content_handler))
        .route("/setup/complete", post(setup_complete_handler))
}

// ─── Middleware ───

/// Check if setup wizard should run (no setup_completed_at in setup_state).
pub async fn is_setup_needed(db: &sqlx::SqlitePool) -> bool {
    matches!(
        sqlx::query_as::<_, (Option<String>,)>(
            "SELECT setup_completed_at FROM setup_state WHERE id = 1",
        )
        .fetch_one(db)
        .await,
        Ok((None,))
    )
}

/// Setup 게이트 미들웨어.
/// `/setup/*` 경로에만 적용: loopback 외 403, 완료 후 410.
pub async fn setup_gate(
    State(state): State<AppState>,
    request: Request,
    next: Next,
) -> Response {
    let path = request.uri().path();
    let is_setup = path.contains("/setup/");

    if is_setup {
        // 1. Loopback check
        let is_loopback = request
            .extensions()
            .get::<ConnectInfo<SocketAddr>>()
            .is_some_and(|ci| ci.0.ip().is_loopback());
        if !is_loopback {
            return ApiError::new(
                StatusCode::FORBIDDEN,
                "setup_loopback_only",
                "setup API is only available from localhost",
            )
            .into_response();
        }

        // 2. Setup completed check
        match sqlx::query_as::<_, (Option<String>,)>(
            "SELECT setup_completed_at FROM setup_state WHERE id = 1",
        )
        .fetch_one(&state.db)
        .await
        {
            Ok((Some(_),)) => {
                return ApiError::new(
                    StatusCode::GONE,
                    "setup_completed",
                    "setup has already been completed",
                )
                .into_response();
            }
            Err(e) => {
                return ApiError::new(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "internal",
                    &format!("setup state check failed: {e}"),
                )
                .into_response();
            }
            _ => {}
        }
    }

    next.run(request).await
}

// ─── Handlers ───

/// GET /api/console/setup/status
pub async fn setup_status_handler(
    State(state): State<AppState>,
) -> Result<Json<DataEnvelope<StatusResult>>, ApiError> {
    let completed_steps = get_completed_steps(&state).await?;

    let extensions: Vec<ExtInfo> = state
        .registry
        .iter()
        .into_iter()
        .map(|ext| ExtInfo {
            id: ext.id().to_string(),
            display_name: ExtDisplayName {
                ko: ext.display_name(crate::extension::Lang::Ko),
                en: ext.display_name(crate::extension::Lang::En),
            },
        })
        .collect();

    Ok(Json(DataEnvelope {
        data: StatusResult {
            setup_mode: true,
            completed_steps,
            available_extensions: extensions,
            available_themes: THEMES.to_vec(),
        },
    }))
}

/// POST /api/console/setup/site
pub async fn setup_site_handler(
    State(state): State<AppState>,
    Json(input): Json<SiteInput>,
) -> Result<Json<DataEnvelope<SimpleOk>>, ApiError> {
    let name = input.name.trim().to_string();
    if name.is_empty() || name.len() > 50 {
        return Err(ApiError::new(
            StatusCode::BAD_REQUEST,
            "invalid_site_name",
            "site name must be 1-50 characters",
        ));
    }
    let base_url = input.base_url.unwrap_or_else(|| "http://127.0.0.1:8787".into());

    // Persist
    let _ = update_toml_site(&name, &base_url);

    sqlx::query("UPDATE setup_state SET site_name = ?1, base_url = ?2 WHERE id = 1")
        .bind(&name)
        .bind(&base_url)
        .execute(&state.db)
        .await
        .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    sqlx::query(
        "UPDATE profile SET display_name = ?1, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = 1",
    )
    .bind(&name)
    .execute(&state.db)
    .await
    .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    *state.site_override.write().await = Some(SiteOverride {
        name: name.clone(),
        base_url: base_url.clone(),
    });

    Ok(Json(DataEnvelope {
        data: SimpleOk { ok: true },
    }))
}

/// POST /api/console/setup/admin
pub async fn setup_admin_handler(
    State(state): State<AppState>,
    Json(input): Json<AdminInput>,
) -> Result<Json<DataEnvelope<SimpleOk>>, ApiError> {
    if input.password.len() < 8 {
        return Err(ApiError::new(
            StatusCode::BAD_REQUEST,
            "password_too_short",
            "password must be at least 8 characters",
        ));
    }

    let hash = hash_password(&input.password).map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    sqlx::query("UPDATE setup_state SET admin_password_hash = ?1 WHERE id = 1")
        .bind(&hash)
        .execute(&state.db)
        .await
        .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    Ok(Json(DataEnvelope {
        data: SimpleOk { ok: true },
    }))
}

/// POST /api/console/setup/extensions
pub async fn setup_extensions_handler(
    State(state): State<AppState>,
    Json(input): Json<ExtensionsInput>,
) -> Result<Json<DataEnvelope<ExtensionsInput>>, ApiError> {
    for id in &input.enabled {
        if state.registry.find(id).is_none() {
            return Err(ApiError::new(
                StatusCode::BAD_REQUEST,
                "unknown_extension",
                &format!("unknown extension: {id}"),
            ));
        }
    }

    // Disable all, then enable selected
    sqlx::query("UPDATE extension_state SET enabled = 0")
        .execute(&state.db)
        .await
        .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    for id in &input.enabled {
        let count = sqlx::query("UPDATE extension_state SET enabled = 1 WHERE extension_id = ?1")
            .bind(id)
            .execute(&state.db)
            .await
            .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;
        if count.rows_affected() == 0 {
            sqlx::query(
                "INSERT INTO extension_state (extension_id, enabled, purged) VALUES (?1, 1, 0)",
            )
            .bind(id)
            .execute(&state.db)
            .await
            .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;
        }
    }

    // Update runtime registry cache
    for ext in state.registry.iter().into_iter() {
        let enabled = input.enabled.iter().any(|id| id == ext.id());
        let _ = state
            .registry
            .set_enabled(&state.db, ext.id(), enabled)
            .await;
    }

    Ok(Json(DataEnvelope {
        data: ExtensionsInput {
            enabled: input.enabled.clone(),
        },
    }))
}

/// POST /api/console/setup/profile
pub async fn setup_profile_handler(
    State(state): State<AppState>,
    Json(input): Json<ProfileInput>,
) -> Result<Json<DataEnvelope<SimpleOk>>, ApiError> {
    let dn = input.display_name.as_deref().unwrap_or("");
    sqlx::query(
        "UPDATE profile SET
            display_name = COALESCE(NULLIF(?1, ''), display_name),
            tagline_ko = ?2, tagline_en = ?3,
            github_username = ?4, bio_ko = ?5, bio_en = ?6,
            updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')
         WHERE id = 1",
    )
    .bind(dn)
    .bind(&input.tagline_ko)
    .bind(&input.tagline_en)
    .bind(&input.github_username)
    .bind(&input.bio_ko)
    .bind(&input.bio_en)
    .execute(&state.db)
    .await
    .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    Ok(Json(DataEnvelope {
        data: SimpleOk { ok: true },
    }))
}

/// POST /api/console/setup/theme
pub async fn setup_theme_handler(
    State(state): State<AppState>,
    Json(input): Json<ThemeInput>,
) -> Result<Json<DataEnvelope<SimpleOk>>, ApiError> {
    if !THEMES.iter().any(|t| t.id == input.theme_id) {
        return Err(ApiError::new(
            StatusCode::BAD_REQUEST,
            "invalid_theme",
            &format!("unknown theme: {}", input.theme_id),
        ));
    }

    sqlx::query("UPDATE theme_config SET theme_id = ?1, updated_at = datetime('now') WHERE id = 1")
        .bind(&input.theme_id)
        .execute(&state.db)
        .await
        .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    if let Some(ref mode) = input.lobby_mode {
        if !matches!(mode.as_str(), "canvas" | "grid" | "list") {
            return Err(ApiError::new(
                StatusCode::BAD_REQUEST,
                "invalid_lobby_mode",
                "lobby mode must be canvas|grid|list",
            ));
        }
        sqlx::query("UPDATE lobby_config SET display_mode = ?1")
            .bind(mode)
            .execute(&state.db)
            .await
            .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;
    }

    Ok(Json(DataEnvelope {
        data: SimpleOk { ok: true },
    }))
}

/// POST /api/console/setup/content
pub async fn setup_content_handler(
    State(state): State<AppState>,
    Json(input): Json<ContentInput>,
) -> Result<Json<DataEnvelope<SimpleOk>>, ApiError> {
    if input.sample_post {
        let exists: Result<(i64,), _> = sqlx::query_as(
            "SELECT COUNT(*) FROM blog_post WHERE slug = ?1",
        )
        .bind(SAMPLE_POST_SLUG)
        .fetch_one(&state.db)
        .await;

        if let Ok((0,)) = exists {
            sqlx::query(
                "INSERT INTO blog_post (slug, title, body, lang, tags, published_at, created_at, updated_at)
                 VALUES (?1, ?2, ?3, 'ko', '[]',
                         strftime('%Y-%m-%dT%H:%M:%fZ','now'),
                         strftime('%Y-%m-%dT%H:%M:%fZ','now'),
                         strftime('%Y-%m-%dT%H:%M:%fZ','now'))",
            )
            .bind(SAMPLE_POST_SLUG)
            .bind(SAMPLE_POST_TITLE)
            .bind(SAMPLE_POST_BODY)
            .execute(&state.db)
            .await
            .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;
        }
    }

    if let Some(key) = input.tmdb_key.as_ref().filter(|k| !k.is_empty()) {
        set_extension_config(&state, "movies", "tmdb_key", key).await?;
    }
    if let Some(key) = input.aladin_key.as_ref().filter(|k| !k.is_empty()) {
        set_extension_config(&state, "books", "aladin_key", key).await?;
    }

    Ok(Json(DataEnvelope {
        data: SimpleOk { ok: true },
    }))
}

/// POST /api/console/setup/complete
pub async fn setup_complete_handler(
    State(state): State<AppState>,
) -> Result<Json<DataEnvelope<CompleteResult>>, ApiError> {
    // Create first PAT (admin scope)
    let scopes = vec!["admin".to_string()];
    let (plain, row) = auth::create_pat(&state, "setup-wizard", &scopes)
        .await
        .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    // Mark setup as completed
    sqlx::query(
        "UPDATE setup_state SET setup_completed_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = 1",
    )
    .execute(&state.db)
    .await
    .map_err(|e| ApiError::internal(anyhow::anyhow!(e)))?;

    // Write PAT to credentials file (best-effort)
    write_credentials(&plain);

    Ok(Json(DataEnvelope {
        data: CompleteResult {
            ok: true,
            token: plain,
            token_label: row.label,
            message: "설정이 완료되었습니다. 이 토큰은 한 번만 표시됩니다.".into(),
        },
    }))
}