forked from izu/student-web-if-development-kit
Use RODA server for dashboard proxy
This commit is contained in:
@@ -76,6 +76,7 @@
|
|||||||
window.APP_CONFIG = window.APP_CONFIG || {
|
window.APP_CONFIG = window.APP_CONFIG || {
|
||||||
debugLogs: false,
|
debugLogs: false,
|
||||||
dashboardRuntimeFetch: 'always',
|
dashboardRuntimeFetch: 'always',
|
||||||
|
dashboardApiUrl: 'api/dashboard',
|
||||||
dashboardTargetUrl: 'https://dashboard.informatika.untan.ac.id/index.php?penelitian',
|
dashboardTargetUrl: 'https://dashboard.informatika.untan.ac.id/index.php?penelitian',
|
||||||
dashboardProxyTemplates: [
|
dashboardProxyTemplates: [
|
||||||
'https://api.codetabs.com/v1/proxy?quest={{url}}',
|
'https://api.codetabs.com/v1/proxy?quest={{url}}',
|
||||||
@@ -1677,14 +1678,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const targetUrl = window.APP_CONFIG?.dashboardTargetUrl?.trim();
|
const targetUrl = window.APP_CONFIG?.dashboardTargetUrl?.trim();
|
||||||
|
const dashboardApiUrl = window.APP_CONFIG?.dashboardApiUrl?.trim();
|
||||||
const proxyTemplates = Array.isArray(window.APP_CONFIG?.dashboardProxyTemplates)
|
const proxyTemplates = Array.isArray(window.APP_CONFIG?.dashboardProxyTemplates)
|
||||||
? window.APP_CONFIG.dashboardProxyTemplates.filter(Boolean)
|
? window.APP_CONFIG.dashboardProxyTemplates.filter(Boolean)
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
// Endpoint server.js hanya ada saat development lokal.
|
const proxyServers = dashboardApiUrl ? [() => dashboardApiUrl] : [];
|
||||||
const proxyServers = shouldUseLocalDashboardProxy() ? [() => '/api/dashboard'] : [];
|
|
||||||
|
|
||||||
// Deployment static memakai proxy publik karena tidak menjalankan server.js.
|
|
||||||
if (targetUrl) {
|
if (targetUrl) {
|
||||||
proxyTemplates.forEach((template) => {
|
proxyTemplates.forEach((template) => {
|
||||||
proxyServers.push((url) => template
|
proxyServers.push((url) => template
|
||||||
|
|||||||
+22
-9
@@ -7,8 +7,10 @@ const ROOT_DIR = __dirname;
|
|||||||
const PARENT_DIR = path.join(__dirname, '..', '..');
|
const PARENT_DIR = path.join(__dirname, '..', '..');
|
||||||
const INDEX_PATH = path.join(ROOT_DIR, 'index.html');
|
const INDEX_PATH = path.join(ROOT_DIR, 'index.html');
|
||||||
const ENV_PATH = path.join(ROOT_DIR, '.env');
|
const ENV_PATH = path.join(ROOT_DIR, '.env');
|
||||||
const HOST = '127.0.0.1';
|
const HOST = process.env.HOST || '127.0.0.1';
|
||||||
const PORT = Number(process.env.PORT || 3000);
|
const PORT = Number(process.env.PORT || 3000);
|
||||||
|
const DEFAULT_DASHBOARD_TARGET_URL = 'https://dashboard.informatika.untan.ac.id/index.php?penelitian';
|
||||||
|
const RODA_PATH_PREFIX = '/groups/RODA';
|
||||||
|
|
||||||
function logServerError(scope, error, extra = {}) {
|
function logServerError(scope, error, extra = {}) {
|
||||||
console.error(`[${scope}]`, error && error.stack ? error.stack : error, extra);
|
console.error(`[${scope}]`, error && error.stack ? error.stack : error, extra);
|
||||||
@@ -62,7 +64,10 @@ function getAppConfig() {
|
|||||||
].filter(Boolean);
|
].filter(Boolean);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dashboardTargetUrl: env.DASHBOARD_TARGET_URL || '',
|
debugLogs: env.DEBUG_LOGS === 'true',
|
||||||
|
dashboardRuntimeFetch: env.DASHBOARD_RUNTIME_FETCH || 'always',
|
||||||
|
dashboardApiUrl: env.DASHBOARD_API_URL || 'api/dashboard',
|
||||||
|
dashboardTargetUrl: env.DASHBOARD_TARGET_URL || DEFAULT_DASHBOARD_TARGET_URL,
|
||||||
dashboardProxyTemplates
|
dashboardProxyTemplates
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -113,10 +118,6 @@ function validateAppConfig(config) {
|
|||||||
errors.push('DASHBOARD_TARGET_URL belum diisi.');
|
errors.push('DASHBOARD_TARGET_URL belum diisi.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(config.dashboardProxyTemplates) || !config.dashboardProxyTemplates.length) {
|
|
||||||
errors.push('Minimal satu DASHBOARD_PROXY_* harus diisi.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,17 +196,29 @@ function sendNotFound(res) {
|
|||||||
res.end('Not found');
|
res.end('Not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getRodaStaticPathname(pathname) {
|
||||||
|
if (pathname === RODA_PATH_PREFIX || pathname === `${RODA_PATH_PREFIX}/` || pathname === `${RODA_PATH_PREFIX}/index.html`) {
|
||||||
|
return '/index.html';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pathname.startsWith(`${RODA_PATH_PREFIX}/`)) {
|
||||||
|
return pathname.slice(RODA_PATH_PREFIX.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pathname;
|
||||||
|
}
|
||||||
|
|
||||||
const server = http.createServer((req, res) => {
|
const server = http.createServer((req, res) => {
|
||||||
try {
|
try {
|
||||||
const requestUrl = new URL(req.url, `http://${req.headers.host}`);
|
const requestUrl = new URL(req.url, `http://${req.headers.host}`);
|
||||||
const pathname = requestUrl.pathname;
|
const pathname = requestUrl.pathname;
|
||||||
|
|
||||||
if (pathname === '/' || pathname === '/index.html') {
|
if (pathname === '/' || pathname === '/index.html' || pathname === RODA_PATH_PREFIX || pathname === `${RODA_PATH_PREFIX}/` || pathname === `${RODA_PATH_PREFIX}/index.html`) {
|
||||||
sendHtml(res);
|
sendHtml(res);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pathname === '/api/dashboard') {
|
if (pathname === '/api/dashboard' || pathname === `${RODA_PATH_PREFIX}/api/dashboard`) {
|
||||||
fetchDashboardHtml()
|
fetchDashboardHtml()
|
||||||
.then(({ html, cached }) => {
|
.then(({ html, cached }) => {
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
@@ -237,7 +250,7 @@ const server = http.createServer((req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Serve static files from ROOT_DIR
|
// Serve static files from ROOT_DIR
|
||||||
const filepath = path.join(ROOT_DIR, pathname);
|
const filepath = path.join(ROOT_DIR, getRodaStaticPathname(pathname));
|
||||||
const normalizedPath = path.normalize(filepath);
|
const normalizedPath = path.normalize(filepath);
|
||||||
|
|
||||||
// Security check: ensure the requested file is within ROOT_DIR
|
// Security check: ensure the requested file is within ROOT_DIR
|
||||||
|
|||||||
+3
-1
@@ -5,7 +5,9 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "Student-safe static page assignment kit for Informatika UNTAN",
|
"description": "Student-safe static page assignment kit for Informatika UNTAN",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "node groups/RODA/server.js",
|
||||||
|
"dev:vite": "vite",
|
||||||
|
"dev:roda": "node groups/RODA/server.js",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"test:tapops": "node --test groups/Tapops/tests/*.test.mjs",
|
"test:tapops": "node --test groups/Tapops/tests/*.test.mjs",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
|
|||||||
Reference in New Issue
Block a user