Skip to content

Commit

Permalink
Optmized
Browse files Browse the repository at this point in the history
  • Loading branch information
Casual-Person authored Aug 22, 2024
1 parent a0190ea commit 74d7588
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { join } from "node:path";
import { hostname } from "node:os";
import wisp from "wisp-server-node"
import wisp from "wisp-server-node";
import compression from "compression";
import serveStatic from "serve-static";

const app = express();
// Load our publicPath first and prioritize it over UV.
app.use(express.static(publicPath));
// Load vendor files last.
// The vendor's uv.config.js won't conflict with our uv.config.js inside the publicPath directory.
app.use("/uv/", express.static(uvPath));
app.use("/epoxy/", express.static(epoxyPath));
app.use("/baremux/", express.static(baremuxPath));

// Use compression middleware
app.use(compression());

// Serve static files with serve-static
app.use(serveStatic(publicPath));
app.use("/uv/", serveStatic(uvPath));
app.use("/epoxy/", serveStatic(epoxyPath));
app.use("/baremux/", serveStatic(baremuxPath));

// Error for everything else
app.use((req, res) => {
Expand All @@ -30,11 +34,13 @@ server.on("request", (req, res) => {
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
app(req, res);
});

server.on("upgrade", (req, socket, head) => {
if (req.url.endsWith("/wisp/"))
if (req.url.endsWith("/wisp/")) {
wisp.routeRequest(req, socket, head);
else
} else {
socket.end();
}
});

let port = parseInt(process.env.PORT || "");
Expand All @@ -44,27 +50,20 @@ if (isNaN(port)) port = 8080;
server.on("listening", () => {
const address = server.address();

// by default we are listening on 0.0.0.0 (every interface)
// we just need to list a few
console.log("Listening on:");
console.log(`\thttp://localhost:${address.port}`);
console.log(`\thttp://${hostname()}:${address.port}`);
console.log(
`\thttp://${address.family === "IPv6" ? `[${address.address}]` : address.address
}:${address.port}`
`\thttp://${address.family === "IPv6" ? `[${address.address}]` : address.address}:${address.port}`
);
});

// https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);

function shutdown() {
console.log("SIGTERM signal received: closing HTTP server");
server.close();
process.exit(0);
server.close(() => {
console.log("Server closed");
process.exit(0);
});
}

server.listen({
port,
});

1 comment on commit 74d7588

@vercel
Copy link

@vercel vercel bot commented on 74d7588 Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ugfd-proxy – ./

ugfd-proxy-ugfd.vercel.app
ugfd-proxy-git-main-ugfd.vercel.app
ugfd-proxy-offical.vercel.app

Please sign in to comment.