Browse Source

Handle promise errors in handleStartup

trunk
Kevin Hoerr 1 year ago
parent
commit
8b70567b94
Signed by: kjhoerr GPG Key ID: 78E4BD33ACC22C86
1 changed files with 42 additions and 35 deletions
  1. +42
    -35
      src/util/config.ts

+ 42
- 35
src/util/config.ts View File

@ -37,7 +37,7 @@ export const persistTemplate = async (input: Template): Promise<void> => {
process.exit(1);
}
// if the output file exists, then we are fine with continuing without
logger.warning(
logger.warn(
"Could not generate '%s' from template file, but file already exists: %s",
input.outputFile,
err1
@ -50,43 +50,50 @@ const TARGET_URL = process.env.TARGET_URL ?? "http://localhost:3000";
const HOST_DIR = configOrError("HOST_DIR");
export const handleStartup = async (): Promise<MongoClient> => {
await fs.promises.access(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK);
if (!path.isAbsolute(HOST_DIR)) {
logger.error("HOST_DIR must be an absolute path");
process.exit(1);
}
const mongo = await new MongoClient(MONGO_URI, { useUnifiedTopology: true })
.connect()
.catch((err: MongoError) => {
logger.error(err.message ?? "Unable to connect to database");
try {
await fs.promises.access(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK);
if (!path.isAbsolute(HOST_DIR)) {
logger.error("HOST_DIR must be an absolute path");
process.exit(1);
});
}
const mongo = await new MongoClient(MONGO_URI, { useUnifiedTopology: true })
.connect()
.catch((err: MongoError) => {
logger.error(err.message ?? "Unable to connect to database");
process.exit(1);
});
await persistTemplate({
inputFile: path.join(
__dirname,
"..",
"public",
"templates",
"bash.template"
),
outputFile: path.join(HOST_DIR, "bash"),
context: { TARGET_URL }
} as Template);
await persistTemplate({
inputFile: path.join(
__dirname,
"..",
"public",
"templates",
"index.html.template"
),
outputFile: path.join(HOST_DIR, "index.html"),
context: { TARGET_URL }
} as Template);
await persistTemplate({
inputFile: path.join(
__dirname,
"..",
"..",
"public",
"templates",
"bash.template"
),
outputFile: path.join(HOST_DIR, "bash"),
context: { TARGET_URL }
} as Template);
await persistTemplate({
inputFile: path.join(
__dirname,
"..",
"..",
"public",
"templates",
"index.html.template"
),
outputFile: path.join(HOST_DIR, "index.html"),
context: { TARGET_URL }
} as Template);
return mongo;
return mongo;
} catch (err) {
logger.error("Error occurred during startup: %s", err);
process.exit(1);
}
};
export const handleShutdown = (mongo: MongoClient, server: Server) => (


Loading…
Cancel
Save