Skip to content

Commit

Permalink
The Static Exposer is now versioned too:
Browse files Browse the repository at this point in the history
- The static routes are still exposed outside the /api/vN, but the code used is dynamically imported.
- Admin can specify a certain version of active API versions to use. Default value is the highest (latest) active API version.
  • Loading branch information
jacobwod committed Apr 11, 2023
1 parent 80a047d commit 4066ce3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
7 changes: 6 additions & 1 deletion new-backend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ REQUEST_LIMIT=1000kb
SESSION_SECRET=mySecret

# Control which directories will be statically exposed on the HTTP server.
# / can contain Hajk's client app
# Because the endpoints of the Static Exposer are not versioned, we must
# decide which API version we want to use for those routes. The default value
# is "LATEST" but you can specify any of the allowed API versions. Usually,
# the default value is fine.
STATIC_EXPOSER_VERSION=LATEST
# Expose Hajk's client app directly under /
EXPOSE_CLIENT=true
# If we expose /admin, we want probably to restrict access to it. Make sure
# to enable AD_* settings below in order for this to work.
Expand Down
35 changes: 30 additions & 5 deletions new-backend/server/common/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import helmet from "helmet";
import cors from "cors";
import compression from "compression";
import cookieParser from "cookie-parser";
import * as OpenApiValidator from "express-openapi-validator";

import log4js from "./utils/hajkLogger";
import clfDate from "clf-date";

import { createProxyMiddleware } from "http-proxy-middleware";

import restrictStatic from "../apis/v1/middlewares/restrict.static";
import detailedRequestLogger from "./middlewares/detailed.request.logger";

import * as OpenApiValidator from "express-openapi-validator";
import errorHandler from "./middlewares/error.handler";

const app = new Express();
Expand Down Expand Up @@ -316,11 +314,38 @@ export default class ExpressServer {
}
}

setupStaticDirs() {
async setupStaticDirs() {
const l = log4js.getLogger("hajk.static");

l.trace("Setting up access to static directories…");
// Try to convert the value from config to an Int
let normalizedStaticExposerVersion = Number.parseInt(
process.env.STATIC_EXPOSER_VERSION?.trim?.()
);

// Grab active API versions
const apiVersions = app.set("apiVersions");

// If NaN, or if version required is not any of the active API versions,
// let's fall back to the highest active version
if (
Number.isNaN(normalizedStaticExposerVersion) ||
!apiVersions.includes(normalizedStaticExposerVersion)
) {
normalizedStaticExposerVersion = Math.max(...apiVersions);
}

const apiVersion = normalizedStaticExposerVersion;

l.info(
`Attempting to expose static directories using Static Exposer from API V${apiVersion}`
);

try {
// Dynamically import the required version of Static Restrictor
const { default: restrictStatic } = await import(
`../apis/v${apiVersion}/middlewares/restrict.static`
);

const dir = path.join(process.cwd(), "static");
// List dir contents, the second parameter will ensure we get Dirent objects
const staticDirs = fs
Expand Down

0 comments on commit 4066ce3

Please sign in to comment.