Skip to content

Commit

Permalink
Modules foundations (#669)
Browse files Browse the repository at this point in the history
* init

* Remove AUTH module

* Reorder npm build scripts a little

* AppSettings are always loaded from the same file as ServerSettings

* We no longer support `dataCubeOfInterest` option

* Split settings getter to simple getter and one with settings refresh. Only root endpoint refreshes settings (if specific option is set in config)

* Move "middlewares" inside app.ts

* Better warning for settings load timeout
  • Loading branch information
adrianmroz authored Nov 10, 2020
1 parent f4365e2 commit de8212f
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 344 deletions.
54 changes: 27 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@
"test-watcher": "mocha --require ./test/setup/mocha.js --watch --watch-extensions ts,tsx",
"start": "./bin/turnilo",
"start:dev": "NODE_ENV=dev-hmr ./bin/turnilo",
"build": "npm run clean && npm run tsc -- -p src/server/tsconfig.json && webpack --config config/webpack.prod.js",
"build:dev": "npm run clean && npm run tsc -- -p src/server/tsconfig.json",
"dev": "npm run clean && npm run tsc -- -p tsconfig.json -w --pretty",
"start:examples": "npm run start -- --examples",
"start:dev:examples": "npm run start:dev -- --examples",
"build:client": "webpack --config config/webpack.prod.js",
"build:server": "npm run tsc -- -p src/server/tsconfig.json ",
"build": "npm-run-all -s clean build:server build:client",
"clean": "rimraf build/*",
"tsc": "tsc",
"lint": "npm-run-all -p lint:*",
"lint:ts": "tslint -p . --format verbose",
"lint:sass": "sass-lint -v",
"start:examples": "npm run start -- --examples",
"start:dev:examples": "npm run start:dev -- --examples",
"e2e": "start-server-and-test start:examples http://localhost:9090/health/ready 'cypress run'",
"e2e:dev": "cypress open",
"check": "npm-run-all -c -p lint test build e2e"
Expand Down
36 changes: 17 additions & 19 deletions src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Handler, Request, Response, Router } from "express";
import { hsts } from "helmet";
import * as path from "path";
import { LOGGER } from "../common/logger/logger";
import { AUTH, SERVER_SETTINGS, SETTINGS_MANAGER, VERSION } from "./config";
import { SERVER_SETTINGS, SETTINGS_MANAGER, VERSION } from "./config";
import { livenessRouter } from "./routes/liveness/liveness";
import { mkurlRouter } from "./routes/mkurl/mkurl";
import { plyqlRouter } from "./routes/plyql/plyql";
Expand Down Expand Up @@ -65,6 +65,20 @@ if (SERVER_SETTINGS.getStrictTransportSecurity() === "always") {
}));
}

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

if (SERVER_SETTINGS.getIframe() === "deny") {
app.use((req: Request, res: Response, next: Function) => {
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("Content-Security-Policy", "frame-ancestors 'none'");
next();
});
}

// TODO: plugins go here

// TODO: after plugins
// development HMR
if (app.get("env") === "dev-hmr") {
// add hot module replacement
Expand Down Expand Up @@ -93,16 +107,8 @@ if (app.get("env") === "dev-hmr") {
attachRouter("/", express.static(path.join(__dirname, "../../build/public")));
attachRouter("/", express.static(path.join(__dirname, "../../assets")));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const settingsGetter: SettingsGetter = opts => SETTINGS_MANAGER.getSettings(opts);

// Auth
if (AUTH) {
app.use(AUTH);
}

attachRouter(SERVER_SETTINGS.getReadinessEndpoint(), readinessRouter(settingsGetter));
attachRouter(SERVER_SETTINGS.getLivenessEndpoint(), livenessRouter);

Expand All @@ -112,16 +118,8 @@ attachRouter("/plyql", plyqlRouter(settingsGetter));
attachRouter("/mkurl", mkurlRouter(settingsGetter));
attachRouter("/shorten", shortenRouter(settingsGetter, isTrustedProxy));

// View routes
if (SERVER_SETTINGS.getIframe() === "deny") {
app.use((req: Request, res: Response, next: Function) => {
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("Content-Security-Policy", "frame-ancestors 'none'");
next();
});
}

attachRouter("/", turniloRouter(settingsGetter, VERSION));
const freshSettingsGetter: SettingsGetter = opts => SETTINGS_MANAGER.getFreshSettings(opts);
attachRouter("/", turniloRouter(freshSettingsGetter, VERSION));

// Catch 404 and redirect to /
app.use((req: Request, res: Response) => {
Expand Down
Loading

0 comments on commit de8212f

Please sign in to comment.