Add base-path deployment support
This commit is contained in:
41
server.js
41
server.js
@@ -3,6 +3,7 @@ import { readFile } from "node:fs/promises";
|
||||
import { extname } from "node:path";
|
||||
|
||||
const PORT = Number(process.env.PORT || 5173);
|
||||
const BASE_PATH = normalizeBasePath(process.env.DOCLINK_BASE_PATH || "");
|
||||
const API_BASE =
|
||||
process.env.DOCLINK_API_BASE ||
|
||||
"https://devbandungraya.aplikasi.web.id/one-api-doctor/doctor_mitra";
|
||||
@@ -23,6 +24,36 @@ function escapeHtml(value) {
|
||||
.replaceAll('"', """);
|
||||
}
|
||||
|
||||
function normalizeBasePath(value) {
|
||||
const raw = String(value || "").trim();
|
||||
if (!raw || raw === "/") return "";
|
||||
const withLeadingSlash = raw.startsWith("/") ? raw : `/${raw}`;
|
||||
return withLeadingSlash.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function appPath(pathname = "/") {
|
||||
const text = String(pathname || "/");
|
||||
if (/^https?:\/\//i.test(text) || text.startsWith("//")) return text;
|
||||
if (!BASE_PATH) return text.startsWith("/") ? text : `/${text}`;
|
||||
if (text === BASE_PATH || text.startsWith(`${BASE_PATH}/`)) return text;
|
||||
if (text === "/" || text === "") return BASE_PATH;
|
||||
if (text.startsWith("/")) return `${BASE_PATH}${text}`;
|
||||
return `${BASE_PATH}/${text}`;
|
||||
}
|
||||
|
||||
function stripBasePath(pathname) {
|
||||
const path = String(pathname || "/");
|
||||
if (!BASE_PATH) return path || "/";
|
||||
if (path === BASE_PATH) return "/";
|
||||
if (path.startsWith(`${BASE_PATH}/`)) return path.slice(BASE_PATH.length) || "/";
|
||||
return path;
|
||||
}
|
||||
|
||||
function rewriteHtmlPaths(html) {
|
||||
if (!BASE_PATH || typeof html !== "string") return html;
|
||||
return html.replace(/\b(href|src|action|hx-get|hx-post)="\/(?!\/)/g, `$1="${BASE_PATH}/`);
|
||||
}
|
||||
|
||||
function statusClass(status) {
|
||||
const mapping = {
|
||||
Processing: "warning",
|
||||
@@ -1998,13 +2029,15 @@ function json(res, status, payload) {
|
||||
}
|
||||
|
||||
function redirect(res, location, headers = {}) {
|
||||
res.writeHead(302, { Location: location, ...headers });
|
||||
const resolvedLocation = typeof location === "string" && location.startsWith("/") ? appPath(location) : location;
|
||||
res.writeHead(302, { Location: resolvedLocation, ...headers });
|
||||
res.end();
|
||||
}
|
||||
|
||||
function html(res, status, body, headers = {}) {
|
||||
const output = rewriteHtmlPaths(body);
|
||||
res.writeHead(status, { "Content-Type": "text/html; charset=utf-8", ...headers });
|
||||
res.end(body);
|
||||
res.end(output);
|
||||
}
|
||||
|
||||
function isHtmx(req) {
|
||||
@@ -2204,7 +2237,7 @@ async function fragmentResultDetail(session, resultId) {
|
||||
}
|
||||
|
||||
async function renderRoute(req, res, url) {
|
||||
const path = url.pathname;
|
||||
const path = stripBasePath(url.pathname);
|
||||
const query = Object.fromEntries(url.searchParams.entries());
|
||||
const session = readSession(req);
|
||||
const authed = Boolean(session);
|
||||
@@ -2572,7 +2605,7 @@ async function renderRoute(req, res, url) {
|
||||
return;
|
||||
}
|
||||
|
||||
html(res, 404, emptyRoute(path));
|
||||
html(res, 404, emptyRoute(url.pathname));
|
||||
}
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user