Merge pull request #1 from sixeyed/v3

Add logging & config for image optimization exercise
This commit is contained in:
Bill Mills 2019-04-10 09:20:17 -04:00 committed by GitHub
commit 7b8510945e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 216 additions and 23 deletions

48
bb-stack-final.yaml Normal file
View File

@ -0,0 +1,48 @@
version: '3.7'
services:
bb-db:
image: ${dockerId}/bb-db:v3
build:
context: ./bulletin-board-db
networks:
- bb-net
volumes:
- sqlbackup:/var/opt/mssql
bb-app:
image: ${dockerId}/bb-app:v3.3
build:
context: ./bulletin-board-app
dockerfile: Dockerfile.v3.3
networks:
- bb-net
configs:
- source: logConfig
target: /app/config/logConfig.js
secrets:
- source: dbConfig
target: /app/config/dbConfig.js
bb-proxy:
image: ${dockerId}/bb-proxy:v3
build:
context: ./bulletin-board-proxy
ports:
- "80:80"
networks:
- bb-net
networks:
bb-net:
volumes:
sqlbackup:
configs:
logConfig:
external: true
secrets:
dbConfig:
external: true

33
bb-stack.yaml Normal file
View File

@ -0,0 +1,33 @@
version: '3.7'
services:
bb-db:
image: ${dockerId}/bb-db:v3
build:
context: ./bulletin-board-db
networks:
- bb-net
volumes:
- sqlbackup:/var/opt/mssql
bb-app:
image: ${dockerId}/bb-app:v3
build:
context: ./bulletin-board-app
networks:
- bb-net
bb-proxy:
image: ${dockerId}/bb-proxy:v3
build:
context: ./bulletin-board-proxy
ports:
- "80:80"
networks:
- bb-net
networks:
bb-net:
volumes:
sqlbackup:

View File

@ -7,5 +7,4 @@ RUN npm install
EXPOSE 8080 EXPOSE 8080
CMD [ "npm", "start" ] CMD [ "npm", "start" ]
COPY . . COPY . .

View File

@ -0,0 +1,15 @@
FROM node AS builder
WORKDIR /src
COPY package.json .
RUN npm install
# app image
FROM node
EXPOSE 8080
CMD npm start
WORKDIR /app
COPY --from=builder /src/node_modules/ /app/node_modules/
COPY . .

View File

@ -0,0 +1,15 @@
FROM node AS builder
WORKDIR /src
COPY package.json .
RUN npm install
# app image
FROM node:alpine
EXPOSE 8080
CMD npm start
WORKDIR /app
COPY --from=builder /src/node_modules/ /app/node_modules/
COPY . .

View File

@ -0,0 +1,19 @@
FROM node:10.15-stretch AS builder
WORKDIR /src
COPY package.json .
RUN npm install
# app image
FROM node:10.15.3-alpine
EXPOSE 8080
CMD [ "node", "server.js" ]
USER node
HEALTHCHECK --interval=5s --timeout=10s --start-period=15s \
CMD [ "node", "healthcheck.js" ]
WORKDIR /app
COPY --from=builder /src/node_modules/ /app/node_modules/
COPY . .

View File

@ -1,20 +1,21 @@
var db = require('./db.js'); var db = require('./db');
var log = require('../log');
exports.events = function (req, res) { exports.events = function (req, res) {
console.log('Loading DB events...'); log.Logger.info('Loading DB events...');
db.Events db.Events
.findAll() .findAll()
.then(events => { .then(events => {
console.log('Fetched events, count: ' + events.length); log.Logger.debug('Fetched events, count: ' + events.length);
res.json(events); res.json(events);
}) })
.catch(err => { .catch(err => {
console.error('** Fetch failed: ', err); log.Logger.error('** Fetch failed: ', err);
}); });
}; };
exports.event = function (req, res) { exports.event = function (req, res) {
console.log('Handling event call, method: ' + req.method + ', event ID: ' + req.params.eventId) log.Logger.debug('Handling event call, method: ' + req.method + ', event ID: ' + req.params.eventId)
switch(req.method) { switch(req.method) {
case "DELETE": case "DELETE":
db.Events db.Events
@ -23,7 +24,7 @@ exports.event = function (req, res) {
id: req.params.eventId id: req.params.eventId
} }
}).then(function() { }).then(function() {
console.log('Deleted event with id: ' + req.params.eventId) log.Logger.info('Deleted event with id: ' + req.params.eventId)
res.status(200).end(); res.status(200).end();
}); });
break break
@ -35,6 +36,7 @@ exports.event = function (req, res) {
date: req.body.date date: req.body.date
}) })
.then(function() { .then(function() {
log.Logger.info('Created event with title: ' + req.body.title)
res.send('{}'); res.send('{}');
res.status(201).end(); res.status(201).end();
}); });

View File

@ -1,25 +1,29 @@
var Sequelize = require('sequelize'); var Sequelize = require('sequelize');
var username = 'sa'; var dbConfig = require('../config/dbConfig');
var password = 'DockerCon!!!'; var log = require('../log');
var host = 'bb-db';
var dbName = 'BulletinBoard';
var sequelize = new Sequelize(dbName, username, password, { log.Logger.debug('Initializing connection to SQL Server: %s', dbConfig.connection.host);
var sequelize = new Sequelize(dbConfig.connection.dbName, dbConfig.connection.username, dbConfig.connection.password, {
dialect: 'mssql', dialect: 'mssql',
host: host, host: dbConfig.connection.host,
port: 1433, port: dbConfig.connection.port,
pool: {
max: dbConfig.pool.max
},
dialectOptions: { dialectOptions: {
requestTimeout: 30000 requestTimeout: 30000
} }
}); });
sequelize sequelize
.authenticate() .authenticate()
.then(() => { .then(() => {
console.log('Successful connection to SQL Server.'); log.Logger.info('Successful connection to SQL Server: %s', dbConfig.connection.host);
log.Logger.info('--Using connection pool max: %d', dbConfig.pool.max)
}) })
.catch(err => { .catch(err => {
console.error('** SQL Server connection failed: ', err); log.Logger.error('** SQL Server connection failed: ', err);
process.exit(1); process.exit(1);
}); });

View File

@ -0,0 +1,13 @@
var dbConfig = module.exports = {};
dbConfig.connection = {
username: 'sa',
password: 'DockerCon!!!',
host: 'bb-db',
post: 1433,
dbName: 'BulletinBoard'
};
dbConfig.pool = {
max: 10
};

View File

@ -0,0 +1,14 @@
const { format, transports } = require('winston');
var logConfig = module.exports = {};
logConfig.options = {
format: format.combine(
format.splat(),
format.simple()
),
transports: [
new transports.Console({
level: 'debug'
})
]
};

View File

@ -0,0 +1,24 @@
var http = require("http");
var options = {
host : "localhost",
port : "8080",
timeout : 2000
};
var request = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
if (res.statusCode == 200) {
process.exit(0);
}
else {
process.exit(1);
}
});
request.on('error', function(err) {
console.log('ERROR');
process.exit(1);
});
request.end();

View File

@ -0,0 +1,5 @@
const winston = require('winston');
var logConfig = require('./config/logConfig');
const logger = winston.createLogger(logConfig.options);
exports.Logger = logger;

View File

@ -14,7 +14,8 @@
"vue-resource": "^0.1.17", "vue-resource": "^0.1.17",
"tedious": "^2.0.1", "tedious": "^2.0.1",
"sequelize": "^4.20.1", "sequelize": "^4.20.1",
"prom-client": "^10.2.2" "prom-client": "^10.2.2",
"winston": "3.2.1"
}, },
"devDependencies": { "devDependencies": {
"body-parser": "^1.14.1", "body-parser": "^1.14.1",

View File

@ -5,7 +5,8 @@ var express = require('express'),
morgan = require('morgan'), morgan = require('morgan'),
prometheus = require('prom-client'), prometheus = require('prom-client'),
routes = require('./backend'), routes = require('./backend'),
api = require('./backend/api'); api = require('./backend/api'),
log = require('./log');
var app = module.exports = express(); var app = module.exports = express();
@ -68,4 +69,4 @@ app.use((req, res, next) => {
prometheus.collectDefaultMetrics(); prometheus.collectDefaultMetrics();
app.listen(8080); app.listen(8080);
console.log('Magic happens on port 8080...'); log.Logger.info('Magic happens on port 8080...');

View File

@ -1,4 +1,4 @@
FROM microsoft/mssql-server-linux:2017-CU1 FROM microsoft/mssql-server-linux:2017-CU13
ENV ACCEPT_EULA=Y \ ENV ACCEPT_EULA=Y \
MSSQL_SA_PASSWORD=DockerCon!!! MSSQL_SA_PASSWORD=DockerCon!!!

View File

@ -1,4 +1,4 @@
FROM nginx:1.13.6 FROM nginx:1.15.10-alpine
RUN mkdir -p /data/nginx/cache RUN mkdir -p /data/nginx/cache
COPY nginx.conf /etc/nginx/nginx.conf COPY nginx.conf /etc/nginx/nginx.conf