Skip to content

Commit

Permalink
Merge pull request #100 from bcgov/sec/cache
Browse files Browse the repository at this point in the history
Remove Cache Report and v1 Endpoints
  • Loading branch information
TimCsaky authored Nov 21, 2023
2 parents f23b82b + a435196 commit 14b74d0
Show file tree
Hide file tree
Showing 29 changed files with 101 additions and 786 deletions.
3 changes: 3 additions & 0 deletions .github/environments/values.dev.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
features:
authentication: true

config:
enabled: true
configMap:
Expand Down
3 changes: 3 additions & 0 deletions .github/environments/values.pr.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
features:
authentication: true

persistentVolumeClaim:
enabled: false

Expand Down
3 changes: 3 additions & 0 deletions .github/environments/values.prod.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
features:
authentication: true

config:
enabled: true
configMap:
Expand Down
3 changes: 3 additions & 0 deletions .github/environments/values.test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
features:
authentication: true

config:
enabled: true
configMap:
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ RUN mkdir -p /.npm
RUN chown -R 1001:0 /.npm

# Install Application
COPY .git ${APP_ROOT}/.git
COPY app ${APP_ROOT}
RUN chown -R 1001:0 ${APP_ROOT}
USER 1001
Expand Down
42 changes: 21 additions & 21 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ const cors = require('cors');
const express = require('express');
const Problem = require('api-problem');

const { name: appName, version: appVersion } = require('./package.json');
const carboneCopyApi = require('./src/components/carboneCopyApi');
const keycloak = require('./src/components/keycloak');
const log = require('./src/components/log')(module.filename);
const httpLogger = require('./src/components/log').httpLogger;
const utils = require('./src/components/utils');
const v1Router = require('./src/routes/v1');
const { getGitRevision, prettyStringify } = require('./src/components/utils');
const v2Router = require('./src/routes/v2');

const { authorizedParty } = require('./src/middleware/authorizedParty');

// API statistics disabled
// const initializeApiTracker = require('./src/middleware/apiTracker');

const apiRouter = express.Router();
const state = {
gitRev: getGitRevision(),
ready: false,
shutdown: false
};

const app = express();
app.use(compression());
app.use(cors());
app.use(cors({
/** Tells browsers to cache preflight requests for Access-Control-Max-Age seconds */
maxAge: 600,
/** Set true to dynamically set Access-Control-Allow-Origin based on Origin */
origin: true
}));
app.use(express.json({
limit: config.get('server.bodyLimit')
}));
Expand All @@ -34,7 +37,7 @@ app.use(express.urlencoded({
}));

// Print out configuration settings in verbose startup
log.verbose('Config', utils.prettyStringify(config));
log.verbose('Config', prettyStringify(config));

// Skip if running tests
if (process.env.NODE_ENV !== 'test') {
Expand Down Expand Up @@ -69,23 +72,20 @@ app.use((_req, res, next) => {
}
});

// CDOGS Base API Directory
// Base API Directory
apiRouter.get('/', (_req, res) => {
res.status(200).json({
endpoints: [
'/api/v1',
'/api/v2'
],
versions: [
1,
2
]
app: {
gitRev: state.gitRev,
name: appName,
nodeVersion: process.version,
version: appVersion
},
endpoints: ['/api/v2'],
versions: [2]
});
});

// v1 Router
apiRouter.use('/v1', v1Router);

// v2 Router
apiRouter.use('/v2', v2Router);

Expand Down Expand Up @@ -146,8 +146,8 @@ function shutdown() {
function cleanup() {
log.info('Service no longer accepting traffic');
state.shutdown = true;
// Wait 10 seconds max before hard exiting
setTimeout(() => process.exit(), 10000);
// Wait 5 seconds max before hard exiting
setTimeout(() => process.exit(), 5000);
}

module.exports = app;
4 changes: 0 additions & 4 deletions app/config/default.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"keycloak": {
"realm": "jbd6rnxw",
"serverUrl": "https://dev.oidc.gov.bc.ca/auth"
},
"server": {
"bodyLimit": "100mb",
"logLevel": "http",
Expand Down
22 changes: 12 additions & 10 deletions app/src/components/carboneCopyApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const telejson = require('telejson');
const log = require('./log')(module.filename);
const carboneRender = require('./carboneRender');
const FileCache = require('./fileCache');
const { truthy } = require('./utils');

const fileCache = new FileCache();

Expand All @@ -24,7 +23,16 @@ const carboneCopyApi = {
}
},

getFromCache: async (hash, hashHeaderName, download, remove, res) => {
/**
* @function getFromCache
* Attempts to fetch a specific file based off the sha-256 `hash` provided
* @param {string} hash A sha-256 hash
* @param {string} hashHeaderName The request header name for the hash
* @param {boolean} download Determines whether to provide the file as a payload
* @param {boolean} remove Determines whether to delete the file after the operation
* @param {object} res Express response object
*/
getFromCache: (hash, hashHeaderName, download, remove, res) => {
const file = fileCache.find(hash);
if (!file.success) {
return new Problem(file.errorType, { detail: file.errorMsg }).send(res);
Expand All @@ -33,14 +41,14 @@ const carboneCopyApi = {
let cached = undefined;
if (download) {
try {
cached = await fileCache.read(hash);
cached = fileCache.read(hash);
} catch (e) {
return new Problem(500, { detail: e.message }).send(res);
}
}

if (remove) {
const removed = await fileCache.remove(hash);
const removed = fileCache.remove(hash);
if (!removed.success) {
return new Problem(removed.errorType, { detail: removed.errorMsg }).send(res);
}
Expand Down Expand Up @@ -105,12 +113,6 @@ const carboneCopyApi = {
res.setHeader('X-Report-Name', output.reportName);
res.setHeader('X-Template-Hash', template.hash);

if (truthy('cacheReport', options)) {
const rendered = await fileCache.write(output.report, output.reportName, 'binary');
if (rendered.success) {
res.setHeader('X-Report-Hash', rendered.hash);
}
}
log.info('Template rendered', { function: 'renderTemplate' });

// log metrics
Expand Down
30 changes: 30 additions & 0 deletions app/src/components/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { existsSync, readFileSync } = require('fs');
const { join } = require('path');
const { v4: uuidv4 } = require('uuid');

const log = require('./log')(module.filename);

module.exports = {
Expand Down Expand Up @@ -47,6 +50,33 @@ module.exports = {
return re.exec(filename)[1];
},

/**
* @function getGitRevision
* Gets the current git revision hash
* @see {@link https://stackoverflow.com/a/34518749}
* @returns {string} The git revision hash, or empty string
*/
getGitRevision() {
try {
const gitDir = (() => {
let dir = '.git', i = 0;
while (!existsSync(join(__dirname, dir)) && i < 5) {
dir = '../' + dir;
i++;
}
return dir;
})();

const head = readFileSync(join(__dirname, `${gitDir}/HEAD`)).toString().trim();
return (head.indexOf(':') === -1)
? head
: readFileSync(join(__dirname, `${gitDir}/${head.substring(5)}`)).toString().trim();
} catch (err) {
log.warn(err.message, { function: 'getGitRevision' });
return '';
}
},

/**
* @function prettyStringify
* Returns a pretty JSON representation of an object
Expand Down
Loading

0 comments on commit 14b74d0

Please sign in to comment.