Allow running servers on multiple ports

(one server per port)
This commit is contained in:
Taevas 2025-03-10 17:11:09 +01:00
parent ca6d1785f5
commit 0c47ad9fcc
2 changed files with 24 additions and 12 deletions

View file

@ -9,14 +9,20 @@ bun i
bun dev
```
## Ports
This website is configured to run on port `8000` when in development mode, while it will otherwise run on `80` (AND `443` if a certificate and the such can be found) in production mode.
## Environment variables
This website uses `SSL_CERT` and `SSL_KEY` (no dotenv support) to determine the path of the required files to establish secured connections through HTTPS, and alternatively looks for files called `cert.pem` and `key.pem` if those environment variables do not exist. If both files are found, they will be used for all the ports that this website uses except for port `80`, and in the event that they are not found, they will simply not be used and, in production mode, no server on port `443` will be run.
This website uses [`@carbon/icons-react`](https://github.com/carbon-design-system/carbon/tree/main/packages/icons-react), which **installs [a telemetry package which can be disabled](https://github.com/ibm-telemetry/telemetry-js/tree/main#opting-out-of-ibm-telemetry-data-collection):**
Set the environment variable IBM_TELEMETRY_DISABLED to true
This website makes use of several online APIs in order to deliver the `Infos` that are available on the right side of the main page, accessing most of these APIs requires a key (or similar), which can be set through the following environment variables:
This website makes use of several online APIs in order to deliver the `Infos` that are available on the right side of the main page. Accessing most of these APIs requires a key (or similar), which can be set through the following environment variables (with dotenv support for development):
- `API_GITHUB`
- `API_GITLAB`

View file

@ -1,3 +1,4 @@
import type { Server } from "bun";
import { parseArgs } from "util";
import { coding_github } from "./api/coding_github";
import { coding_gitlab } from "./api/coding_gitlab";
@ -14,17 +15,20 @@ import { website_umami } from "./api/website_umami";
// PORT AND SSL STUFF
const certificate = Bun.file("./cert.pem");
const key = Bun.file("./key.pem");
const ssl_available = await certificate.exists() && await key.exists();
console.log("Are we able to use SSL?", ssl_available);
const { values } = parseArgs({args: Bun.argv, allowPositionals: true, options: {dev: {type: "boolean"}}});
const dev = values.dev ?? false;
console.log("Are we in development mode?", dev);
const port = dev ? 8000 : ssl_available ? 443 : 80;
const tls = ssl_available ? {certificate, key} : undefined;
const cert = Bun.file(process.env["SSL_CERT"] ?? "./cert.pem");
const key = Bun.file(process.env["SSL_KEY"] ?? "./key.pem");
const ssl_available = await cert.exists() && await key.exists();
console.log("Are we able to use SSL?", ssl_available);
// const port = dev ? 8000 : ssl_available ? 443 : 80;
const tls = ssl_available ? {cert, key} : undefined;
const ports: number[] = [dev ? 8000 : 80];
if (!dev && tls) ports.push(443);
console.log("Therefore, we are opening ports on:", ports);
// ACTUAL CODE
@ -55,10 +59,10 @@ const builds = await Bun.build({
},
});
const server = Bun.serve({
const servers: Server[] = ports.map((port) => Bun.serve({
idleTimeout: 30,
// @ts-expect-error https://github.com/oven-sh/bun/issues/17772
tls,
tls: port !== 80 ? tls : undefined,
port,
fetch: async (req) => {
const url = new URL(req.url);
@ -119,6 +123,8 @@ const server = Bun.serve({
return new Response("Not Found", {status: 404});
},
});
}));
servers.forEach((server) => console.log(`Listening on ${server.hostname}:${server.port}`));
console.log("\n\n--------\n\n");
console.log(`Listening on ${server.hostname}:${server.port}\n\n--------\n\n`);