Skip to content

Commit

Permalink
Added mongodb and kubernetes descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
rkamradt committed Jun 28, 2020
1 parent 22daf65 commit 3b18941
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions mongodb.yaml
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions mongoservice.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
43 changes: 43 additions & 0 deletions myfirstrepository.yaml
Original file line number Diff line number Diff line change
@@ -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
117 changes: 117 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Loading

0 comments on commit 3b18941

Please sign in to comment.