react-express-mysql: get version from mysql

Signed-off-by: Jérémie Drouet <jeremie.drouet@gmail.com>
This commit is contained in:
Jérémie Drouet 2020-03-24 10:48:45 +01:00
parent 75aa52524c
commit 59724f87a1
8 changed files with 545 additions and 183 deletions

1
react-express-mysql/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

View File

@ -15,7 +15,7 @@ EXPOSE $PORT 9229 9230
COPY package.json /code/package.json COPY package.json /code/package.json
COPY package-lock.json /code/package-lock.json COPY package-lock.json /code/package-lock.json
RUN npm ci && npm cache clean --force RUN npm ci
# check every 30s to ensure this service returns HTTP 200 # check every 30s to ensure this service returns HTTP 200
HEALTHCHECK --interval=30s \ HEALTHCHECK --interval=30s \

File diff suppressed because it is too large Load Diff

View File

@ -4,20 +4,20 @@
"version": "2.0.0", "version": "2.0.0",
"description": "Node.js Hello world app using docker features for easy docker-compose local dev and solid production defaults", "description": "Node.js Hello world app using docker features for easy docker-compose local dev and solid production defaults",
"author": "Bret Fisher <bret@bretfisher.com>", "author": "Bret Fisher <bret@bretfisher.com>",
"main": "index.js", "main": "src/index.js",
"scripts": { "scripts": {
"start": "node index.js", "start": "node src/index.js",
"dev-docker": "../node_modules/nodemon/bin/nodemon.js --debug=5858", "start-watch": "nodemon src/index.js --inspect=0.0.0.0:9229",
"dev-host": "nodemon --debug=5858", "start-wait-debuger": "nodemon src/index.js --inspect-brk=0.0.0.0:9229",
"start-watch": "nodemon index.js --inspect=0.0.0.0:9229",
"start-wait-debuger": "nodemon index.js --inspect-brk=0.0.0.0:9229",
"test": "cross-env NODE_ENV=test PORT=8081 mocha --timeout 10000 --exit --inspect=0.0.0.0:9230", "test": "cross-env NODE_ENV=test PORT=8081 mocha --timeout 10000 --exit --inspect=0.0.0.0:9230",
"test-watch": "nodemon --exec \"npm test\"", "test-watch": "nodemon --exec \"npm test\"",
"test-wait-debuger": "cross-env NODE_ENV=test PORT=8081 mocha --no-timeouts --exit --inspect-brk=0.0.0.0:9230" "test-wait-debuger": "cross-env NODE_ENV=test PORT=8081 mocha --no-timeouts --exit --inspect-brk=0.0.0.0:9230"
}, },
"dependencies": { "dependencies": {
"express": "^4.16.3", "express": "^4.16.3",
"morgan": "^1.8.1" "knex": "^0.20.13",
"morgan": "^1.8.1",
"mysql2": "^2.1.0"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.1.2", "chai": "^4.1.2",

View File

@ -1,5 +1,18 @@
const fs = require("fs");
const readFileSync = filename => fs.readFileSync(filename).toString("utf8");
// Constants // Constants
module.exports = { module.exports = {
database: {
host: process.env.DATABASE_HOST || "localhost",
port: process.env.DATABASE_PORT,
database: process.env.DATABASE_DB,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD
? readFileSync(process.env.DATABASE_PASSWORD)
: null
},
port: process.env.PORT || 8080 port: process.env.PORT || 8080
// if you're not using docker-compose for local development, this will default to 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 // to prevent non-root permission problems with 80. Dockerfile is set to make this 80

View File

@ -0,0 +1,7 @@
const knex = require('knex');
const { database } = require('./config');
module.exports = knex({
client: 'mysql2',
connection: database,
});

View File

@ -12,13 +12,18 @@ const morgan = require("morgan");
// which is a best practice in Docker. Friends don't let friends code their apps to // which is a best practice in Docker. Friends don't let friends code their apps to
// do app logging to files in containers. // do app logging to files in containers.
const database = require("./database");
// Appi // Appi
const app = express(); const app = express();
app.use(morgan("common")); app.use(morgan("common"));
app.get("/", function(req, res) { app.get("/", function(req, res, next) {
res.json({ message: "Hello Docker World!" }); database.raw('select VERSION() version')
.then(([rows, columns]) => rows[0])
.then((row) => res.json({ message: `Hello from MySQL ${row.version}` }))
.catch(next);
}); });
app.get("/healthz", function(req, res) { app.get("/healthz", function(req, res) {

View File

@ -5,17 +5,23 @@ services:
args: args:
- NODE_ENV=development - NODE_ENV=development
context: backend context: backend
command: ../node_modules/.bin/nodemon --inspect=0.0.0.0:9229 command: npm run start-watch
environment: environment:
- DATABASE_DB=example
- DATABASE_USER=root
- DATABASE_PASSWORD=/run/secrets/db-password
- DATABASE_HOST=db
- NODE_ENV=development - NODE_ENV=development
ports: ports:
- 8080:80 - 8080:80
- 9229:9229 - 9229:9229
- 9230:9230 - 9230:9230
secrets:
- db-password
volumes: volumes:
- ./backend:/opt/app:delegated - ./backend/src:/code/src:ro
- ./backend/package.json:/opt/package.json - ./backend/package.json:/code/package.json
- ./backend/package-lock.json:/opt/package-lock.json - ./backend/package-lock.json:/code/package-lock.json
- back-notused:/opt/app/node_modules - back-notused:/opt/app/node_modules
networks: networks:
- public - public