aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/index.ts132
-rw-r--r--src/metadata.ts11
-rw-r--r--src/routes.ts123
3 files changed, 137 insertions, 129 deletions
diff --git a/src/index.ts b/src/index.ts
index 53592da..7bf2354 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,7 +1,5 @@
import dotenv from "dotenv";
import express from "express";
-import { JSDOM } from "jsdom";
-import { badgen } from "badgen";
import { MongoClient } from "mongodb";
import path from "path";
import fs from "fs";
@@ -11,7 +9,7 @@ import expressWinston from "express-winston";
dotenv.config();
-import formats, { Format } from "./formats";
+import routes from "./routes";
import Metadata from "./metadata";
import logger_config from "./util/logger";
import { config_or_error, handle_shutdown } from "./util/config";
@@ -19,8 +17,6 @@ import { config_or_error, handle_shutdown } from "./util/config";
// Start-up configuration
const BIND_ADDRESS = process.env.BIND_ADDRESS || "localhost";
const PORT = Number(process.env.PORT || 3000);
-const TOKEN = process.env.TOKEN || "";
-const UPLOAD_LIMIT = Number(process.env.UPLOAD_LIMIT || 4194304);
const logger = winston.createLogger(logger_config("ROOT"));
@@ -53,130 +49,8 @@ new MongoClient(MONGO_URI, { useUnifiedTopology: true }).connect(
})
);
- // Upload HTML file
- app.post("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
- const { org, repo, branch, commit } = req.params;
-
- const { token, format } = req.query;
- //TODO @Metadata token should come from metadata
- if (token != TOKEN) {
- return res.status(401).send("Invalid token");
- }
-
- if (!formats.list_formats().includes(format)) {
- return res.status(406).send("Report format unknown");
- }
-
- var contents = "";
- req.on("data", raw => {
- if (contents.length + raw.length > UPLOAD_LIMIT) {
- res.status(413).send("Uploaded file is too large");
- } else {
- contents += raw;
- }
- });
- req.on("end", () => {
- let formatter: Format, coverage: number;
- try {
- const doc = new JSDOM(contents).window.document;
- formatter = formats.get_format(format);
- coverage = formatter.parse_coverage(doc);
- } catch {
- return res.status(400).send("Invalid report document");
- }
-
- const badge = badgen({
- label: "coverage",
- status: Math.floor(coverage).toString() + "%",
- //TODO @Metadata stage values should come from metadata
- color: formatter.match_color(coverage, 95, 80)
- });
-
- const report_path = path.join(HOST_DIR, org, repo, branch, commit);
-
- fs.promises
- .mkdir(report_path, { recursive: true })
- .then(() =>
- fs.promises.writeFile(path.join(report_path, "badge.svg"), badge)
- )
- .then(() =>
- fs.promises.writeFile(
- path.join(report_path, "index.html"),
- contents
- )
- )
- .then(() =>
- metadata.updateBranch({ org, repo, name: branch, head: commit })
- )
- .then(result =>
- result
- ? res.status(200).send()
- : res.status(500).send("Unknown error occurred")
- );
- });
- });
-
- app.get("/v1/:org/:repo/:branch.svg", (req, res) => {
- const { org, repo, branch } = req.params;
-
- metadata.getHeadCommit(org, repo, branch).then(
- commit => {
- logger.debug(
- "Found commit %s for ORB %s/%s/%s",
- commit,
- org,
- repo,
- branch
- );
-
- res.sendFile(
- path.join(HOST_DIR, org, repo, branch, commit, "badge.svg")
- );
- },
- () => {
- res.status(500).send("Unknown error occurred");
- }
- );
- });
-
- app.get("/v1/:org/:repo/:branch.html", (req, res) => {
- const { org, repo, branch } = req.params;
-
- metadata.getHeadCommit(org, repo, branch).then(
- commit => {
- logger.debug(
- "Found commit %s for ORB %s/%s/%s",
- commit,
- org,
- repo,
- branch
- );
-
- res.sendFile(
- path.join(HOST_DIR, org, repo, branch, commit, "index.html")
- );
- },
- () => {
- res.status(500).send("Unknown error occurred");
- }
- );
- });
-
- // provide hard link for commit
- app.get("/v1/:org/:repo/:branch/:commit.svg", (req, res) => {
- const { org, repo, branch, commit } = req.params;
-
- res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "badge.svg"));
- });
-
- // provide hard link for commit
- app.get("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
- const { org, repo, branch, commit } = req.params;
-
- res.sendFile(
- path.join(HOST_DIR, org, repo, branch, commit, "index.html")
- );
- });
+ // actual app routes
+ app.use(routes(metadata));
app.use(expressWinston.errorLogger(logger_config("_ERR")));
diff --git a/src/metadata.ts b/src/metadata.ts
index 1eb7f1a..a25e8bf 100644
--- a/src/metadata.ts
+++ b/src/metadata.ts
@@ -1,4 +1,7 @@
import { Db } from "mongodb";
+import winston from "winston";
+
+import logger_config from "./util/logger";
/** //FIXME fix document schema
* Rather than using branches as the core, this should be adopted into the following document model:
@@ -18,6 +21,7 @@ export interface Branch {
name: string;
head: string;
}
+const logger = winston.createLogger(logger_config("META"));
class Metadata {
database: Db;
@@ -36,6 +40,13 @@ class Metadata {
.findOne({ org, repo, name: branch });
if (result !== null) {
+ logger.debug(
+ "Found commit %s for ORB %s/%s/%s",
+ result.head,
+ org,
+ repo,
+ branch
+ );
return result.head;
} else {
throw Error("Branch not found");
diff --git a/src/routes.ts b/src/routes.ts
new file mode 100644
index 0000000..1dbc94f
--- /dev/null
+++ b/src/routes.ts
@@ -0,0 +1,123 @@
+import express from "express";
+import { JSDOM } from "jsdom";
+import { badgen } from "badgen";
+import path from "path";
+import fs from "fs";
+
+import formats, { Format } from "./formats";
+import Metadata from "./metadata";
+import { config_or_error } from "./util/config";
+
+const TOKEN = process.env.TOKEN || "";
+const UPLOAD_LIMIT = Number(process.env.UPLOAD_LIMIT || 4194304);
+const HOST_DIR = config_or_error("HOST_DIR");
+
+export default (metadata: Metadata) => {
+ const router = express.Router();
+
+ // Upload HTML file
+ router.post("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
+ const { org, repo, branch, commit } = req.params;
+
+ const { token, format } = req.query;
+ //TODO @Metadata token should come from metadata
+ if (token != TOKEN) {
+ return res.status(401).send("Invalid token");
+ }
+
+ if (!formats.list_formats().includes(format)) {
+ return res.status(406).send("Report format unknown");
+ }
+
+ var contents = "";
+ req.on("data", raw => {
+ if (contents.length + raw.length > UPLOAD_LIMIT) {
+ res.status(413).send("Uploaded file is too large");
+ } else {
+ contents += raw;
+ }
+ });
+ req.on("end", () => {
+ let formatter: Format, coverage: number;
+ try {
+ const doc = new JSDOM(contents).window.document;
+ formatter = formats.get_format(format);
+ coverage = formatter.parse_coverage(doc);
+ } catch {
+ return res.status(400).send("Invalid report document");
+ }
+
+ const badge = badgen({
+ label: "coverage",
+ status: Math.floor(coverage).toString() + "%",
+ //TODO @Metadata stage values should come from metadata
+ color: formatter.match_color(coverage, 95, 80)
+ });
+
+ const report_path = path.join(HOST_DIR, org, repo, branch, commit);
+
+ fs.promises
+ .mkdir(report_path, { recursive: true })
+ .then(() =>
+ fs.promises.writeFile(path.join(report_path, "badge.svg"), badge)
+ )
+ .then(() =>
+ fs.promises.writeFile(path.join(report_path, "index.html"), contents)
+ )
+ .then(() =>
+ metadata.updateBranch({ org, repo, name: branch, head: commit })
+ )
+ .then(result =>
+ result
+ ? res.status(200).send()
+ : res.status(500).send("Unknown error occurred")
+ );
+ });
+ });
+
+ router.get("/v1/:org/:repo/:branch.svg", (req, res) => {
+ const { org, repo, branch } = req.params;
+
+ metadata.getHeadCommit(org, repo, branch).then(
+ commit => {
+ res.sendFile(
+ path.join(HOST_DIR, org, repo, branch, commit, "badge.svg")
+ );
+ },
+ () => {
+ res.status(500).send("Unknown error occurred");
+ }
+ );
+ });
+
+ router.get("/v1/:org/:repo/:branch.html", (req, res) => {
+ const { org, repo, branch } = req.params;
+
+ metadata.getHeadCommit(org, repo, branch).then(
+ commit => {
+ res.sendFile(
+ path.join(HOST_DIR, org, repo, branch, commit, "index.html")
+ );
+ },
+ () => {
+ res.status(500).send("Unknown error occurred");
+ }
+ );
+ });
+
+ // provide hard link for commit
+ router.get("/v1/:org/:repo/:branch/:commit.svg", (req, res) => {
+ const { org, repo, branch, commit } = req.params;
+
+ res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "badge.svg"));
+ });
+
+ // provide hard link for commit
+ router.get("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
+ const { org, repo, branch, commit } = req.params;
+
+ res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "index.html"));
+ });
+
+ return router;
+};