Spaces:
Runtime error
Runtime error
incognitolm commited on
Commit ·
dc4d10c
1
Parent(s): 89de491
Update index.js
Browse files- server/index.js +48 -10
server/index.js
CHANGED
|
@@ -35,23 +35,62 @@ const verifyLimiter = rateLimit({ windowMs: 60*1000, max: 5, standardHeaders: tr
|
|
| 35 |
|
| 36 |
const DATA_DIR = path.join(__dirname, 'data');
|
| 37 |
const VERSION_FILE = path.join(DATA_DIR, 'version.json');
|
|
|
|
|
|
|
| 38 |
|
| 39 |
function loadStoredSHA() {
|
| 40 |
try {
|
| 41 |
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
|
| 42 |
if (!fs.existsSync(VERSION_FILE)) return null;
|
| 43 |
const data = JSON.parse(fs.readFileSync(VERSION_FILE, 'utf-8'));
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
} catch (e) {
|
| 46 |
console.error('Failed to load stored SHA:', e);
|
| 47 |
return null;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
|
|
|
| 51 |
function saveStoredSHA(sha) {
|
| 52 |
try {
|
| 53 |
-
if (!fs.existsSync(DATA_DIR))
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
} catch (e) {
|
| 56 |
console.error('Failed to save SHA:', e);
|
| 57 |
}
|
|
@@ -109,20 +148,19 @@ async function fetchLatestSHA() {
|
|
| 109 |
const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}/commits/main`);
|
| 110 |
const data = await res.json();
|
| 111 |
latestSHA = data.sha;
|
| 112 |
-
console.log(
|
| 113 |
-
saveStoredSHA(latestSHA);
|
| 114 |
} catch (e) {
|
| 115 |
console.error('Failed to fetch latest commit SHA', e);
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
-
// Load persisted SHA if exists; otherwise fetch latest
|
| 120 |
latestSHA = loadStoredSHA();
|
| 121 |
if (!latestSHA) {
|
| 122 |
-
console.log(
|
| 123 |
await fetchLatestSHA();
|
| 124 |
} else {
|
| 125 |
-
console.log(
|
| 126 |
}
|
| 127 |
|
| 128 |
// --- Admin endpoints ---
|
|
@@ -157,8 +195,8 @@ app.get('/admin/refresh', verifyLimiter, async (req, res) => {
|
|
| 157 |
if (sha) {
|
| 158 |
if (!/^[0-9a-f]{7,40}$/.test(sha)) return res.status(400).send('Invalid SHA');
|
| 159 |
latestSHA = sha;
|
| 160 |
-
saveStoredSHA(latestSHA);
|
| 161 |
-
console.log(`Manual SHA set by admin: ${latestSHA}`);
|
| 162 |
return res.send(`Version set to commit ${latestSHA}`);
|
| 163 |
}
|
| 164 |
|
|
|
|
| 35 |
|
| 36 |
const DATA_DIR = path.join(__dirname, 'data');
|
| 37 |
const VERSION_FILE = path.join(DATA_DIR, 'version.json');
|
| 38 |
+
const PUBLIC_URL = process.env.PUBLIC_URL || 'default';
|
| 39 |
+
|
| 40 |
|
| 41 |
function loadStoredSHA() {
|
| 42 |
try {
|
| 43 |
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
|
| 44 |
if (!fs.existsSync(VERSION_FILE)) return null;
|
| 45 |
const data = JSON.parse(fs.readFileSync(VERSION_FILE, 'utf-8'));
|
| 46 |
+
|
| 47 |
+
// Find SHA for this PUBLIC_URL
|
| 48 |
+
const obj = data.find(item => item[PUBLIC_URL]);
|
| 49 |
+
return obj ? obj[PUBLIC_URL] : null;
|
| 50 |
} catch (e) {
|
| 51 |
console.error('Failed to load stored SHA:', e);
|
| 52 |
return null;
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
+
|
| 57 |
function saveStoredSHA(sha) {
|
| 58 |
try {
|
| 59 |
+
if (!fs.existsSync(DATA_DIR)) {
|
| 60 |
+
fs.mkdirSync(DATA_DIR, { recursive: true });
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
let data = [];
|
| 64 |
+
|
| 65 |
+
if (fs.existsSync(VERSION_FILE)) {
|
| 66 |
+
try {
|
| 67 |
+
data = JSON.parse(fs.readFileSync(VERSION_FILE, 'utf-8'));
|
| 68 |
+
if (!Array.isArray(data)) data = [];
|
| 69 |
+
} catch {
|
| 70 |
+
data = [];
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
let found = false;
|
| 75 |
+
|
| 76 |
+
for (const entry of data) {
|
| 77 |
+
if (entry[PUBLIC_URL]) {
|
| 78 |
+
entry[PUBLIC_URL] = sha;
|
| 79 |
+
found = true;
|
| 80 |
+
break;
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
if (!found) {
|
| 85 |
+
data.push({ [PUBLIC_URL]: sha });
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
fs.writeFileSync(
|
| 89 |
+
VERSION_FILE,
|
| 90 |
+
JSON.stringify(data, null, 2),
|
| 91 |
+
'utf-8'
|
| 92 |
+
);
|
| 93 |
+
|
| 94 |
} catch (e) {
|
| 95 |
console.error('Failed to save SHA:', e);
|
| 96 |
}
|
|
|
|
| 148 |
const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}/commits/main`);
|
| 149 |
const data = await res.json();
|
| 150 |
latestSHA = data.sha;
|
| 151 |
+
console.log(`[${PUBLIC_URL}] Updated latest SHA: ${latestSHA}`);
|
| 152 |
+
saveStoredSHA(latestSHA);
|
| 153 |
} catch (e) {
|
| 154 |
console.error('Failed to fetch latest commit SHA', e);
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
|
|
|
| 158 |
latestSHA = loadStoredSHA();
|
| 159 |
if (!latestSHA) {
|
| 160 |
+
console.log(`[${PUBLIC_URL}] No stored SHA found, fetching latest...`);
|
| 161 |
await fetchLatestSHA();
|
| 162 |
} else {
|
| 163 |
+
console.log(`[${PUBLIC_URL}] Using stored SHA: ${latestSHA}`);
|
| 164 |
}
|
| 165 |
|
| 166 |
// --- Admin endpoints ---
|
|
|
|
| 195 |
if (sha) {
|
| 196 |
if (!/^[0-9a-f]{7,40}$/.test(sha)) return res.status(400).send('Invalid SHA');
|
| 197 |
latestSHA = sha;
|
| 198 |
+
saveStoredSHA(latestSHA);
|
| 199 |
+
console.log(`[${PUBLIC_URL}] Manual SHA set by admin: ${latestSHA}`);
|
| 200 |
return res.send(`Version set to commit ${latestSHA}`);
|
| 201 |
}
|
| 202 |
|