diff --git a/Dockerfile b/Dockerfile index d90a6e952..ced0eef1d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,4 @@ RUN npm install COPY . . -CMD [ "node", "index.js" ] +CMD [ "node", "index.js" ] \ No newline at end of file diff --git a/index.js b/index.js index 41de1b134..4f006c353 100644 --- a/index.js +++ b/index.js @@ -1,89 +1,85 @@ -import express from 'express' -import basicAuth from 'express-basic-auth' -import http from 'node:http' -import { createBareServer } from '@tomphttp/bare-server-node' -import path from 'node:path' -import cors from 'cors' -import config from './config.js' - -const __dirname = process.cwd() -const server = http.createServer() -const app = express(server) -const bareServer = createBareServer('/o/') -const PORT = process.env.PORT || 8080 +import express from "express"; +import basicAuth from "express-basic-auth"; +import http from "node:http"; +import { createBareServer } from "@tomphttp/bare-server-node"; +import path from "node:path"; +import cors from "cors"; +import config from "./config.js"; + +const __dirname = process.cwd(); +const server = http.createServer(); +const app = express(server); +const bareServer = createBareServer("/o/"); +const PORT = process.env.PORT || 8080; if (config.challenge) { - console.log('Password protection is enabled. Usernames are: ' + Object.keys(config.users)) - console.log('Passwords are: ' + Object.values(config.users)) + console.log("Password protection is enabled. Usernames are: " + Object.keys(config.users)); + console.log("Passwords are: " + Object.values(config.users)); app.use( basicAuth({ users: config.users, challenge: true, }) - ) + ); } -app.use(express.json()) -app.use(express.urlencoded({ extended: true })) -app.use(cors()) -app.use(express.static(path.join(__dirname, 'static'))) +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); +app.use(cors()); +app.use(express.static(path.join(__dirname, "static"))); if (config.routes !== false) { const routes = [ - { path: '/ap', file: 'apps.html' }, - { path: '/g', file: 'games.html' }, - { path: '/s', file: 'settings.html' }, - { path: '/t', file: 'tabs.html' }, - { path: '/p', file: 'go.html' }, - { path: '/', file: 'index.html' }, - { path: '/tos', file: 'tos.html' }, - ] + { path: "/ap", file: "apps.html" }, + { path: "/g", file: "games.html" }, + { path: "/s", file: "settings.html" }, + { path: "/t", file: "tabs.html" }, + { path: "/p", file: "go.html" }, + { path: "/", file: "index.html" }, + { path: "/tos", file: "tos.html" }, + ]; routes.forEach((route) => { app.get(route.path, (req, res) => { - res.sendFile(path.join(__dirname, 'static', route.file)) - }) - }) + res.sendFile(path.join(__dirname, "static", route.file)); + }); + }); } if (config.local !== false) { - app.get('/e/*', (req, res, next) => { - const baseUrls = [ - 'https://raw.githubusercontent.com/v-5x/x/fixy', - 'https://raw.githubusercontent.com/ypxa/y/main', - 'https://raw.githubusercontent.com/ypxa/w/master', - ] - fetchData(req, res, next, baseUrls) - }) + app.get("/e/*", (req, res, next) => { + const baseUrls = ["https://raw.githubusercontent.com/v-5x/x/fixy", "https://raw.githubusercontent.com/ypxa/y/main", "https://raw.githubusercontent.com/ypxa/w/master"]; + fetchData(req, res, next, baseUrls); + }); } const fetchData = async (req, res, next, baseUrls) => { try { - const reqTarget = baseUrls.map((baseUrl) => `${baseUrl}/${req.params[0]}`) - let data - let asset + const reqTarget = baseUrls.map((baseUrl) => `${baseUrl}/${req.params[0]}`); + let data; + let asset; for (const target of reqTarget) { - asset = await fetch(target) + asset = await fetch(target); if (asset.ok) { - data = await asset.arrayBuffer() - break + data = await asset.arrayBuffer(); + break; } } if (data) { - res.end(Buffer.from(data)) + res.end(Buffer.from(data)); } else { - res.status(404).send() + res.status(404).send(); } } catch (error) { - console.error(`Error fetching ${req.url}:`, error) - res.status(500).send() + console.error(`Error fetching ${req.url}:`, error); + res.status(500).send(); } -} +}; -app.get('*', (req, res) => { +app.get("*", (req, res) => { res.status(404).send(); }); @@ -92,26 +88,26 @@ app.use((err, req, res, next) => { res.status(500).send(); }); -server.on('request', (req, res) => { +server.on("request", (req, res) => { if (bareServer.shouldRoute(req)) { - bareServer.routeRequest(req, res) + bareServer.routeRequest(req, res); } else { - app(req, res) + app(req, res); } -}) +}); -server.on('upgrade', (req, socket, head) => { +server.on("upgrade", (req, socket, head) => { if (bareServer.shouldRoute(req)) { - bareServer.routeUpgrade(req, socket, head) + bareServer.routeUpgrade(req, socket, head); } else { - socket.end() + socket.end(); } -}) +}); -server.on('listening', () => { - console.log(`Running at http://localhost:${PORT}`) -}) +server.on("listening", () => { + console.log(`Running at http://localhost:${PORT}`); +}); server.listen({ port: PORT, -}) +}); \ No newline at end of file diff --git a/package.json b/package.json index 22ef66209..c8ab737af 100644 --- a/package.json +++ b/package.json @@ -22,4 +22,4 @@ "devDependencies": { "prettier": "3.2.5" } -} +} \ No newline at end of file diff --git a/static/apps.html b/static/apps.html index ed599a4a4..8ede5e4c8 100644 --- a/static/apps.html +++ b/static/apps.html @@ -1,4 +1,4 @@ - +
@@ -42,15 +42,15 @@ - +