diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2020-01-11 21:41:41 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2020-01-11 21:41:41 -0500 |
| commit | c7ebf8009e27256db7eb36fa259c250bd80dbf09 (patch) | |
| tree | 93e111f5e4c1084de9f9105a7b5a06b5dc2464eb /src/routes.ts | |
| parent | edfdc5cfcfa9b7df9f5c7b5ff53f432b0579b433 (diff) | |
| download | ao-coverage-c7ebf8009e27256db7eb36fa259c250bd80dbf09.tar.gz ao-coverage-c7ebf8009e27256db7eb36fa259c250bd80dbf09.tar.bz2 ao-coverage-c7ebf8009e27256db7eb36fa259c250bd80dbf09.zip | |
#8 Add router unit tests using supertest
Also moved the template processing from index to router.
Diffstat (limited to 'src/routes.ts')
| -rw-r--r-- | src/routes.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/routes.ts b/src/routes.ts index 9ce3bbc..92d3542 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -5,6 +5,7 @@ import winston from "winston"; import path from "path"; import fs from "fs"; +import processTemplate, { Template } from "./templates"; import formats, { GradientStyle } from "./formats"; import Metadata, { HeadIdentity } from "./metadata"; import { configOrError } from "./util/config"; @@ -14,12 +15,45 @@ import { Messages } from "./errors"; const TOKEN = process.env.TOKEN ?? ""; const UPLOAD_LIMIT = Number(process.env.UPLOAD_LIMIT ?? 4194304); const HOST_DIR = configOrError("HOST_DIR"); +const TARGET_URL = process.env.TARGET_URL ?? "http://localhost:3000"; const logger = winston.createLogger(loggerConfig("HTTP")); export default (metadata: Metadata): Router => { const router = Router(); + const bashTemplate = { + inputFile: path.join(__dirname, "..", "public", "templates", "bash.template"), + outputFile: path.join(HOST_DIR, "bash"), + context: { TARGET_URL } + } as Template; + const indexTemplate = { + inputFile: path.join(__dirname, "..", "public", "templates", "index.html.template"), + outputFile: path.join(HOST_DIR, "index.html"), + context: { TARGET_URL } + } as Template; + + processTemplate(bashTemplate) + .then(template => { + logger.debug("Generated '%s' from template file", template.outputFile); + }) + .then(() => processTemplate(indexTemplate)) + .then(template => { + logger.debug("Generated '%s' from template file", template.outputFile); + }) + .catch(err => { + logger.error("Unable to process template file: %s", err); + + // if the output file exists, then we are fine with continuing without + return fs.promises.access(bashTemplate.outputFile, fs.constants.R_OK); + }) + .then(() => fs.promises.access(indexTemplate.outputFile, fs.constants.R_OK)) + .catch(err => { + logger.error("Cannot proceed: %s", err); + + process.exit(1); + }); + // serve landing page router.get("/", (_, res) => { res.sendFile(path.join(HOST_DIR, "index.html")) |
