forked from BretFisher/node-docker-good-defaults
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (68 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// simple node web server that displays hello world
// optimized for Docker image
const express = require('express');
// this example uses express web framework so we know what longer build times
// do and how Dockerfile layer ordering matters. If you mess up Dockerfile ordering
// you'll see long build times on every code change + build. If done correctly,
// code changes should be only a few seconds to build locally due to build cache.
const mongoose = require('mongoose');
const morgan = require('morgan');
// morgan provides easy logging for express, and by default it logs to stdout
// which is a best practice in Docker. Friends don't let friends code their apps to
// do app logging to files in containers.
// Constants
const PORT = process.env.PORT || 8080;
// if you're not using docker-compose for local development, this will default to 8080
// to prevent non-root permission problems with 80. Dockerfile is set to make this 80
// because containers don't have that issue :)
// App
const app = express();
mongoose.connect(process.env.MONGO_URI)
.then(function () {
console.log("Connected to MongoDB");
// Seed the example product table
require('./models/seeds/product.js')();
}).catch(err => console.error(err));
app.use(morgan('common'));
app.get('/', function (req, res) {
res.send('Hello Docker World\n');
});
app.get('/healthcheck', function (req, res) {
// do app logic here to determine if app is truly healthy
// you should return 200 if healthy, and anything else will fail
// if you want, you should be able to restrict this to localhost (include ipv4 and ipv6)
res.send('I am happy and healthy\n');
});
const server = app.listen(PORT, function () {
console.log('Webserver is ready');
});
//
// need this in docker container to properly exit since node doesn't handle SIGINT/SIGTERM
// this also won't work on using npm start since:
// https://github.com/npm/npm/issues/4603
// https://github.com/npm/npm/pull/10868
// https://github.com/RisingStack/kubernetes-graceful-shutdown-example/blob/master/src/index.js
// if you want to use npm then start with `docker run --init` to help, but I still don't think it's
// a graceful shutdown of node process
//
// quit on ctrl-c when running docker in terminal
process.on('SIGINT', function onSigint() {
console.info('Got SIGINT (aka ctrl-c in docker). Graceful shutdown ', new Date().toISOString());
shutdown();
});
// quit properly on docker stop
process.on('SIGTERM', function onSigterm() {
console.info('Got SIGTERM (docker container stop). Graceful shutdown ', new Date().toISOString());
shutdown();
});
// shut down server
function shutdown() {
server.close(function onServerClosed(err) {
if (err) {
console.error(err);
process.exitCode = 1;
}
process.exit();
})
}
module.exports = app;