matrix-dimension/src-ts/api/Webserver.ts
Travis Ralston 599fb80112 Add the start of an admin API and re-add widgets
The frontend is still broken and doesn't use these endpoints at all. A migration tool still needs to be written to pull in existing widget configurations.
2017-12-18 21:44:01 -07:00

76 lines
2.9 KiB
TypeScript

import * as express from "express";
import * as path from "path";
import * as bodyParser from "body-parser";
import * as URL from "url";
import { LogService } from "matrix-js-snippets";
import { Server } from "typescript-rest";
import * as _ from "lodash";
import config from "../config";
import { ApiError } from "./ApiError";
export default class Webserver {
private app: express.Application;
constructor() {
this.app = express();
this.configure();
this.loadRoutes();
}
private loadRoutes() {
const apis = ["scalar", "dimension"].map(a => path.join(__dirname, a, "*.js"));
const router = express.Router();
apis.forEach(a => Server.loadServices(router, [a]));
const routes = _.uniq(router.stack.map(r => r.route.path));
for (const route of routes) {
this.app.options(route, (_req, res) => res.sendStatus(200));
LogService.info("Webserver", "Registered route: " + route);
}
this.app.use(router);
// We register the default route last to make sure we don't override anything by accident.
// We'll pass off all other requests to the web app
this.app.get("*", (_req, res) => {
res.sendFile(path.join(__dirname, "..", "..", "web", "index.html"));
});
// Set up the error handler
this.app.use((err: any, _req, res, next) => {
if (err instanceof ApiError) {
// Don't do anything for 'connection reset'
if (res.headersSent) return next(err);
LogService.warn("Webserver", "Handling ApiError " + err.statusCode + " " + JSON.stringify(err.jsonResponse));
res.setHeader("Content-Type", "application/json");
res.status(err.statusCode);
res.json(err.jsonResponse);
} else next(err);
});
}
private configure() {
this.app.use(express.static(path.join(__dirname, "..", "..", "web")));
this.app.use(bodyParser.json());
this.app.use((req, _res, next) => {
const parsedUrl = URL.parse(req.url, true);
if (parsedUrl.query && parsedUrl.query["scalar_token"]) {
parsedUrl.query["scalar_token"] = "redacted";
parsedUrl.search = undefined; // to force URL.format to use `query`
}
LogService.verbose("Webserver", "Incoming request: " + req.method + " " + URL.format(parsedUrl));
next();
});
this.app.use((_req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
}
start() {
this.app.listen(config.web.port, config.web.address);
LogService.info("Webserver", "API and UI listening on " + config.web.address + ":" + config.web.port);
}
}