Hello world app

This commit is contained in:
René Preuß
2021-12-05 14:57:42 +01:00
parent 04594dc41a
commit 849425db43
4 changed files with 155 additions and 1 deletions

View File

@@ -0,0 +1,21 @@
FROM node:14-alpine AS build
WORKDIR /app
COPY . /app
RUN set -ex \
# Build JS-Application
&& npm install --production \
# Delete unnecessary files
&& rm package* \
# Correct User's file access
&& chown -R node:node /app
FROM node:14-alpine AS final
WORKDIR /app
COPY --from=build /app /app
ENV HTTP_PORT=8000
EXPOSE $HTTP_PORT $HTTPS_PORT
USER 1000
EXPOSE 8000
CMD ["node", "./index.js"]

View File

@@ -0,0 +1,111 @@
const express = require('express')
const morgan = require('morgan');
const http = require('http')
const app = express()
const os = require('os');
const jwt = require('jsonwebtoken');
const concat = require('concat-stream');
const { promisify } = require('util');
const sleep = promisify(setTimeout);
app.set('json spaces', 2);
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
app.use(morgan('combined'));
app.use(function(req, res, next){
req.pipe(concat(function(data){
req.body = data.toString('utf8');
next();
}));
});
app.all('*', (req, res) => {
const echo = {
path: req.path,
headers: req.headers,
method: req.method,
body: req.body,
cookies: req.cookies,
fresh: req.fresh,
hostname: req.hostname,
ip: req.ip,
ips: req.ips,
protocol: req.protocol,
query: req.query,
subdomains: req.subdomains,
xhr: req.xhr,
os: {
hostname: os.hostname()
},
connection: {
servername: req.connection.servername
}
};
if(req.is('application/json')){
echo.json = JSON.parse(req.body)
}
if (process.env.JWT_HEADER) {
let token = req.headers[process.env.JWT_HEADER.toLowerCase()];
if (!token) {
echo.jwt = token;
} else {
token = token.split(" ").pop();
const decoded = jwt.decode(token, {complete: true});
echo.jwt = decoded;
}
}
const setResponseStatusCode = parseInt(req.headers["x-set-response-status-code"] || req.query["x-set-response-status-code"], 10)
if (100 <= setResponseStatusCode && setResponseStatusCode < 600) {
res.status(setResponseStatusCode)
}
const sleepTime = parseInt(req.headers["x-set-response-delay-ms"] || req.query["x-set-response-delay-ms"], 0)
sleep(sleepTime).then(() => {
if (process.env.ECHO_BACK_TO_CLIENT != undefined && process.env.ECHO_BACK_TO_CLIENT == "false"){
res.end();
} else {
res.json(echo);
}
if (process.env.LOG_IGNORE_PATH != req.path) {
console.log('-----------------')
let spacer = 4;
if(process.env.LOG_WITHOUT_NEWLINE){
spacer = null;
}
console.log(JSON.stringify(echo, null, spacer));
}
});
});
let httpServer = http.createServer(app).listen(process.env.HTTP_PORT || 8000);
console.log(`Listening on port ${process.env.HTTP_PORT || 8000}`);
let calledClose = false;
process.on('exit', function () {
if (calledClose) return;
console.log('Got exit event. Trying to stop Express server.');
server.close(function() {
console.log("Express server closed");
});
});
process.on('SIGINT', shutDown);
process.on('SIGTERM', shutDown);
function shutDown(){
console.log('Got a kill signal. Trying to exit gracefully.');
calledClose = true;
httpServer.close(function() {
console.log("HTTP servers closed. Asking process to exit.");
});
}

View File

@@ -0,0 +1,21 @@
{
"name": "@bitinflow/hello-world",
"version": "1.0.0",
"description": "JSON service for debugging a web setup",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "bitinflow Containers Team <containers@bitinflow.com>",
"license": "MIT",
"engines": {
"node": ">=6.3.0"
},
"dependencies": {
"concat-stream": "^2.0.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.0",
"morgan": "^1.10.0"
}
}