Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Server port; optional, defaults to '3000'
PORT=

# Config as JSON; see src/config.js for the schema
PROXYBEEZ_CONFIG=

# File path to a JSON file containing the config; only used if PROXYBEEZ_CONFIG is empty
PROXYBEEZ_CONFIG_FILE=config.json
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.env
*.txt
users/
node_modules/
lastupdate
.env
config.json
31 changes: 30 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
import * as fs from "fs";
import { typecheckConfig } from "./config.js";
import { createServer } from "./server.js";

const PORT = process.env.PORT || 3000;
const CONFIG = parseConfig(process.env.PROXYBEEZ_CONFIG);
const CONFIG = readConfig(
process.env.PROXYBEEZ_CONFIG,
process.env.PROXYBEEZ_CONFIG_FILE
);

createServer(CONFIG).listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});

/**
*
* @param {string | undefined} envConfig
* @param {string | undefined} fileConfig
*/
function readConfig(envConfig, fileConfig) {
if (envConfig) {
return parseConfig(envConfig);
}
if (!fileConfig) {
throw new Error(
`environment variable PROXYBEEZ_CONFIG and PROXYBEEZ_CONFIG_FILE are both empty; please set either one`
);
}
let fileContent;
try {
fileContent = fs.readFileSync(fileConfig).toString();
} catch (err) {
throw new Error(
`environment variable PROXYBEEZ_CONFIG is empty and PROXYBEEZ_CONFIG_FILE is '${fileConfig}' which does not point to a file that could be read: ${err.message}`
);
}
return parseConfig(fileContent);
}

/**
*
* @param {unknown} stringConfig
Expand Down