ilhamdev commited on
Commit
703dbd9
·
1 Parent(s): a364ccb

fix: default backend URL to HuggingFace space and optimize keep-alive ping

Browse files
Files changed (2) hide show
  1. app.py +2 -2
  2. frontend/app.js +21 -22
app.py CHANGED
@@ -54,8 +54,8 @@ def start_keep_alive():
54
 
55
  while True:
56
  try:
57
- # Interval ping setiap 15 menit (HF idle sleep threshold adalah 48 jam)
58
- time.sleep(15 * 60)
59
  req = urllib.request.Request(
60
  target_url,
61
  headers={"User-Agent": "HF-Space-SelfKeepAlive/1.0"}
 
54
 
55
  while True:
56
  try:
57
+ # Interval ping setiap 5 menit agar Space selalu terjaga aktif
58
+ time.sleep(5 * 60)
59
  req = urllib.request.Request(
60
  target_url,
61
  headers={"User-Agent": "HF-Space-SelfKeepAlive/1.0"}
frontend/app.js CHANGED
@@ -7,25 +7,24 @@ const DEFAULT_HF_BACKEND = 'https://ilhamdev-wa-full-pfp.hf.space';
7
  function getDefaultBackendUrl() {
8
  const saved = localStorage.getItem('wa_pfp_backend_url');
9
  if (saved && saved.trim() !== '') {
10
- return saved.trim();
11
- }
12
- // Jika dibuka di domain kustom / subdomain mangrusdi.my.id
13
- if (window.location.hostname.includes('mangrusdi.my.id')) {
14
- return window.location.origin;
 
15
  }
16
- // Jika dibuka lokal di komputer (localhost/127.0.0.1)
17
  if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
18
  return window.location.origin;
19
  }
20
- // Jika dibuka langsung di web space Hugging Face
21
  if (window.location.hostname.includes('hf.space')) {
22
  return window.location.origin;
23
  }
24
- // Default jika di Vercel:
25
- if (window.location.hostname.includes('vercel.app')) {
26
- return DEFAULT_HF_BACKEND;
27
- }
28
- return window.location.origin || DEFAULT_HF_BACKEND;
29
  }
30
 
31
  // Application State
@@ -230,7 +229,7 @@ async function fetchBackendConfig() {
230
  async function checkBackendHealth() {
231
  const base = getBackendHttpUrl();
232
  if (!isWakingUp) {
233
- serverStatusBtn.className = 'status-pill status-checking';
234
  serverStatusText.textContent = 'Memeriksa Backend...';
235
  }
236
 
@@ -247,27 +246,27 @@ async function checkBackendHealth() {
247
 
248
  if (res.ok) {
249
  isWakingUp = false;
250
- serverStatusBtn.className = 'status-pill status-online';
251
- serverStatusText.textContent = 'Backend Online';
252
  // Ambil konfigurasi publik dari backend saat online
253
  fetchBackendConfig();
254
  } else if (res.status === 502 || res.status === 503 || res.status === 504) {
255
  isWakingUp = true;
256
- serverStatusBtn.className = 'status-pill status-waking';
257
- serverStatusText.textContent = 'Membangunkan Server (~30d)...';
258
- setTimeout(checkBackendHealth, 5000);
259
  } else {
260
  throw new Error(`Status ${res.status}`);
261
  }
262
  } catch (err) {
263
  if (err.name === 'AbortError' || isWakingUp) {
264
  isWakingUp = true;
265
- serverStatusBtn.className = 'status-pill status-waking';
266
- serverStatusText.textContent = 'Membangunkan Server (~30d)...';
267
- setTimeout(checkBackendHealth, 5000);
268
  } else {
269
  isWakingUp = false;
270
- serverStatusBtn.className = 'status-pill status-offline';
271
  serverStatusText.textContent = 'Backend Offline';
272
  }
273
  }
 
7
  function getDefaultBackendUrl() {
8
  const saved = localStorage.getItem('wa_pfp_backend_url');
9
  if (saved && saved.trim() !== '') {
10
+ const cleanSaved = saved.trim().replace(/\/+$/, '');
11
+ // Abaikan jika sebelumnya tersimpan URL frontend Vercel/mangrusdi.my.id yang bukan backend Go
12
+ if (!cleanSaved.includes('mangrusdi.my.id') && !cleanSaved.includes('vercel.app')) {
13
+ return cleanSaved;
14
+ }
15
+ localStorage.removeItem('wa_pfp_backend_url');
16
  }
17
+ // Jika dibuka lokal di komputer saat development (localhost/127.0.0.1)
18
  if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
19
  return window.location.origin;
20
  }
21
+ // Jika dibuka langsung di dalam web space Hugging Face
22
  if (window.location.hostname.includes('hf.space')) {
23
  return window.location.origin;
24
  }
25
+ // Default utama untuk produksi (misal di https://ppfull.mangrusdi.my.id/ atau Vercel):
26
+ // Langsung terhubung ke backend resmi Hugging Face agar pengguna awam tinggal upload foto saja
27
+ return DEFAULT_HF_BACKEND;
 
 
28
  }
29
 
30
  // Application State
 
229
  async function checkBackendHealth() {
230
  const base = getBackendHttpUrl();
231
  if (!isWakingUp) {
232
+ serverStatusBtn.className = 'status-indicator status-checking';
233
  serverStatusText.textContent = 'Memeriksa Backend...';
234
  }
235
 
 
246
 
247
  if (res.ok) {
248
  isWakingUp = false;
249
+ serverStatusBtn.className = 'status-indicator status-online';
250
+ serverStatusText.textContent = 'Backend Online (Siap)';
251
  // Ambil konfigurasi publik dari backend saat online
252
  fetchBackendConfig();
253
  } else if (res.status === 502 || res.status === 503 || res.status === 504) {
254
  isWakingUp = true;
255
+ serverStatusBtn.className = 'status-indicator status-waking';
256
+ serverStatusText.textContent = 'Membangunkan Server (~25d)...';
257
+ setTimeout(checkBackendHealth, 4000);
258
  } else {
259
  throw new Error(`Status ${res.status}`);
260
  }
261
  } catch (err) {
262
  if (err.name === 'AbortError' || isWakingUp) {
263
  isWakingUp = true;
264
+ serverStatusBtn.className = 'status-indicator status-waking';
265
+ serverStatusText.textContent = 'Membangunkan Server (~25d)...';
266
+ setTimeout(checkBackendHealth, 4000);
267
  } else {
268
  isWakingUp = false;
269
+ serverStatusBtn.className = 'status-indicator status-offline';
270
  serverStatusText.textContent = 'Backend Offline';
271
  }
272
  }