puruu Puruu commited on
Commit ·
927c386
1
Parent(s): 27a7889
update
Browse files- Dockerfile +17 -0
- package.json +13 -0
- server.js +98 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gunakan base image Node.js versi LTS yang ringan
|
| 2 |
+
FROM node:20-alpine
|
| 3 |
+
|
| 4 |
+
# Atur direktori kerja di dalam container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Salin file package.json untuk caching dependensi
|
| 8 |
+
COPY package.json ./
|
| 9 |
+
|
| 10 |
+
# Instal dependensi Node.js
|
| 11 |
+
RUN npm install
|
| 12 |
+
|
| 13 |
+
# Salin sisa kode aplikasi
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Jalankan server menggunakan skrip dari package.json
|
| 17 |
+
CMD ["npm", "start"]
|
package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "nirkyy-database-backend",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"main": "server.js",
|
| 5 |
+
"scripts": {
|
| 6 |
+
"start": "node server.js"
|
| 7 |
+
},
|
| 8 |
+
"dependencies": {
|
| 9 |
+
"cors": "^2.8.5",
|
| 10 |
+
"express": "^4.19.2",
|
| 11 |
+
"lru-cache": "^10.2.2"
|
| 12 |
+
}
|
| 13 |
+
}
|
server.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const cors = require('cors');
|
| 3 |
+
const fs = require('fs');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
const { LRUCache } = require('lru-cache');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const PORT = process.env.PORT || 8080;
|
| 9 |
+
const dbDirectory = path.join(__dirname, 'database');
|
| 10 |
+
|
| 11 |
+
if (!fs.existsSync(dbDirectory)) {
|
| 12 |
+
fs.mkdirSync(dbDirectory);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const cache = new LRUCache({
|
| 16 |
+
max: 500,
|
| 17 |
+
ttl: 1000 * 60 * 60 * 24 * 30,
|
| 18 |
+
allowStale: false,
|
| 19 |
+
updateAgeOnGet: true,
|
| 20 |
+
updateAgeOnHas: true,
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
function loadDatabasesIntoCache() {
|
| 24 |
+
const ownerDirs = fs.readdirSync(dbDirectory);
|
| 25 |
+
for (const owner of ownerDirs) {
|
| 26 |
+
const ownerPath = path.join(dbDirectory, owner);
|
| 27 |
+
if (fs.statSync(ownerPath).isDirectory()) {
|
| 28 |
+
const files = fs.readdirSync(ownerPath);
|
| 29 |
+
for (const file of files) {
|
| 30 |
+
if (path.extname(file) === '.json') {
|
| 31 |
+
const dbName = path.basename(file, '.json');
|
| 32 |
+
const filePath = path.join(ownerPath, file);
|
| 33 |
+
const cacheKey = `${owner}_${dbName}`;
|
| 34 |
+
try {
|
| 35 |
+
const data = fs.readFileSync(filePath, 'utf8');
|
| 36 |
+
cache.set(cacheKey, JSON.parse(data));
|
| 37 |
+
} catch (e) {
|
| 38 |
+
console.error(`Gagal memuat ${file} untuk owner ${owner} ke cache.`, e);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
console.log('Semua database dari disk telah dimuat ke LRU Cache.');
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
app.use(cors());
|
| 48 |
+
app.use(express.json({ limit: '50mb' }));
|
| 49 |
+
|
| 50 |
+
app.get('/db/:owner/:name', (req, res) => {
|
| 51 |
+
const { owner, name } = req.params;
|
| 52 |
+
const cacheKey = `${owner}_${name}`;
|
| 53 |
+
const data = cache.get(cacheKey);
|
| 54 |
+
|
| 55 |
+
if (data) {
|
| 56 |
+
return res.status(200).json(data);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
const filePath = path.join(dbDirectory, owner, `${name}.json`);
|
| 60 |
+
if (fs.existsSync(filePath)) {
|
| 61 |
+
try {
|
| 62 |
+
const fileData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
| 63 |
+
cache.set(cacheKey, fileData);
|
| 64 |
+
return res.status(200).json(fileData);
|
| 65 |
+
} catch (e) {
|
| 66 |
+
return res.status(500).json({ success: false, message: 'Gagal membaca database.' });
|
| 67 |
+
}
|
| 68 |
+
} else {
|
| 69 |
+
return res.status(200).json({});
|
| 70 |
+
}
|
| 71 |
+
});
|
| 72 |
+
|
| 73 |
+
app.post('/db/:owner/:name', (req, res) => {
|
| 74 |
+
try {
|
| 75 |
+
const { owner, name } = req.params;
|
| 76 |
+
const data = req.body;
|
| 77 |
+
const cacheKey = `${owner}_${name}`;
|
| 78 |
+
|
| 79 |
+
cache.set(cacheKey, data);
|
| 80 |
+
|
| 81 |
+
const ownerDir = path.join(dbDirectory, owner);
|
| 82 |
+
if (!fs.existsSync(ownerDir)) {
|
| 83 |
+
fs.mkdirSync(ownerDir, { recursive: true });
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
const filePath = path.join(ownerDir, `${name}.json`);
|
| 87 |
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
| 88 |
+
|
| 89 |
+
res.status(200).json({ success: true, message: 'Database berhasil disimpan.' });
|
| 90 |
+
} catch (error) {
|
| 91 |
+
res.status(500).json({ success: false, message: 'Terjadi kesalahan internal.' });
|
| 92 |
+
}
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
app.listen(PORT, () => {
|
| 96 |
+
loadDatabasesIntoCache();
|
| 97 |
+
console.log(`Server database multi-tenant berjalan di port ${PORT}.`);
|
| 98 |
+
});
|