forked from izu/student-web-if-development-kit
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a24e2e14e |
@@ -0,0 +1,4 @@
|
|||||||
|
DASHBOARD_TARGET_URL=https://dashboard.informatika.untan.ac.id/index.php?penelitian
|
||||||
|
DASHBOARD_PROXY_1=https://api.codetabs.com/v1/proxy?quest={{url}}
|
||||||
|
DASHBOARD_PROXY_2=https://api.allorigins.win/get?url={{url}}&ts={{ts}}
|
||||||
|
DASHBOARD_PROXY_3=https://thingproxy.freeboard.io/fetch/{{rawUrl}}
|
||||||
+48
-19
@@ -76,7 +76,13 @@
|
|||||||
window.APP_CONFIG = window.APP_CONFIG || {
|
window.APP_CONFIG = window.APP_CONFIG || {
|
||||||
debugLogs: false,
|
debugLogs: false,
|
||||||
dashboardRuntimeFetch: 'always',
|
dashboardRuntimeFetch: 'always',
|
||||||
dashboardApiUrl: 'https://proxykbk.netlify.app/.netlify/functions/dashboard',
|
dashboardApiUrl: 'api/dashboard',
|
||||||
|
dashboardTargetUrl: 'https://dashboard.informatika.untan.ac.id/index.php?penelitian',
|
||||||
|
dashboardProxyTemplates: [
|
||||||
|
'https://api.codetabs.com/v1/proxy?quest={{url}}',
|
||||||
|
'https://api.allorigins.win/get?url={{url}}&ts={{ts}}',
|
||||||
|
'https://thingproxy.freeboard.io/fetch/{{rawUrl}}'
|
||||||
|
]
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
<!-- RODA_DASHBOARD_DATA -->
|
<!-- RODA_DASHBOARD_DATA -->
|
||||||
@@ -1656,33 +1662,56 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let lastError = null;
|
let lastError = null;
|
||||||
|
|
||||||
if (isDashboardRuntimeFetchAllowed()) {
|
|
||||||
const dashboardApiUrl = window.APP_CONFIG?.dashboardApiUrl?.trim();
|
|
||||||
|
|
||||||
if (dashboardApiUrl) {
|
|
||||||
try {
|
|
||||||
const rows = await fetchProxyRows(dashboardApiUrl);
|
|
||||||
applyDashboardRows(rows);
|
|
||||||
return;
|
|
||||||
} catch (error) {
|
|
||||||
lastError = error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback ke data yang di-embed saat build jika runtime fetch gagal atau dinonaktifkan
|
|
||||||
const embeddedHtml = readEmbeddedDashboardHtml();
|
const embeddedHtml = readEmbeddedDashboardHtml();
|
||||||
if (embeddedHtml) {
|
if (embeddedHtml) {
|
||||||
try {
|
try {
|
||||||
applyDashboardRows(parseDashboardRows(embeddedHtml));
|
applyDashboardRows(parseDashboardRows(embeddedHtml));
|
||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
lastError = lastError || error;
|
lastError = error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderRisetFeedback('Pemuatan Data Gagal', getDashboardErrorMessage(lastError || new Error('DASHBOARD_RUNTIME_FETCH_DISABLED')), 'Coba Lagi', 'fetchDashboardData()');
|
if (!isDashboardRuntimeFetchAllowed()) {
|
||||||
|
renderRisetFeedback('Pemuatan Data Gagal', getDashboardErrorMessage(lastError || new Error('DASHBOARD_RUNTIME_FETCH_DISABLED')), 'Coba Lagi', 'fetchDashboardData()');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetUrl = window.APP_CONFIG?.dashboardTargetUrl?.trim();
|
||||||
|
const dashboardApiUrl = window.APP_CONFIG?.dashboardApiUrl?.trim();
|
||||||
|
const proxyTemplates = Array.isArray(window.APP_CONFIG?.dashboardProxyTemplates)
|
||||||
|
? window.APP_CONFIG.dashboardProxyTemplates.filter(Boolean)
|
||||||
|
: [];
|
||||||
|
|
||||||
|
const proxyServers = dashboardApiUrl ? [() => dashboardApiUrl] : [];
|
||||||
|
|
||||||
|
if (targetUrl) {
|
||||||
|
proxyTemplates.forEach((template) => {
|
||||||
|
proxyServers.push((url) => template
|
||||||
|
.replaceAll('{{url}}', encodeURIComponent(url))
|
||||||
|
.replaceAll('{{rawUrl}}', url)
|
||||||
|
.replaceAll('{{ts}}', String(Date.now())));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!proxyServers.length) {
|
||||||
|
renderRisetFeedback('Pemuatan Data Gagal', getDashboardErrorMessage(new Error('DASHBOARD_PROXY_NOT_CONFIGURED')), 'Coba Lagi', 'fetchDashboardData()');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const proxyUrls = proxyServers.map((getProxyUrl) => getProxyUrl(targetUrl));
|
||||||
|
const rows = await Promise.any(proxyUrls.map((proxyUrl) => fetchProxyRows(proxyUrl)));
|
||||||
|
applyDashboardRows(rows);
|
||||||
|
return;
|
||||||
|
} catch (error) {
|
||||||
|
lastError = error instanceof AggregateError && error.errors?.length
|
||||||
|
? error.errors[error.errors.length - 1]
|
||||||
|
: error;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Jika semua proxy gagal
|
||||||
|
renderRisetFeedback('Pemuatan Data Gagal', getDashboardErrorMessage(lastError), 'Coba Lagi', 'fetchDashboardData()');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+81
-21
@@ -6,21 +6,69 @@ const { URL } = require('url');
|
|||||||
const ROOT_DIR = __dirname;
|
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 HOST = process.env.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 DASHBOARD_TARGET_URL = 'https://dashboard.informatika.untan.ac.id/index.php?penelitian';
|
const DEFAULT_DASHBOARD_TARGET_URL = 'https://dashboard.informatika.untan.ac.id/index.php?penelitian';
|
||||||
const RODA_PATH_PREFIX = '/groups/RODA';
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseEnvFile(filePath) {
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const env = {};
|
||||||
|
const lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
|
||||||
|
|
||||||
|
for (const rawLine of lines) {
|
||||||
|
const line = rawLine.trim();
|
||||||
|
if (!line || line.startsWith('#')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const separatorIndex = line.indexOf('=');
|
||||||
|
if (separatorIndex === -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = line.slice(0, separatorIndex).trim();
|
||||||
|
let value = line.slice(separatorIndex + 1).trim();
|
||||||
|
|
||||||
|
if (
|
||||||
|
(value.startsWith('"') && value.endsWith('"')) ||
|
||||||
|
(value.startsWith("'") && value.endsWith("'"))
|
||||||
|
) {
|
||||||
|
value = value.slice(1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
env[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
function getAppConfig() {
|
function getAppConfig() {
|
||||||
|
const env = {
|
||||||
|
...parseEnvFile(ENV_PATH),
|
||||||
|
...process.env
|
||||||
|
};
|
||||||
|
|
||||||
|
const dashboardProxyTemplates = [
|
||||||
|
env.DASHBOARD_PROXY_1,
|
||||||
|
env.DASHBOARD_PROXY_2,
|
||||||
|
env.DASHBOARD_PROXY_3
|
||||||
|
].filter(Boolean);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
debugLogs: false,
|
debugLogs: env.DEBUG_LOGS === 'true',
|
||||||
dashboardRuntimeFetch: 'always',
|
dashboardRuntimeFetch: env.DASHBOARD_RUNTIME_FETCH || 'always',
|
||||||
dashboardApiUrl: 'api/dashboard',
|
dashboardApiUrl: env.DASHBOARD_API_URL || 'api/dashboard',
|
||||||
dashboardTargetUrl: DASHBOARD_TARGET_URL,
|
dashboardTargetUrl: env.DASHBOARD_TARGET_URL || DEFAULT_DASHBOARD_TARGET_URL,
|
||||||
|
dashboardProxyTemplates
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +76,10 @@ const DASHBOARD_CACHE_TTL_MS = 5 * 60 * 1000;
|
|||||||
let dashboardCache = { html: null, timestamp: 0 };
|
let dashboardCache = { html: null, timestamp: 0 };
|
||||||
|
|
||||||
async function fetchDashboardHtml() {
|
async function fetchDashboardHtml() {
|
||||||
|
const targetUrl = getAppConfig().dashboardTargetUrl;
|
||||||
|
if (!targetUrl) {
|
||||||
|
throw new Error('DASHBOARD_TARGET_URL kosong');
|
||||||
|
}
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (dashboardCache.html && (now - dashboardCache.timestamp) < DASHBOARD_CACHE_TTL_MS) {
|
if (dashboardCache.html && (now - dashboardCache.timestamp) < DASHBOARD_CACHE_TTL_MS) {
|
||||||
@@ -37,7 +89,7 @@ async function fetchDashboardHtml() {
|
|||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timeoutId = setTimeout(() => controller.abort(), 12000);
|
const timeoutId = setTimeout(() => controller.abort(), 12000);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(DASHBOARD_TARGET_URL, {
|
const response = await fetch(targetUrl, {
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36'
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36'
|
||||||
@@ -59,14 +111,35 @@ async function fetchDashboardHtml() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateAppConfig(config) {
|
||||||
|
const errors = [];
|
||||||
|
|
||||||
|
if (!config.dashboardTargetUrl) {
|
||||||
|
errors.push('DASHBOARD_TARGET_URL belum diisi.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors;
|
||||||
|
}
|
||||||
|
|
||||||
function injectConfig(html) {
|
function injectConfig(html) {
|
||||||
const configScript = `<script>window.APP_CONFIG = ${JSON.stringify(getAppConfig())};</script>`;
|
const configScript = `<script>window.APP_CONFIG = ${JSON.stringify(getAppConfig())};</script>`;
|
||||||
|
const defaultConfigScript = '<script>\n window.APP_CONFIG = window.APP_CONFIG || {\n dashboardTargetUrl: \'\',\n dashboardProxyTemplates: []\n };\n </script>';
|
||||||
|
|
||||||
|
if (html.includes(defaultConfigScript)) {
|
||||||
|
return html.replace(defaultConfigScript, configScript);
|
||||||
|
}
|
||||||
|
|
||||||
return html.replace('</head>', ` ${configScript}\n</head>`);
|
return html.replace('</head>', ` ${configScript}\n</head>`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendHtml(res) {
|
function sendHtml(res) {
|
||||||
try {
|
try {
|
||||||
const html = fs.readFileSync(INDEX_PATH, 'utf8');
|
const html = fs.readFileSync(INDEX_PATH, 'utf8');
|
||||||
|
const configErrors = validateAppConfig(getAppConfig());
|
||||||
|
if (configErrors.length) {
|
||||||
|
logServerError('config', new Error('Invalid app config'), { configErrors });
|
||||||
|
}
|
||||||
|
|
||||||
const withConfig = injectConfig(html);
|
const withConfig = injectConfig(html);
|
||||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||||
res.end(withConfig);
|
res.end(withConfig);
|
||||||
@@ -146,30 +219,17 @@ const server = http.createServer((req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pathname === '/api/dashboard' || pathname === `${RODA_PATH_PREFIX}/api/dashboard`) {
|
if (pathname === '/api/dashboard' || pathname === `${RODA_PATH_PREFIX}/api/dashboard`) {
|
||||||
if (req.method === 'OPTIONS') {
|
|
||||||
res.writeHead(204, {
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Methods': 'GET',
|
|
||||||
});
|
|
||||||
res.end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fetchDashboardHtml()
|
fetchDashboardHtml()
|
||||||
.then(({ html, cached }) => {
|
.then(({ html, cached }) => {
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'Content-Type': 'text/html; charset=utf-8',
|
'Content-Type': 'text/html; charset=utf-8',
|
||||||
'Access-Control-Allow-Origin': '*',
|
'X-Cache': cached
|
||||||
'X-Cache': cached,
|
|
||||||
});
|
});
|
||||||
res.end(html);
|
res.end(html);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
logServerError('api-dashboard', error);
|
logServerError('api-dashboard', error);
|
||||||
res.writeHead(502, {
|
res.writeHead(502, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||||
'Content-Type': 'text/plain; charset=utf-8',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
});
|
|
||||||
res.end('DASHBOARD_FETCH_FAILED');
|
res.end('DASHBOARD_FETCH_FAILED');
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user