mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-26 10:43:35 +02:00
react-express-mysql: split backend code
Signed-off-by: Jérémie Drouet <jeremie.drouet@gmail.com>
This commit is contained in:
parent
2ef179e53b
commit
75aa52524c
7 changed files with 130 additions and 126 deletions
7
react-express-mysql/backend/src/config.js
vendored
Normal file
7
react-express-mysql/backend/src/config.js
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
// Constants
|
||||
module.exports = {
|
||||
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 :)
|
||||
};
|
48
react-express-mysql/backend/src/index.js
vendored
Executable file
48
react-express-mysql/backend/src/index.js
vendored
Executable file
|
@ -0,0 +1,48 @@
|
|||
const app = require("./server");
|
||||
const { port } = require("./config");
|
||||
|
||||
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.exit(1);
|
||||
}
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
//
|
||||
// need above in docker container to properly exit
|
||||
//
|
31
react-express-mysql/backend/src/server.js
vendored
Normal file
31
react-express-mysql/backend/src/server.js
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
// 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 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.
|
||||
|
||||
// Appi
|
||||
const app = express();
|
||||
|
||||
app.use(morgan("common"));
|
||||
|
||||
app.get("/", function(req, res) {
|
||||
res.json({ message: "Hello Docker World!" });
|
||||
});
|
||||
|
||||
app.get("/healthz", 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");
|
||||
});
|
||||
|
||||
module.exports = app;
|
Loading…
Add table
Add a link
Reference in a new issue