nirkyy commited on
Commit
3caea19
·
verified ·
1 Parent(s): 0f48397

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +12 -15
Dockerfile CHANGED
@@ -6,7 +6,7 @@ RUN apk add --no-cache git
6
  WORKDIR /app
7
  # Buat direktori api dan direktori publik untuk struktur awal
8
  RUN mkdir -p /app/api
9
- # Salin script server utama yang akan memindai folder api secara dinamis dengan escape '$' yang benar
10
  RUN cat << 'EOF' > server.js
11
  const express = require('express');
12
  const fs = require('fs');
@@ -28,9 +28,9 @@ while ((match = requireRegex.exec(content)) !== null) {
28
  const pkg = match[1];
29
  // Abaikan built-in modules Node.js atau relative path
30
  if (!pkg.startsWith('.') && !pkg.startsWith('/')) {
31
- // Ambil nama package utama (misal: lodash/clone -> lodash)
32
  const parts = pkg.split('/');
33
- const pkgName = pkg.startsWith('@') ? ${parts[0]}/${parts[1]} : parts[0];
 
34
  dependencies.add(pkgName);
35
  }
36
  }
@@ -40,7 +40,7 @@ while ((match = importRegex.exec(content)) !== null) {
40
  const pkg = match[1];
41
  if (!pkg.startsWith('.') && !pkg.startsWith('/')) {
42
  const parts = pkg.split('/');
43
- const pkgName = pkg.startsWith('@') ? ${parts[0]}/${parts[1]} : parts[0];
44
  dependencies.add(pkgName);
45
  }
46
  }
@@ -78,7 +78,7 @@ const pkgData = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
78
  let needsInstall = false;
79
  for (const dep of allNeededDeps) {
80
  if (!pkgData.dependencies[dep]) {
81
- console.log([*] Mendeteksi dependency baru: ${dep});
82
  pkgData.dependencies[dep] = "*";
83
  needsInstall = true;
84
  }
@@ -92,30 +92,27 @@ execSync('npm install --production', { stdio: 'inherit' });
92
  for (const file of files) {
93
  if (file.endsWith('.js') || file.endsWith('.mjs')) {
94
  const fileNameWithoutExt = path.parse(file).name;
95
- const routePath = /api/${fileNameWithoutExt};
96
  const filePath = path.join(API_DIR, file);
97
- // Gunakan dynamic import agar support ESM maupun CJS
98
  try {
99
- // Buat handler router universal untuk metode GET, POST, PUT, DELETE, dll.
100
  app.all(routePath, async (req, res) => {
101
  try {
102
- // Import file secara dinamis dengan cache-busting timestamp
103
- const fileUrl = require('url').pathToFileURL(filePath).href + ?update=${Date.now()};
104
  const importedModule = await import(fileUrl);
105
  const handler = importedModule.default || importedModule;
106
  if (typeof handler === 'function') {
107
  return await handler(req, res);
108
  } else {
109
- return res.status(500).json({ error: File ${file} tidak mengekspor fungsi handler yang valid (req, res). });
110
  }
111
  } catch (err) {
112
- console.error([Error di endpoint ${routePath}]:, err);
113
  return res.status(500).json({ error: err.message, stack: err.stack });
114
  }
115
  });
116
- console.log([+] Endpoint terdaftar: ${routePath} (dari api/${file}));
117
  } catch (err) {
118
- console.error([-] Gagal memuat file api/${file}:, err.message);
119
  }
120
  }
121
  }
@@ -127,7 +124,7 @@ res.json({ status: "Server Aktif", port: PORT, timestamp: new Date() });
127
  // Jalankan registrasi rute dan jalankan server
128
  registerApiRoutes().then(() => {
129
  app.listen(PORT, () => {
130
- console.log([🚀] Server berjalan di port ${PORT});
131
  });
132
  });
133
  EOF
 
6
  WORKDIR /app
7
  # Buat direktori api dan direktori publik untuk struktur awal
8
  RUN mkdir -p /app/api
9
+ # Salin script server utama yang menggunakan konkatenasi biasa agar aman dari escape shell
10
  RUN cat << 'EOF' > server.js
11
  const express = require('express');
12
  const fs = require('fs');
 
28
  const pkg = match[1];
29
  // Abaikan built-in modules Node.js atau relative path
30
  if (!pkg.startsWith('.') && !pkg.startsWith('/')) {
 
31
  const parts = pkg.split('/');
32
+ // Gunakan konkatenasi string biasa untuk menghindari error escape
33
+ const pkgName = pkg.startsWith('@') ? parts[0] + '/' + parts[1] : parts[0];
34
  dependencies.add(pkgName);
35
  }
36
  }
 
40
  const pkg = match[1];
41
  if (!pkg.startsWith('.') && !pkg.startsWith('/')) {
42
  const parts = pkg.split('/');
43
+ const pkgName = pkg.startsWith('@') ? parts[0] + '/' + parts[1] : parts[0];
44
  dependencies.add(pkgName);
45
  }
46
  }
 
78
  let needsInstall = false;
79
  for (const dep of allNeededDeps) {
80
  if (!pkgData.dependencies[dep]) {
81
+ console.log('[*] Mendeteksi dependency baru: ' + dep);
82
  pkgData.dependencies[dep] = "*";
83
  needsInstall = true;
84
  }
 
92
  for (const file of files) {
93
  if (file.endsWith('.js') || file.endsWith('.mjs')) {
94
  const fileNameWithoutExt = path.parse(file).name;
95
+ const routePath = '/api/' + fileNameWithoutExt;
96
  const filePath = path.join(API_DIR, file);
 
97
  try {
 
98
  app.all(routePath, async (req, res) => {
99
  try {
100
+ const fileUrl = require('url').pathToFileURL(filePath).href + '?update=' + Date.now();
 
101
  const importedModule = await import(fileUrl);
102
  const handler = importedModule.default || importedModule;
103
  if (typeof handler === 'function') {
104
  return await handler(req, res);
105
  } else {
106
+ return res.status(500).json({ error: 'File ' + file + ' tidak mengekspor fungsi handler yang valid (req, res).' });
107
  }
108
  } catch (err) {
109
+ console.error('[Error di endpoint ' + routePath + ']:', err);
110
  return res.status(500).json({ error: err.message, stack: err.stack });
111
  }
112
  });
113
+ console.log('[+] Endpoint terdaftar: ' + routePath + ' (dari api/' + file + ')');
114
  } catch (err) {
115
+ console.error('[-] Gagal memuat file api/' + file + ':', err.message);
116
  }
117
  }
118
  }
 
124
  // Jalankan registrasi rute dan jalankan server
125
  registerApiRoutes().then(() => {
126
  app.listen(PORT, () => {
127
+ console.log('[🚀] Server berjalan di port ' + PORT);
128
  });
129
  });
130
  EOF