File size: 1,493 Bytes
f79de19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# init_db.py
import os, sqlite3
from datetime import datetime

# DB location (can be overridden by env)
DB_PATH = os.environ.get("API_DB_PATH", "/data/api_keys.sqlite3")

# Seed values
DEFAULT_KEY   = os.environ.get("DEFAULT_API_KEY", "sk-1234")   # can override via env
BOOTSTRAP_KEY = "sk-bootstrap-1234"                            # hard-coded as requested

os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)

with sqlite3.connect(DB_PATH) as conn:
    cur = conn.cursor()
    cur.execute("""
    CREATE TABLE IF NOT EXISTS api_keys(
        id         INTEGER PRIMARY KEY AUTOINCREMENT,
        api_key    TEXT UNIQUE,
        label      TEXT,
        created_at TEXT NOT NULL,
        last_used  TEXT,
        active     INTEGER NOT NULL DEFAULT 1
    )
    """)

    now = datetime.utcnow().isoformat(timespec="seconds")

    def upsert(key: str, label: str):
        key = (key or "").strip()
        if not key:
            return
        # Insert full row; on conflict, refresh label/last_used/active
        cur.execute(
            """
            INSERT INTO api_keys(api_key, label, created_at, last_used, active)
            VALUES (?, ?, ?, ?, 1)
            ON CONFLICT(api_key) DO UPDATE SET
                label     = excluded.label,
                last_used = excluded.last_used,
                active    = 1
            """,
            (key, label, now, now),
        )

    upsert(DEFAULT_KEY,   "default")
    upsert(BOOTSTRAP_KEY, "bootstrap")

    conn.commit()