From 3b189410bfdb5300a5edf5d55aae187937483ff2 Mon Sep 17 00:00:00 2001 From: Randal Kamradt Date: Sun, 28 Jun 2020 09:29:07 -0700 Subject: [PATCH] Added mongodb and kubernetes descriptors --- Dockerfile | 8 +++ app.js | 4 ++ mongodb.yaml | 44 ++++++++++++++++ mongoservice.js | 57 ++++++++++++++++++++ myfirstrepository.yaml | 43 +++++++++++++++ package-lock.json | 117 +++++++++++++++++++++++++++++++++++++++++ package.json | 1 + routes/index.js | 8 ++- views/index.jade | 3 ++ 9 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 mongodb.yaml create mode 100644 mongoservice.js create mode 100644 myfirstrepository.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..760fef4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM node:10.15.3-alpine +# Create app directory +WORKDIR /usr/src/app +COPY package*.json ./ +RUN npm install +# Bundle app source +COPY . . +CMD [ "node", "bin/www" ] diff --git a/app.js b/app.js index 662bcc9..84e6bce 100644 --- a/app.js +++ b/app.js @@ -3,10 +3,13 @@ var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); +var mongoservice = require('./mongoservice') var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); +mongoservice.initialize(); + var app = express(); // view engine setup @@ -17,6 +20,7 @@ app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); +app.use(mongoservice.saveRequestAsync); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); diff --git a/mongodb.yaml b/mongodb.yaml new file mode 100644 index 0000000..f0b6c2f --- /dev/null +++ b/mongodb.yaml @@ -0,0 +1,44 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mongodb +spec: + serviceName: "mongodb" + replicas: 1 + selector: + matchLabels: + app: mongodb + template: + metadata: + labels: + app: mongodb + selector: mongodb + spec: + containers: + - name: mongodb + image: mongo:4.0.8 + env: + - name: MONGO_INITDB_ROOT_USERNAME + valueFrom: + secretKeyRef: + name: mongo-secret + key: username + - name: MONGO_INITDB_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: mongo-secret + key: password +--- +apiVersion: v1 +kind: Service +metadata: + name: mongodb + labels: + name: mongodb +spec: + ports: + - port: 27017 + targetPort: 27017 + clusterIP: None + selector: + app: mongodb diff --git a/mongoservice.js b/mongoservice.js new file mode 100644 index 0000000..253e9c7 --- /dev/null +++ b/mongoservice.js @@ -0,0 +1,57 @@ +const MongoClient = require('mongodb').MongoClient + +const MONGO_USER = process.env.MONGO_USER +const MONGO_PASS = process.env.MONGO_PASS +const MONGO_SERVER = process.env.MONGO_SERVER || 'mongodb' +const MONGO_PORT = process.env.MONGO_PORT || 27017 + +const dbName = 'requests' + +const mongourl = `mongodb://${MONGO_USER}:${MONGO_PASS}@${MONGO_SERVER}:${MONGO_PORT}`; +const client = new MongoClient(mongourl, { + useUnifiedTopology: true +}) + +module.exports.saveRequestAsync = async (req, res, next) => { + try { + const db = client.db(dbName) + + const r = await db.collection('inserts').insertOne({ + 'verb': req.method, + 'host': req.host, + 'path': req.path + }, { + w: 'majority', + wtimeout: 10000, + serializeFunctions: true, + forceServerObjectId: true + } + ) + console.log('Inserted') + } catch (err) { + console.log(err.stack) + } + + next() +} + +module.exports.addListToRequest = async (req,res,next) => { + try { + const db = client.db(dbName) + + req.list = await db.collection('inserts').find({}).toArray(); + } catch (err) { + console.log(err.stack) + } + next() +} + +module.exports.initialize = async () => { + try { + console.log(`Connecting to mongodb://${MONGO_USER}:******@${MONGO_SERVER}:${MONGO_PORT}`) + await client.connect() + console.log('Connected correctly to server') + } catch (err) { + console.log(err.stack); + } +} diff --git a/myfirstrepository.yaml b/myfirstrepository.yaml new file mode 100644 index 0000000..aecce0d --- /dev/null +++ b/myfirstrepository.yaml @@ -0,0 +1,43 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: myfirstrepository-deployment + labels: + app: myfirstrepository +spec: + selector: + matchLabels: + app: myfirstrepository + template: + metadata: + labels: + app: myfirstrepository + spec: + containers: + - name: myfirstrepository + image: docker.io/rlkamradt/myfirstrepository:latest + ports: + - containerPort: 3000 + env: + - name: MONGO_USER + valueFrom: + secretKeyRef: + name: mongo-secret + key: username + - name: MONGO_PASS + valueFrom: + secretKeyRef: + name: mongo-secret + key: password +--- +apiVersion: v1 +kind: Service +metadata: + name: myfirstrepository-service +spec: + type: LoadBalancer + selector: + app: myfirstrepository + ports: + - port: 80 + targetPort: 3000 diff --git a/package-lock.json b/package-lock.json index ad15e94..9916c73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -59,6 +59,15 @@ "safe-buffer": "5.1.2" } }, + "bl": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.0.tgz", + "integrity": "sha512-wbgvOpqopSr7uq6fJrLH8EsvYMJf9gzfo2jCsL2eTy75qXPukA4pCgHamOQkZtY5vmfVtjB+P3LNlMHW5CEZXA==", + "requires": { + "readable-stream": "^2.3.5", + "safe-buffer": "^5.1.1" + } + }, "body-parser": { "version": "1.18.3", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", @@ -76,6 +85,11 @@ "type-is": "~1.6.16" } }, + "bson": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.4.tgz", + "integrity": "sha512-S/yKGU1syOMzO86+dGpg2qGoDL0zvzcb262G+gqEy6TgP6rt6z6qxSFX/8X6vLC91P7G7C3nLs0+bvDzmvBA3Q==" + }, "bytes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", @@ -178,6 +192,11 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, "css": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/css/-/css-1.0.8.tgz", @@ -210,6 +229,11 @@ "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=" }, + "denque": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", + "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" + }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -352,6 +376,11 @@ "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.2.tgz", "integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==" }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, "jade": { "version": "1.11.0", "resolved": "https://registry.npmjs.org/jade/-/jade-1.11.0.tgz", @@ -401,6 +430,12 @@ "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, + "memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -442,6 +477,19 @@ "minimist": "^1.2.5" } }, + "mongodb": { + "version": "3.5.9", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.5.9.tgz", + "integrity": "sha512-vXHBY1CsGYcEPoVWhwgxIBeWqP3dSu9RuRDsoLRPTITrcrgm1f0Ubu1xqF9ozMwv53agmEiZm0YGo+7WL3Nbug==", + "requires": { + "bl": "^2.2.0", + "bson": "^1.1.4", + "denque": "^1.4.1", + "require_optional": "^1.0.1", + "safe-buffer": "^5.1.2", + "saslprep": "^1.0.0" + } + }, "morgan": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", @@ -495,6 +543,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "promise": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/promise/-/promise-6.1.0.tgz", @@ -533,11 +586,39 @@ "unpipe": "1.0.0" } }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, "repeat-string": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz", "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=" }, + "require_optional": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", + "requires": { + "resolve-from": "^2.0.0", + "semver": "^5.1.0" + } + }, + "resolve-from": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" + }, "right-align": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/right-align/-/right-align-0.1.3.tgz", @@ -556,6 +637,20 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "saslprep": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "optional": true, + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + }, "send": { "version": "0.16.2", "resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz", @@ -600,11 +695,28 @@ "amdefine": ">=0.0.4" } }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", + "optional": true, + "requires": { + "memory-pager": "^1.0.2" + } + }, "statuses": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, "transformers": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/transformers/-/transformers-2.1.0.tgz", @@ -684,6 +796,11 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/package.json b/package.json index 49ffec0..1f9d32e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "express": "~4.16.1", "http-errors": "~1.6.3", "jade": "~1.11.0", + "mongodb": "^3.5.9", "morgan": "~1.9.1" } } diff --git a/routes/index.js b/routes/index.js index 0555817..bf79f8d 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,9 +1,13 @@ var express = require('express'); var router = express.Router(); +var mongoservice = require('../mongoservice') /* GET home page. */ -router.get('/', function(req, res, next) { - res.render('index', { title: 'My awesome server!' }); +router.get('/', mongoservice.addListToRequest, (req,res) => { + res.render('index', { + title: 'My awesome server!', + list: req.list + }); }); module.exports = router; diff --git a/views/index.jade b/views/index.jade index 3d63b9a..98dda0a 100644 --- a/views/index.jade +++ b/views/index.jade @@ -3,3 +3,6 @@ extends layout block content h1= title p Welcome to #{title} + ul + each val in list + li= JSON.stringify(val)