Files
controller-v2/server/index.ts
T

75 lines
2.4 KiB
TypeScript
Raw Normal View History

import express from "express";
import { createServer } from "http";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function startServer() {
const app = express();
const server = createServer(app);
2026-06-02 19:55:09 +08:00
const modernPath = path.resolve(__dirname, "..", "dist-modern");
const legacyPath = path.resolve(__dirname, "..", "dist-legacy");
2026-06-02 19:55:09 +08:00
app.use("/modern", express.static(modernPath));
app.use("/legacy", express.static(legacyPath));
2026-06-02 19:55:09 +08:00
function shouldUseLegacy(req: express.Request) {
// Prefer capability detection on client, but keep a UA escape hatch for very old WebViews.
const ua = req.headers["user-agent"] || "";
// Chrome 91 WebView frequently reports like: Chrome/91.0.4472.114
const m = String(ua).match(/Chrome\/(\d+)\./);
if (m) return Number(m[1]) <= 91;
return false;
}
// Bootstrap entry: redirect to /legacy or /modern while preserving hash routes.
app.get(["/", "/index.html", "/i.html"], (req, res) => {
const defaultToLegacy = shouldUseLegacy(req) ? "true" : "false";
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(`<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1" />
<title>Luxsin X8 Controller</title>
<script>
(function () {
function supportsModernCss() {
try {
return (
typeof CSS !== "undefined" &&
CSS.supports("color", "color-mix(in srgb, red 50%, blue)") &&
CSS.supports("height", "100dvh")
);
} catch (e) {
return false;
}
}
var useLegacy = ${defaultToLegacy} || !supportsModernCss();
var prefix = useLegacy ? "/x8/v2/legacy/" : "/x8/v2/modern/";
// Hash router: keep location.hash
location.replace(prefix + (location.hash || "#/"));
})();
</script>
</head>
<body></body>
</html>`);
});
// Hash routing: any other path should serve the bootstrap as well.
app.get("*", (_req, res) => {
2026-06-02 19:55:09 +08:00
res.redirect(302, "/x8/v2/");
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}/`);
});
}
startServer().catch(console.error);