aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/config.test.ts5
-rw-r--r--src/config.ts65
-rw-r--r--src/index.ts10
-rw-r--r--src/metadata.ts1
-rw-r--r--src/routes.test.ts44
-rw-r--r--src/routes.ts6
6 files changed, 83 insertions, 48 deletions
diff --git a/src/config.test.ts b/src/config.test.ts
index c7a8be4..b7c869f 100644
--- a/src/config.test.ts
+++ b/src/config.test.ts
@@ -290,10 +290,9 @@ describe("handleStartup", () => {
const config = {
hostDir: "/apple",
publicDir: "/public",
- targetUrl: "localhost"
+ targetUrl: "localhost",
} as EnvConfig;
- const confStartup = (): Promise<MongoClient> =>
- handleStartup(config, logger);
+ const confStartup = (): Promise<MongoClient> => handleStartup(config, logger);
it("should pass back MongoClient", async () => {
const superClient = {} as MongoClient;
diff --git a/src/config.ts b/src/config.ts
index e836552..1908c10 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -29,7 +29,10 @@ export const initializeToken = (logger: winston.Logger): string => {
/**
* Get environment variable or exit application if it doesn't exist
*/
-export const configOrError = (varName: string, logger: winston.Logger): string => {
+export const configOrError = (
+ varName: string,
+ logger: winston.Logger
+): string => {
const value = process.env[varName];
if (value !== undefined) {
return value;
@@ -42,7 +45,10 @@ export const configOrError = (varName: string, logger: winston.Logger): string =
/**
* Process a template and persist based on template.
*/
-export const persistTemplate = async (input: Template, logger: winston.Logger): Promise<void> => {
+export const persistTemplate = async (
+ input: Template,
+ logger: winston.Logger
+): Promise<void> => {
try {
const template = await processTemplate(input);
logger.debug("Generated '%s' from template file", template.outputFile);
@@ -70,13 +76,16 @@ export const persistTemplate = async (input: Template, logger: winston.Logger):
/**
* Handle server start-up functions:
- *
+ *
* - Open database connection
* - Generate documents from templates based on configuration
- *
+ *
* If one of these actions cannot be performed, it will call `process.exit(1)`.
*/
-export const handleStartup = async (config: EnvConfig, logger: winston.Logger): Promise<MongoClient> => {
+export const handleStartup = async (
+ config: EnvConfig,
+ logger: winston.Logger
+): Promise<MongoClient> => {
try {
const { hostDir, publicDir, dbUri, targetUrl } = config;
await fs.promises.access(hostDir, fs.constants.R_OK | fs.constants.W_OK);
@@ -88,21 +97,27 @@ export const handleStartup = async (config: EnvConfig, logger: winston.Logger):
Promise.reject(err.message ?? "Unable to connect to database")
);
- await persistTemplate({
- inputFile: path.join(publicDir, "templates", "sh.tmpl"),
- outputFile: path.join(hostDir, "sh"),
- context: { TARGET_URL: targetUrl },
- } as Template, logger);
- await persistTemplate({
- inputFile: path.join(publicDir, "templates", "index.html.tmpl"),
- outputFile: path.join(hostDir, "index.html"),
- context: {
- TARGET_URL: targetUrl,
- CURL_HTTPS: targetUrl.includes("https")
- ? "--proto '=https' --tlsv1.2 "
- : "",
- },
- } as Template, logger);
+ await persistTemplate(
+ {
+ inputFile: path.join(publicDir, "templates", "sh.tmpl"),
+ outputFile: path.join(hostDir, "sh"),
+ context: { TARGET_URL: targetUrl },
+ } as Template,
+ logger
+ );
+ await persistTemplate(
+ {
+ inputFile: path.join(publicDir, "templates", "index.html.tmpl"),
+ outputFile: path.join(hostDir, "index.html"),
+ context: {
+ TARGET_URL: targetUrl,
+ CURL_HTTPS: targetUrl.includes("https")
+ ? "--proto '=https' --tlsv1.2 "
+ : "",
+ },
+ } as Template,
+ logger
+ );
return mongo;
} catch (err) {
@@ -125,10 +140,12 @@ export const handleShutdown =
logger.info("MongoDB client connection closed.");
// must await for callback - wrapped in Promise
- await new Promise((res, rej) => server.close((err) => {
- logger.info("Express down.");
- (err ? rej : res)(err);
- }));
+ await new Promise((res, rej) =>
+ server.close((err) => {
+ logger.info("Express down.");
+ (err ? rej : res)(err);
+ })
+ );
process.exit(0);
} catch (e) {
diff --git a/src/index.ts b/src/index.ts
index d101ba3..ae15509 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -60,10 +60,16 @@ handleStartup(ENV_CONFIG, logger).then((mongo) => {
// actual app routes
app.use(routes(metadata));
- app.use(expressWinston.errorLogger(loggerConfig("_ERR", ENV_CONFIG.logLevel)));
+ app.use(
+ expressWinston.errorLogger(loggerConfig("_ERR", ENV_CONFIG.logLevel))
+ );
const server = app.listen(ENV_CONFIG.port, ENV_CONFIG.bindAddress, () => {
- logger.info("Express has started: http://%s:%d/", ENV_CONFIG.bindAddress, ENV_CONFIG.port);
+ logger.info(
+ "Express has started: http://%s:%d/",
+ ENV_CONFIG.bindAddress,
+ ENV_CONFIG.port
+ );
});
// application exit handling
diff --git a/src/metadata.ts b/src/metadata.ts
index 6f6d401..184580f 100644
--- a/src/metadata.ts
+++ b/src/metadata.ts
@@ -193,7 +193,6 @@ class Metadata {
stage2: this.config.stage2,
};
}
-
}
export default Metadata;
diff --git a/src/routes.test.ts b/src/routes.test.ts
index 5d7839c..2633308 100644
--- a/src/routes.test.ts
+++ b/src/routes.test.ts
@@ -124,11 +124,20 @@ const TARGET_URL = "https://localhost:3000";
describe("templates", () => {
describe("GET /sh", () => {
it("should return the sh file containing the curl command", async () => {
- await persistTemplate({
- inputFile: path.join(__dirname, "..", "public", "templates", "sh.tmpl"),
- outputFile: path.join(HOST_DIR, "sh"),
- context: { TARGET_URL },
- } as Template, logger);
+ await persistTemplate(
+ {
+ inputFile: path.join(
+ __dirname,
+ "..",
+ "public",
+ "templates",
+ "sh.tmpl"
+ ),
+ outputFile: path.join(HOST_DIR, "sh"),
+ context: { TARGET_URL },
+ } as Template,
+ logger
+ );
const res = await (await request()).get("/sh").expect(200);
expect(exit).not.toHaveBeenCalled();
@@ -139,17 +148,20 @@ describe("templates", () => {
describe("GET /", () => {
it("should return the index HTML file containing the bash command", async () => {
- await persistTemplate({
- inputFile: path.join(
- __dirname,
- "..",
- "public",
- "templates",
- "index.html.tmpl"
- ),
- outputFile: path.join(HOST_DIR, "index.html"),
- context: { TARGET_URL, CURL_HTTPS: "--https " },
- } as Template, logger);
+ await persistTemplate(
+ {
+ inputFile: path.join(
+ __dirname,
+ "..",
+ "public",
+ "templates",
+ "index.html.tmpl"
+ ),
+ outputFile: path.join(HOST_DIR, "index.html"),
+ context: { TARGET_URL, CURL_HTTPS: "--https " },
+ } as Template,
+ logger
+ );
const res = await (await request())
.get("/")
diff --git a/src/routes.ts b/src/routes.ts
index 0998345..1c8bd07 100644
--- a/src/routes.ts
+++ b/src/routes.ts
@@ -9,12 +9,14 @@ import Metadata, { HeadIdentity, isError } from "./metadata";
import loggerConfig from "./util/logger";
import { InvalidReportDocumentError, Messages } from "./errors";
-/**
+/**
* Provide routes from application state
*/
const routes = (metadata: Metadata): Router => {
const router = Router();
- const logger = winston.createLogger(loggerConfig("HTTP", metadata.logger.level));
+ const logger = winston.createLogger(
+ loggerConfig("HTTP", metadata.logger.level)
+ );
/**
* Persist uploaded coverage report and creates coverage badge