Skip to content

Commit 1d3be98

Browse files
committed
Initial Commit
0 parents  commit 1d3be98

24 files changed

+43808
-0
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PORT=8080

.replit

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
language = "nodejs"
2+
run = "npm run start"
3+
4+
[env]
5+
XDG_CONFIG_HOME = "/home/runner/.config"
6+
PATH = "/home/runner/$REPL_SLUG/.config/npm/node_global/bin:/home/runner/$REPL_SLUG/node_modules/.bin"
7+
npm_config_prefix = "/home/runner/$REPL_SLUG/.config/npm/node_global"
8+
9+
[nix]
10+
channel = "stable-22_05"
11+
12+
[packager]
13+
language = "nodejs"
14+
15+
[packager.features]
16+
packageSearch = true
17+
guessImports = true
18+
enabledForHosting = false
19+
20+
[debugger]
21+
support = true
22+
23+
[debugger.interactive]
24+
transport = "localhost:0"
25+
startCommand = ["dap-node"]
26+
27+
[debugger.interactive.initializeMessage]
28+
command = "initialize"
29+
type = "request"
30+
31+
[debugger.interactive.initializeMessage.arguments]
32+
clientID = "replit"
33+
clientName = "replit.com"
34+
columnsStartAt1 = true
35+
linesStartAt1 = true
36+
locale = "en-us"
37+
pathFormat = "path"
38+
supportsInvalidatedEvent = true
39+
supportsProgressReporting = true
40+
supportsRunInTerminalRequest = true
41+
supportsVariablePaging = true
42+
supportsVariableType = true
43+
44+
[debugger.interactive.launchMessage]
45+
command = "launch"
46+
type = "request"
47+
48+
[debugger.interactive.launchMessage.arguments]
49+
console = "externalTerminal"
50+
cwd = "."
51+
pauseForSourceMap = false
52+
program = "./index.js"
53+
request = "launch"
54+
sourceMaps = true
55+
stopOnEntry = false
56+
type = "pwa-node"
57+
58+
[unitTest]
59+
language = "nodejs"

Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM node:19-bullseye
4+
ENV NODE_ENV=production
5+
6+
WORKDIR /app
7+
8+
COPY ["package.json", "package-lock.json*", "./"]
9+
10+
RUN npm install
11+
12+
COPY . .
13+
14+
CMD [ "node", "index.js" ]

app.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "P45",
3+
"description": "P45",
4+
"repository": "https://github.com/cybertitus/tn",
5+
"logo": "",
6+
"keywords": ["P45", "proxy", "www"]
7+
}

index.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import cluster from "cluster";
2+
import os from "os";
3+
import express from "express";
4+
import http from "node:http";
5+
import createBareServer from "@tomphttp/bare-server-node";
6+
import path from "node:path";
7+
import * as dotenv from "dotenv";
8+
dotenv.config();
9+
10+
const __dirname = process.cwd();
11+
12+
// Increase max sockets to 1000
13+
http.globalAgent.maxSockets = 1000;
14+
15+
if (cluster.isMaster) {
16+
const numCPUs = os.cpus().length;
17+
console.log(`Master process running with PID ${process.pid}`);
18+
19+
// Fork worker processes
20+
for (let i = 0; i < numCPUs; i++) {
21+
cluster.fork();
22+
}
23+
24+
// Handle worker exit events
25+
cluster.on("exit", (worker, code, signal) => {
26+
console.log(
27+
`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`
28+
);
29+
console.log("Forking a new worker...");
30+
cluster.fork();
31+
});
32+
} else {
33+
const server = http.createServer();
34+
const app = express(server);
35+
const bareServer = createBareServer("/bare/");
36+
37+
// Serve static files with caching
38+
const staticOptions = {
39+
maxAge: "1d",
40+
setHeaders: (res, path) => {
41+
if (path.endsWith(".html")) {
42+
res.setHeader("Cache-Control", "no-cache");
43+
} else {
44+
res.setHeader("Cache-Control", "public, max-age=86400");
45+
}
46+
},
47+
};
48+
app.use(express.static(path.join(__dirname, "static"), staticOptions));
49+
50+
app.use(express.json());
51+
app.use(
52+
express.urlencoded({
53+
extended: true,
54+
})
55+
);
56+
57+
// Define routes
58+
const routes = [
59+
{ path: "/", file: "web.html" },
60+
// { path: "/web", file: "web.html" },
61+
// { path: "/apps", file: "apps.html" },
62+
{ path: "/go", file: "go.html" },
63+
// { path: "/settings", file: "settings.html" },
64+
{ path: "/404", file: "404.html" },
65+
];
66+
67+
// Define routes using the routes array
68+
routes.forEach((route) => {
69+
app.get(route.path, (req, res) => {
70+
res.sendFile(path.join(__dirname, "static", route.file));
71+
});
72+
});
73+
74+
// Catch-all route
75+
app.get("/*", (req, res) => {
76+
res.redirect("/404");
77+
});
78+
79+
// Bare Server
80+
server.on("request", (req, res) => {
81+
if (bareServer.shouldRoute(req)) {
82+
bareServer.routeRequest(req, res);
83+
} else {
84+
app(req, res);
85+
}
86+
});
87+
88+
server.on("upgrade", (req, socket, head) => {
89+
if (bareServer.shouldRoute(req)) {
90+
bareServer.routeUpgrade(req, socket, head);
91+
} else {
92+
socket.end();
93+
}
94+
});
95+
96+
server.listen({
97+
port: process.env.PORT,
98+
});
99+
100+
console.log(`Worker process running with PID ${process.pid}`);
101+
}

0 commit comments

Comments
 (0)