Spaces:
Running
Running
Commit ·
4812e1d
1
Parent(s): fef4454
refactor server.js to support configurable data file path and ensure directory structure on startup
Browse files
server.js
CHANGED
|
@@ -5,18 +5,22 @@ const { randomUUID } = require('crypto');
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const PORT = process.env.PORT || 3000;
|
| 8 |
-
|
|
|
|
| 9 |
const IMAGES_DIR = path.join(__dirname, 'images');
|
| 10 |
|
| 11 |
app.use(express.json());
|
| 12 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 13 |
app.use('/images', express.static(IMAGES_DIR));
|
| 14 |
|
| 15 |
-
//
|
| 16 |
-
['
|
| 17 |
const p = path.join(__dirname, dir);
|
| 18 |
if (!fs.existsSync(p)) fs.mkdirSync(p, { recursive: true });
|
| 19 |
});
|
|
|
|
|
|
|
|
|
|
| 20 |
if (!fs.existsSync(DATA_FILE)) {
|
| 21 |
fs.writeFileSync(DATA_FILE, JSON.stringify({ sessions: [], trials: [] }, null, 2));
|
| 22 |
}
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const PORT = process.env.PORT || 3000;
|
| 8 |
+
// On HF Spaces the persistent bucket is mounted at /data; locally falls back to ./data/
|
| 9 |
+
const DATA_FILE = process.env.DATA_PATH || path.join(__dirname, 'data', 'results.json');
|
| 10 |
const IMAGES_DIR = path.join(__dirname, 'images');
|
| 11 |
|
| 12 |
app.use(express.json());
|
| 13 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 14 |
app.use('/images', express.static(IMAGES_DIR));
|
| 15 |
|
| 16 |
+
// Ensure local image dirs exist (no-op when /data is an external mount)
|
| 17 |
+
['images/real', 'images/fake'].forEach(dir => {
|
| 18 |
const p = path.join(__dirname, dir);
|
| 19 |
if (!fs.existsSync(p)) fs.mkdirSync(p, { recursive: true });
|
| 20 |
});
|
| 21 |
+
// Ensure the data file's parent directory exists, then seed an empty store
|
| 22 |
+
const dataDir = path.dirname(DATA_FILE);
|
| 23 |
+
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
|
| 24 |
if (!fs.existsSync(DATA_FILE)) {
|
| 25 |
fs.writeFileSync(DATA_FILE, JSON.stringify({ sessions: [], trials: [] }, null, 2));
|
| 26 |
}
|