rianmz-genz commited on
Commit
c4e73c1
·
1 Parent(s): c44aad8
Files changed (1) hide show
  1. Dockerfile +103 -14
Dockerfile CHANGED
@@ -1,18 +1,107 @@
1
- FROM node:20-slim
2
 
3
- # Set environment agar port dan host terkunci untuk Hugging Face
4
- ENV PORT=7860 \
5
- HOSTNAME=0.0.0.0 \
6
- NODE_ENV=production \
7
- NEXT_TELEMETRY_DISABLED=1
8
 
9
- # Install 9router dan library sqlite yang dibutuhkan secara global
10
- RUN npm install -g 9router@latest --prefer-online --no-audit --no-fund \
11
- && mkdir -p /root/.9router/runtime \
12
- && cd /root/.9router/runtime \
13
- && npm install sql.js@1.13.0 better-sqlite3@12.6.2 --no-audit --no-fund --prefer-online
14
 
15
- EXPOSE 7860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Jalankan 9router dengan flag --host 0.0.0.0 dan --tray agar standby di foreground
18
- CMD ["9router", "--tray", "--skip-update", "--no-browser", "--log", "--port", "7860", "--host", "0.0.0.0"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM decolua/9router:latest
2
 
3
+ # 1. Switch to root to install system packages
4
+ USER root
 
 
 
5
 
6
+ # 2. Install Python for the backup script
7
+ RUN apk add --no-cache python3 py3-pip tar \
8
+ && pip3 install --break-system-packages --no-cache-dir huggingface_hub
 
 
9
 
10
+ WORKDIR /app
11
+
12
+ # 3. Create the Python sync script
13
+ RUN cat > /app/sync.py <<'PY'
14
+ import os
15
+ import tarfile
16
+ import time
17
+ import sys
18
+ from pathlib import Path
19
+ from huggingface_hub import HfApi, hf_hub_download
20
+
21
+ REPO_ID = os.environ.get("DATASET_REPO")
22
+ TOKEN = os.environ.get("HF_TOKEN")
23
+ BACKUP_FILE = "/tmp/9router_backup.tar.gz"
24
+ DATA_DIR = Path("/app/data")
25
+
26
+ def safe_extract(tar, path):
27
+ base = Path(path).resolve()
28
+ for member in tar.getmembers():
29
+ target = (base / member.name).resolve()
30
+ if not str(target).startswith(str(base)):
31
+ raise RuntimeError(f"Unsafe tar member: {member.name}")
32
+ tar.extractall(path)
33
+
34
+ def upload():
35
+ if not REPO_ID or not TOKEN:
36
+ print(">>> [Sync] DATASET_REPO or HF_TOKEN not configured. Skipping backup.")
37
+ return
38
+ if not DATA_DIR.exists():
39
+ print(">>> [Sync] Data directory does not exist. Skipping backup.")
40
+ return
41
+ try:
42
+ with tarfile.open(BACKUP_FILE, "w:gz") as tar:
43
+ # We pack the /app/data folder as 'data'
44
+ tar.add(str(DATA_DIR), arcname="data")
45
+ HfApi(token=TOKEN).upload_file(
46
+ path_or_fileobj=BACKUP_FILE,
47
+ path_in_repo="9router_backup.tar.gz",
48
+ repo_id=REPO_ID,
49
+ repo_type="dataset",
50
+ )
51
+ print(f">>> [Sync] Backup successful: {time.strftime('%Y-%m-%d %H:%M:%S')}", flush=True)
52
+ except Exception as e:
53
+ print(f">>> [Sync] Backup failed: {e}", flush=True)
54
+
55
+ def download():
56
+ if not REPO_ID or not TOKEN:
57
+ print(">>> [Sync] DATASET_REPO or HF_TOKEN not configured. Skipping restore.")
58
+ return
59
+ try:
60
+ print(">>> [Sync] Downloading data from Dataset...", flush=True)
61
+ path = hf_hub_download(
62
+ repo_id=REPO_ID,
63
+ filename="9router_backup.tar.gz",
64
+ repo_type="dataset",
65
+ token=TOKEN,
66
+ )
67
+ with tarfile.open(path, "r:gz") as tar:
68
+ # Extracting into /app will perfectly place it at /app/data
69
+ safe_extract(tar, "/app")
70
+ print(">>> [Sync] Data restore successful.", flush=True)
71
+ except Exception as e:
72
+ print(f">>> [Sync] Skipping restore (might be first run): {e}", flush=True)
73
+
74
+ if __name__ == "__main__":
75
+ if len(sys.argv) > 1 and sys.argv[1] == "upload":
76
+ upload()
77
+ elif len(sys.argv) > 1 and sys.argv[1] == "download":
78
+ download()
79
+ PY
80
+
81
+ # 4. Create the startup shell script
82
+ RUN cat > /app/run.sh <<'SH'
83
+ #!/bin/sh
84
+ set -e
85
 
86
+ mkdir -p /app/data
87
+
88
+ echo '=== 1. Restoring Data ==='
89
+ python3 /app/sync.py download
90
+
91
+ echo '=== 2. Starting Scheduled Backup (Every 10 minutes) ==='
92
+ (while true; do sleep 600; python3 /app/sync.py upload; done) &
93
+
94
+ echo '=== 3. Starting 9router ==='
95
+ export PORT=7860
96
+ export HOSTNAME=0.0.0.0
97
+ export DATA_DIR=/app/data
98
+ export NEXT_PUBLIC_BASE_URL="https://${SPACE_HOST:-otnansirk-9router.hf.space}"
99
+
100
+ # The critical fix: We run the raw server directly, NO CLI, NO TRAY.
101
+ exec node server.js
102
+ SH
103
+
104
+ RUN chmod +x /app/run.sh
105
+
106
+ EXPOSE 7860
107
+ CMD ["/bin/sh", "/app/run.sh"]