From 849425db434bfc639bba89348243e62d9cb5606d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Preu=C3=9F?= Date: Sun, 5 Dec 2021 14:57:42 +0100 Subject: [PATCH] Hello world app --- .github/workflows/laravel.yml | 3 +- runtimes/hello-world:latest/Dockerfile | 21 +++++ runtimes/hello-world:latest/index.js | 111 +++++++++++++++++++++++ runtimes/hello-world:latest/package.json | 21 +++++ 4 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 runtimes/hello-world:latest/Dockerfile create mode 100644 runtimes/hello-world:latest/index.js create mode 100644 runtimes/hello-world:latest/package.json diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index dcc1b4b..1ac305c 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -5,7 +5,8 @@ jobs: strategy: fail-fast: false matrix: - runtime: [ "laravel:8.0-octane", "laravel:8.1-octane" ] + runtime: [ "hello-world:latest" ] + #runtime: [ "hello-world:latest", "laravel:8.0-octane", "laravel:8.1-octane" ] name: "bpkg.io/${{ matrix.runtime }}" runs-on: ubuntu-latest steps: diff --git a/runtimes/hello-world:latest/Dockerfile b/runtimes/hello-world:latest/Dockerfile new file mode 100644 index 0000000..6c12b55 --- /dev/null +++ b/runtimes/hello-world:latest/Dockerfile @@ -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"] \ No newline at end of file diff --git a/runtimes/hello-world:latest/index.js b/runtimes/hello-world:latest/index.js new file mode 100644 index 0000000..59443dc --- /dev/null +++ b/runtimes/hello-world:latest/index.js @@ -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."); + }); +} \ No newline at end of file diff --git a/runtimes/hello-world:latest/package.json b/runtimes/hello-world:latest/package.json new file mode 100644 index 0000000..387e200 --- /dev/null +++ b/runtimes/hello-world:latest/package.json @@ -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 ", + "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" + } +} \ No newline at end of file