Skip to content

Commit 7f51d06

Browse files
committed
add readiness check in kubenetes pod and update database seeding
1 parent 793ee2b commit 7f51d06

File tree

4 files changed

+63
-50
lines changed

4 files changed

+63
-50
lines changed

kubernetes/server.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ spec:
2727
path: /health-check
2828
port: 3000
2929
initialDelaySeconds: 15
30+
readinessProbe:
31+
httpGet:
32+
path: /health-check
33+
port: 3000
3034

3135
---
3236
apiVersion: v1

server/src/api/health-check/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ router
1818
})
1919

2020

21-
module.exports = router
21+
module.exports = router

server/src/config/seed.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,34 @@ const Thing = require('../api/thing/thing.model')
77
const User = require('../api/user/user.model')
88

99
const seeding = async() => {
10-
await Thing.collection.deleteMany()
11-
await Thing.create(
12-
{ name: 'Our first Koa and Node app', info: 'Lightweight server' },
13-
{ name: 'Mongo is here', info: 'We use mongodb to store data!' }
14-
)
15-
await User.collection.deleteMany()
16-
await User.create({
17-
name: 'tester',
18-
19-
password: 'helloworld',
20-
role: 'user'
21-
})
22-
await User.create({
23-
name: 'admin',
24-
25-
password: 'admini',
26-
role: 'admin'
27-
})
28-
console.log('Finish populating database')
10+
let numberOfThings = await Thing.countDocuments()
11+
// populate initial things if nothing is in the database
12+
if (numberOfThings == 0) {
13+
await Thing.create({
14+
name: 'Our first Koa and Node app', info: 'Lightweight server'
15+
}, {
16+
name: 'Mongo is here', info: 'We use mongodb to store data!'
17+
})
18+
console.log('Finish populating database - things')
19+
}
20+
21+
let numberOfUsers = await User.countDocuments()
22+
// populate initial users if no user is in the database
23+
if (numberOfUsers == 0) {
24+
await User.create({
25+
name: 'tester',
26+
27+
password: 'helloworld',
28+
role: 'user'
29+
})
30+
await User.create({
31+
name: 'admin',
32+
33+
password: '123456',
34+
role: 'admin'
35+
})
36+
console.log('Finish populating database - users')
37+
}
2938
}
3039

3140
seeding()

server/src/routes.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
module.exports = function(app) {
2-
// Customized error handling, default error code: 500
3-
app.use(async (ctx, next) => {
4-
try {
5-
await next()
6-
} catch (err) {
7-
// Handle Validation Error
8-
if(err.name === 'ValidationError') {
9-
err.status = 422
10-
}
11-
// Handle Mongoose Item Duplication Error
12-
if (err.name === 'BulkWriteError' && err.code === 11000) {
13-
err.status = 422
14-
}
15-
ctx.status = err.status || 500
16-
ctx.body = { message: err.message }
17-
}
18-
})
19-
20-
// Health check does not require authentication
21-
app.use(require('./api/health-check').routes())
22-
23-
app.use(require('./api/auth').routes())
24-
app.use(require('./api/user').routes())
25-
// route middleware to verify a token
26-
app.use(require('./api/auth/service').isAuthenticated)
27-
app
28-
.use(require('./api/thing').routes())
29-
.use(require('./api/thing').allowedMethods())
30-
}
1+
module.exports = function(app) {
2+
// Customized error handling, default error code: 500
3+
app.use(async (ctx, next) => {
4+
try {
5+
await next()
6+
} catch (err) {
7+
// Handle Validation Error
8+
if(err.name === 'ValidationError') {
9+
err.status = 422
10+
}
11+
// Handle Mongoose Item Duplication Error
12+
if (err.name === 'BulkWriteError' && err.code === 11000) {
13+
err.status = 422
14+
}
15+
ctx.status = err.status || 500
16+
ctx.body = { message: err.message }
17+
}
18+
})
19+
20+
// Health check does not require authentication
21+
app.use(require('./api/health-check').routes())
22+
23+
app.use(require('./api/auth').routes())
24+
app.use(require('./api/user').routes())
25+
// route middleware to verify a token
26+
app.use(require('./api/auth/service').isAuthenticated)
27+
app
28+
.use(require('./api/thing').routes())
29+
.use(require('./api/thing').allowedMethods())
30+
}

0 commit comments

Comments
 (0)