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-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
HEALTHCHECK --interval=30s \

File diff suppressed because it is too large Load Diff

View File

@ -4,20 +4,20 @@
"version": "2.0.0",
"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>",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"start": "node index.js",
"dev-docker": "../node_modules/nodemon/bin/nodemon.js --debug=5858",
"dev-host": "nodemon --debug=5858",
"start-watch": "nodemon index.js --inspect=0.0.0.0:9229",
"start-wait-debuger": "nodemon index.js --inspect-brk=0.0.0.0:9229",
"start": "node src/index.js",
"start-watch": "nodemon src/index.js --inspect=0.0.0.0:9229",
"start-wait-debuger": "nodemon src/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-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"
},
"dependencies": {
"express": "^4.16.3",
"morgan": "^1.8.1"
"knex": "^0.20.13",
"morgan": "^1.8.1",
"mysql2": "^2.1.0"
},
"devDependencies": {
"chai": "^4.1.2",

View File

@ -1,5 +1,18 @@
const fs = require("fs");
const readFileSync = filename => fs.readFileSync(filename).toString("utf8");
// Constants
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
// 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

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
// do app logging to files in containers.
const database = require("./database");
// Appi
const app = express();
app.use(morgan("common"));
app.get("/", function(req, res) {
res.json({ message: "Hello Docker World!" });
app.get("/", function(req, res, next) {
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) {

View File

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