Code files
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const wiring = require('./wiring')
|
||||
const service = require('./service')()
|
||||
|
||||
|
||||
wiring(service)
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "adderservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "tap test"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"restify": "^4.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tap": "^10.3.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = service
|
||||
|
||||
function service () {
|
||||
function add (args, cb) {
|
||||
const {first, second} = args
|
||||
const result = (parseInt(first, 10) + parseInt(second, 10))
|
||||
cb(null, {result: result.toString()})
|
||||
}
|
||||
|
||||
return { add }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const {test} = require('tap')
|
||||
const service = require('../service')()
|
||||
|
||||
test('test add', (t) => {
|
||||
t.plan(2)
|
||||
|
||||
service.add({first: 1, second: 2}, (err, answer) => {
|
||||
t.error(err)
|
||||
t.same(answer, {result: 3})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
const { ADDERSERVICE_SERVICE_PORT } = process.env
|
||||
|
||||
module.exports = wiring
|
||||
|
||||
function wiring (service) {
|
||||
const server = restify.createServer()
|
||||
|
||||
server.get('/add/:first/:second', (req, res, next) => {
|
||||
service.add(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
next()
|
||||
return
|
||||
}
|
||||
res.send(200, result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.listen(ADDERSERVICE_SERVICE_PORT, '0.0.0.0', () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const wiring = require('./wiring')
|
||||
const service = require('./service')()
|
||||
|
||||
wiring(service)
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "auditservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"concordant": "^0.2.1",
|
||||
"mongo": "^0.1.0",
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
'use strict'
|
||||
|
||||
const { MongoClient } = require('mongodb')
|
||||
const { dns } = require('concordant')()
|
||||
|
||||
module.exports = service
|
||||
|
||||
function service () {
|
||||
|
||||
var db
|
||||
|
||||
setup()
|
||||
|
||||
function setup () {
|
||||
const mongo = '_main._tcp.mongo.micro.svc.cluster.local'
|
||||
|
||||
dns.resolve(mongo, (err, locs) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const url = `mongodb://${host}:${port}/audit`
|
||||
MongoClient.connect(url, (err, client) => {
|
||||
if (err) {
|
||||
console.log('failed to connect to MongoDB retrying in 100ms')
|
||||
setTimeout(setup, 100)
|
||||
return
|
||||
}
|
||||
db = client
|
||||
db.on('close', () => db = null)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function append (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const audit = db.collection('audit')
|
||||
const data = {
|
||||
ts: Date.now(),
|
||||
calc: args.calc,
|
||||
result: args.calcResult
|
||||
}
|
||||
|
||||
audit.insert(data, (err, result) => {
|
||||
if (err) {
|
||||
cb(err)
|
||||
return
|
||||
}
|
||||
cb(null, {result: result.toString()})
|
||||
})
|
||||
}
|
||||
|
||||
function list (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const audit = db.collection('audit')
|
||||
audit.find({}, {limit: 10}).toArray((err, docs) => {
|
||||
if (err) {
|
||||
cb(err)
|
||||
return
|
||||
}
|
||||
cb(null, {list: docs})
|
||||
})
|
||||
}
|
||||
|
||||
return { append, list }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
const { AUDITSERVICE_SERVICE_PORT } = process.env
|
||||
|
||||
module.exports = wiring
|
||||
|
||||
function wiring (service) {
|
||||
const server = restify.createServer()
|
||||
|
||||
server.use(restify.bodyParser())
|
||||
|
||||
server.post('/append', (req, res, next) => {
|
||||
service.append(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
res.send(result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.get('/list', (req, res, next) => {
|
||||
service.list(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
res.send(200, result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.listen(AUDITSERVICE_SERVICE_PORT, '0.0.0.0', () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const wiring = require('./wiring')
|
||||
const service = require('./service')()
|
||||
wiring(service)
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "eventservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"concordant": "^0.2.1",
|
||||
"mongo": "^0.1.0",
|
||||
"redis": "^2.6.5",
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
'use strict'
|
||||
|
||||
const { MongoClient } = require('mongodb')
|
||||
const { dns } = require('concordant')()
|
||||
|
||||
module.exports = service
|
||||
|
||||
function service () {
|
||||
var db
|
||||
|
||||
setup()
|
||||
|
||||
function setup () {
|
||||
const mongo = '_main._tcp.mongo.micro.svc.cluster.local'
|
||||
|
||||
dns.resolve(mongo, (err, locs) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const url = `mongodb://${host}:${port}/events`
|
||||
MongoClient.connect(url, (err, client) => {
|
||||
if (err) {
|
||||
console.log('failed to connect to MongoDB retrying in 100ms')
|
||||
setTimeout(setup, 100)
|
||||
return
|
||||
}
|
||||
db = client
|
||||
db.on('close', () => db = null)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function record (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const events = db.collection('events')
|
||||
const data = {
|
||||
ts: Date.now(),
|
||||
eventType: args.type,
|
||||
url: args.url
|
||||
}
|
||||
events.insert(data, (err, result) => {
|
||||
if (err) {
|
||||
cb(err)
|
||||
return
|
||||
}
|
||||
cb(null, result)
|
||||
})
|
||||
}
|
||||
|
||||
function summary (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const summary = {}
|
||||
const events = db.collection('events')
|
||||
events.find({}).toArray( (err, docs) => {
|
||||
if (err) return cb(err)
|
||||
|
||||
docs.forEach(function (doc) {
|
||||
if (!(summary[doc.url])) {
|
||||
summary[doc.url] = 1
|
||||
} else {
|
||||
summary[doc.url]++
|
||||
}
|
||||
})
|
||||
cb(null, summary)
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
record: record,
|
||||
summary: summary
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
'use strict'
|
||||
|
||||
const { dns } = require('concordant')()
|
||||
const redis = require('redis')
|
||||
const QNAME = 'eventservice'
|
||||
|
||||
module.exports = wiring
|
||||
|
||||
function wiring (service) {
|
||||
|
||||
const endpoint = '_main._tcp.redis.micro.svc.cluster.local'
|
||||
|
||||
dns.resolve(endpoint, (err, locs) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
return
|
||||
}
|
||||
const { port, host } = locs[0]
|
||||
pullFromQueue(redis.createClient(port, host))
|
||||
})
|
||||
|
||||
function pullFromQueue (client) {
|
||||
client.brpop(QNAME, 5, function (err, data) {
|
||||
if (err) console.error(err)
|
||||
if (err || !data) {
|
||||
pullFromQueue(client)
|
||||
return
|
||||
}
|
||||
const msg = JSON.parse(data[1])
|
||||
const { action, returnPath } = msg
|
||||
const cmd = service[action]
|
||||
if (typeof cmd !== 'function') {
|
||||
pullFromQueue(client)
|
||||
return
|
||||
}
|
||||
cmd(msg, (err, result) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
pullFromQueue(client)
|
||||
return
|
||||
}
|
||||
if (!returnPath) {
|
||||
pullFromQueue(client)
|
||||
return
|
||||
}
|
||||
client.lpush(returnPath, JSON.stringify(result), (err) => {
|
||||
if (err) console.error(err)
|
||||
pullFromQueue(client)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
fuge_global:
|
||||
run_containers: true
|
||||
dns_enabled: true
|
||||
dns_host: 127.0.0.1
|
||||
dns_port: 53053
|
||||
dns_suffix: svc.cluster.local
|
||||
dns_namespace: micro
|
||||
tail: true
|
||||
monitor: true
|
||||
monitor_excludes:
|
||||
- '**/node_modules/**'
|
||||
- '**/.git/**'
|
||||
- '**/*.log'
|
||||
adderservice:
|
||||
type: node
|
||||
path: ../adderservice
|
||||
run: node index.js
|
||||
ports:
|
||||
- main=8080
|
||||
auditservice:
|
||||
type: process
|
||||
path: ../auditservice
|
||||
run: 'node index.js'
|
||||
ports:
|
||||
- main=8081
|
||||
eventservice:
|
||||
type: process
|
||||
path: ../eventservice
|
||||
run: 'node index.js'
|
||||
webapp:
|
||||
type: process
|
||||
path: ../webapp
|
||||
run: npm start
|
||||
ports:
|
||||
- http=3000
|
||||
mongo:
|
||||
image: mongo
|
||||
type: container
|
||||
ports:
|
||||
- main=27017:27017
|
||||
redis:
|
||||
image: redis
|
||||
type: container
|
||||
ports:
|
||||
- main=6379:6379
|
||||
@@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
// provides environment variable setup for concordant
|
||||
|
||||
const env = {
|
||||
DNS_NAMESPACE: 'micro',
|
||||
DNS_SUFFIX: 'svc.cluster.local'
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
Object.assign(env, {
|
||||
DNS_HOST: '127.0.0.1',
|
||||
DNS_PORT: '53053'
|
||||
})
|
||||
}
|
||||
|
||||
Object.assign(process.env, env)
|
||||
@@ -0,0 +1,43 @@
|
||||
'use strict'
|
||||
require('./env')
|
||||
const { dns } = require('concordant')()
|
||||
const redis = require('redis')
|
||||
const CliTable = require('cli-table')
|
||||
const QNAME = 'eventservice'
|
||||
const RESPONSE_QUEUE = 'summary'
|
||||
const ENDPOINT = '_main._tcp.redis.micro.svc.cluster.local'
|
||||
|
||||
dns.resolve(ENDPOINT, report)
|
||||
|
||||
function report (err, locs) {
|
||||
if (err) { return console.log(err) }
|
||||
const { port, host } = locs[0]
|
||||
const client = redis.createClient(port, host)
|
||||
const event = JSON.stringify({
|
||||
action: 'summary',
|
||||
returnPath: RESPONSE_QUEUE
|
||||
})
|
||||
|
||||
client.lpush(QNAME, event, (err) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
|
||||
client.brpop(RESPONSE_QUEUE, 5, (err, data) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
const summary = JSON.parse(data[1])
|
||||
const cols = Object.keys(summary).map((url) => [url, summary[url]])
|
||||
const table = new CliTable({
|
||||
head: ['url', 'count'],
|
||||
colWidths: [50, 10]
|
||||
})
|
||||
table.push(...cols)
|
||||
console.log(table.toString())
|
||||
client.quit()
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "report",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"cli-table": "^0.3.1",
|
||||
"concordant": "^0.2.1",
|
||||
"redis": "^2.6.5"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
var express = require('express')
|
||||
var path = require('path')
|
||||
var favicon = require('serve-favicon')
|
||||
var logger = require('morgan')
|
||||
var cookieParser = require('cookie-parser')
|
||||
var bodyParser = require('body-parser')
|
||||
var eventLogger = require('./lib/event-logger')
|
||||
|
||||
var index = require('./routes/index')
|
||||
var users = require('./routes/users')
|
||||
var add = require('./routes/add')
|
||||
var audit = require('./routes/audit')
|
||||
|
||||
var app = express()
|
||||
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
// uncomment after placing your favicon in /public
|
||||
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
|
||||
app.use(eventLogger())
|
||||
app.use(logger('dev'))
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
app.use(cookieParser())
|
||||
app.use(express.static(path.join(__dirname, 'public')))
|
||||
|
||||
app.use('/', index)
|
||||
app.use('/users', users)
|
||||
app.use('/add', add)
|
||||
app.use('/audit', audit)
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
var err = new Error('Not Found')
|
||||
err.status = 404
|
||||
next(err)
|
||||
})
|
||||
|
||||
// error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {}
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500)
|
||||
res.render('error')
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var app = require('../app');
|
||||
var debug = require('debug')('webapp:server');
|
||||
var http = require('http');
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
|
||||
var port = normalizePort(process.env.PORT || '3000');
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
* Create HTTP server.
|
||||
*/
|
||||
|
||||
var server = http.createServer(app);
|
||||
|
||||
/**
|
||||
* Listen on provided port, on all network interfaces.
|
||||
*/
|
||||
|
||||
server.listen(port);
|
||||
server.on('error', onError);
|
||||
server.on('listening', onListening);
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
*/
|
||||
|
||||
function normalizePort(val) {
|
||||
var port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
return val;
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
// port number
|
||||
return port;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "error" event.
|
||||
*/
|
||||
|
||||
function onError(error) {
|
||||
if (error.syscall !== 'listen') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
var bind = typeof port === 'string'
|
||||
? 'Pipe ' + port
|
||||
: 'Port ' + port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
console.error(bind + ' requires elevated privileges');
|
||||
process.exit(1);
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
console.error(bind + ' is already in use');
|
||||
process.exit(1);
|
||||
break;
|
||||
default:
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "listening" event.
|
||||
*/
|
||||
|
||||
function onListening() {
|
||||
var addr = server.address();
|
||||
var bind = typeof addr === 'string'
|
||||
? 'pipe ' + addr
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const { dns } = require('concordant')()
|
||||
const redis = require('redis')
|
||||
|
||||
module.exports = eventLogger
|
||||
|
||||
function eventLogger () {
|
||||
const QNAME = 'eventservice'
|
||||
var client
|
||||
|
||||
const endpoint = '_main._tcp.redis.micro.svc.cluster.local'
|
||||
dns.resolve(endpoint, (err, locs) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
const { port, host } = locs[0]
|
||||
client = redis.createClient(port, host)
|
||||
})
|
||||
|
||||
function middleware (req, res, next) {
|
||||
if (!client) {
|
||||
console.log('client not ready, waiting 100ms')
|
||||
setTimeout(middleware, 100, req, res, next)
|
||||
return
|
||||
}
|
||||
const event = {
|
||||
action: 'record',
|
||||
type: 'page',
|
||||
url: `${req.protocol}://${req.get('host')}${req.originalUrl}`
|
||||
}
|
||||
client.lpush(QNAME, JSON.stringify(event), (err) => {
|
||||
if (err) console.error(err)
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
return middleware
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "webapp",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "~1.16.0",
|
||||
"concordant": "^0.2.1",
|
||||
"cookie-parser": "~1.4.3",
|
||||
"debug": "~2.6.0",
|
||||
"ejs": "~2.5.5",
|
||||
"express": "~4.14.1",
|
||||
"morgan": "~1.7.0",
|
||||
"mu": "^2.1.2",
|
||||
"restify": "^4.3.0",
|
||||
"serve-favicon": "~2.3.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #00B7FF;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
'use strict'
|
||||
|
||||
const { Router } = require('express')
|
||||
const restify = require('restify')
|
||||
const { dns } = require('concordant')()
|
||||
const router = Router()
|
||||
var clients
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
res.render('add', { first: 0, second: 0, result: 0 })
|
||||
})
|
||||
|
||||
router.post('/calculate', resolve, respond)
|
||||
|
||||
function resolve (req, res, next) {
|
||||
if (clients) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
const adderservice = `_main._tcp.adderservice.micro.svc.cluster.local`
|
||||
const auditservice = `_main._tcp.auditservice.micro.svc.cluster.local`
|
||||
dns.resolve(adderservice, (err, locs) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const adder = `${host}:${port}`
|
||||
dns.resolve(auditservice, (err, locs) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const audit = `${host}:${port}`
|
||||
clients = {
|
||||
adder: restify.createJSONClient({url: `http://${adder}`}),
|
||||
audit: restify.createJSONClient({url: `http://${audit}`})
|
||||
}
|
||||
next()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function respond (req, res, next) {
|
||||
const { first, second } = req.body
|
||||
clients.adder.get(
|
||||
`/add/${first}/${second}`,
|
||||
(err, svcReq, svcRes, data) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
|
||||
const { result } = data
|
||||
clients.audit.post('/append', {
|
||||
calc: first + '+' + second,
|
||||
calcResult: result
|
||||
}, (err) => {
|
||||
if (err) console.error(err)
|
||||
})
|
||||
|
||||
res.render('add', { first, second, result })
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
const { Router } = require('express')
|
||||
const restify = require('restify')
|
||||
const { dns } = require('concordant')()
|
||||
const router = Router()
|
||||
var client
|
||||
|
||||
router.get('/', resolve, respond)
|
||||
|
||||
function resolve (req, res, next) {
|
||||
if (client) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
const auditservice = `_main._tcp.auditservice.micro.svc.cluster.local`
|
||||
dns.resolve(auditservice, (err, locs) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
client = restify.createJSONClient(`http://${host}:${port}`)
|
||||
})
|
||||
}
|
||||
|
||||
function respond (req, res, next) {
|
||||
client.get('/list', (err, svcReq, svcRes, data) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
res.render('audit', data)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.render('index', { title: 'Express' })
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET users listing. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.send('respond with a resource')
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Add</title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Add it up!</h1>
|
||||
<form id='calc-form' action='/add/calculate' method='post'>
|
||||
<input type='text' id='first', name='first' value=<%= first %>></input>
|
||||
<input type='text' id='second', name='second' value=<%= second %>></input>
|
||||
</form>
|
||||
<button type="submit" form="calc-form" value="Submit">Submit</button>
|
||||
<h2>result = <%= result %></h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Audit</title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Calculation History</h1>
|
||||
<ul>
|
||||
<% list.forEach(function (el) { %>
|
||||
<li>at: <%= new Date(el.ts).toLocaleString() %>, calculated: <%= el.calc %>, result: <%= el.result %></li>
|
||||
<% }) %>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,3 @@
|
||||
<h1><%= message %></h1>
|
||||
<h2><%= error.status %></h2>
|
||||
<pre><%= error.stack %></pre>
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%= title %></title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= title %></h1>
|
||||
<p>Welcome to <%= title %></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "adderservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
|
||||
function respond (req, res, next) {
|
||||
const result = (parseInt(req.params.first, 10) +
|
||||
parseInt(req.params.second, 10)).toString()
|
||||
res.send(result)
|
||||
next()
|
||||
}
|
||||
|
||||
const server = restify.createServer()
|
||||
server.get('/add/:first/:second', respond)
|
||||
|
||||
server.listen(8080, () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
const request = require('superagent')
|
||||
const { test } = require('tap')
|
||||
|
||||
test('add test', (t) => {
|
||||
t.plan(2)
|
||||
|
||||
request
|
||||
.post('http://localhost:3000/add/calculate')
|
||||
.send('first=1')
|
||||
.send('second=2')
|
||||
.end((err, res) => {
|
||||
t.equal(err, null)
|
||||
t.ok(/result = 3/ig.test(res.text))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "inttest",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"superagent": "^3.5.2",
|
||||
"tap": "^10.3.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
var express = require('express')
|
||||
var path = require('path')
|
||||
var favicon = require('serve-favicon')
|
||||
var logger = require('morgan')
|
||||
var cookieParser = require('cookie-parser')
|
||||
var bodyParser = require('body-parser')
|
||||
|
||||
var index = require('./routes/index')
|
||||
var users = require('./routes/users')
|
||||
var add = require('./routes/add')
|
||||
|
||||
var app = express()
|
||||
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
// uncomment after placing your favicon in /public
|
||||
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
|
||||
app.use(logger('dev'))
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
app.use(cookieParser())
|
||||
app.use(express.static(path.join(__dirname, 'public')))
|
||||
|
||||
app.use('/', index)
|
||||
app.use('/users', users)
|
||||
app.use('/add', add)
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
var err = new Error('Not Found')
|
||||
err.status = 404
|
||||
next(err)
|
||||
})
|
||||
|
||||
// error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {}
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500)
|
||||
res.render('error')
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var app = require('../app');
|
||||
var debug = require('debug')('webapp:server');
|
||||
var http = require('http');
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
|
||||
var port = normalizePort(process.env.PORT || '3000');
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
* Create HTTP server.
|
||||
*/
|
||||
|
||||
var server = http.createServer(app);
|
||||
|
||||
/**
|
||||
* Listen on provided port, on all network interfaces.
|
||||
*/
|
||||
|
||||
server.listen(port);
|
||||
server.on('error', onError);
|
||||
server.on('listening', onListening);
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
*/
|
||||
|
||||
function normalizePort(val) {
|
||||
var port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
return val;
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
// port number
|
||||
return port;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "error" event.
|
||||
*/
|
||||
|
||||
function onError(error) {
|
||||
if (error.syscall !== 'listen') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
var bind = typeof port === 'string'
|
||||
? 'Pipe ' + port
|
||||
: 'Port ' + port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
console.error(bind + ' requires elevated privileges');
|
||||
process.exit(1);
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
console.error(bind + ' is already in use');
|
||||
process.exit(1);
|
||||
break;
|
||||
default:
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "listening" event.
|
||||
*/
|
||||
|
||||
function onListening() {
|
||||
var addr = server.address();
|
||||
var bind = typeof addr === 'string'
|
||||
? 'pipe ' + addr
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "webapp",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "~1.16.0",
|
||||
"cookie-parser": "~1.4.3",
|
||||
"debug": "~2.6.0",
|
||||
"ejs": "~2.5.5",
|
||||
"express": "~4.14.1",
|
||||
"morgan": "~1.7.0",
|
||||
"restify": "^4.3.0",
|
||||
"serve-favicon": "~2.3.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #00B7FF;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
const { Router } = require('express')
|
||||
const restify = require('restify')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
res.render('add', { first: 0, second: 0, result: 0 })
|
||||
})
|
||||
|
||||
router.post('/calculate', function (req, res, next) {
|
||||
const client = restify.createStringClient({
|
||||
url: 'http://localhost:8080'
|
||||
})
|
||||
const {first, second} = req.body
|
||||
client.get(
|
||||
`/add/${first}/${second}`,
|
||||
(err, svcReq, svcRes, result) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
res.render('add', { first, second, result })
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.render('index', { title: 'Express' })
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET users listing. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.send('respond with a resource')
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Add</title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Add it up!</h1>
|
||||
<form id='calc-form' action='/add/calculate' method='post'>
|
||||
<input type='text' id='first', name='first' value=<%= first %>></input>
|
||||
<input type='text' id='second', name='second' value=<%= second %>></input>
|
||||
</form>
|
||||
<button type="submit" form="calc-form" value="Submit">Submit</button>
|
||||
<h2>result = <%= result %></h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,3 @@
|
||||
<h1><%= message %></h1>
|
||||
<h2><%= error.status %></h2>
|
||||
<pre><%= error.stack %></pre>
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%= title %></title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= title %></h1>
|
||||
<p>Welcome to <%= title %></p>
|
||||
</body>
|
||||
</html>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "adderservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const http = require('http')
|
||||
|
||||
const server = http.createServer(respond)
|
||||
|
||||
server.listen(8080, function () {
|
||||
console.log('listening on port 8080')
|
||||
})
|
||||
|
||||
function respond (req, res) {
|
||||
const [cmd, first, second] = req.url.split('/').slice(1)
|
||||
const notFound = cmd !== 'add' ||
|
||||
first === undefined ||
|
||||
second === undefined
|
||||
|
||||
if (notFound) {
|
||||
error(404, res)
|
||||
return
|
||||
}
|
||||
|
||||
const result = parseInt(first, 10) + parseInt(second, 10)
|
||||
res.end(result)
|
||||
}
|
||||
|
||||
function error(code, res) {
|
||||
res.statusCode = code
|
||||
res.end(http.STATUS_CODES[code])
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
curl http://localhost:8080/add/1/2
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "adderservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
|
||||
function respond (req, res, next) {
|
||||
const result = (parseInt(req.params.first, 10) +
|
||||
parseInt(req.params.second, 10)).toString()
|
||||
res.send(result)
|
||||
next()
|
||||
}
|
||||
|
||||
const server = restify.createServer()
|
||||
server.get('/add/:first/:second', respond)
|
||||
|
||||
server.listen(8080, () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
curl http://localhost:8080/add/1/2
|
||||
@@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const wiring = require('./wiring')
|
||||
const service = require('./service')()
|
||||
|
||||
|
||||
wiring(service)
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "adderservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "tap test"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"restify": "^4.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tap": "^10.3.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = service
|
||||
|
||||
function service () {
|
||||
function add (args, cb) {
|
||||
const {first, second} = args
|
||||
const result = (parseInt(first, 10) + parseInt(second, 10))
|
||||
cb(null, {result: result.toString()})
|
||||
}
|
||||
|
||||
return { add }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const {test} = require('tap')
|
||||
const service = require('../service')()
|
||||
|
||||
test('test add', (t) => {
|
||||
t.plan(2)
|
||||
|
||||
service.add({first: 1, second: 2}, (err, answer) => {
|
||||
t.error(err)
|
||||
t.same(answer, {result: 3})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
const { ADDERSERVICE_SERVICE_PORT } = process.env
|
||||
|
||||
module.exports = wiring
|
||||
|
||||
function wiring (service) {
|
||||
const server = restify.createServer()
|
||||
|
||||
server.get('/add/:first/:second', (req, res, next) => {
|
||||
service.add(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
next()
|
||||
return
|
||||
}
|
||||
res.send(200, result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.listen(ADDERSERVICE_SERVICE_PORT, '0.0.0.0', () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const wiring = require('./wiring')
|
||||
const service = require('./service')()
|
||||
|
||||
wiring(service)
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "auditservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"concordant": "^0.2.1",
|
||||
"mongo": "^0.1.0",
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
'use strict'
|
||||
|
||||
const { MongoClient } = require('mongodb')
|
||||
const { dns } = require('concordant')()
|
||||
|
||||
module.exports = service
|
||||
|
||||
function service () {
|
||||
|
||||
var db
|
||||
|
||||
setup()
|
||||
|
||||
function setup () {
|
||||
const mongo = '_main._tcp.mongo.micro.svc.cluster.local'
|
||||
|
||||
dns.resolve(mongo, (err, locs) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const url = `mongodb://${host}:${port}/audit`
|
||||
MongoClient.connect(url, (err, client) => {
|
||||
if (err) {
|
||||
console.log('failed to connect to MongoDB retrying in 100ms')
|
||||
setTimeout(setup, 100)
|
||||
return
|
||||
}
|
||||
db = client
|
||||
db.on('close', () => db = null)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function append (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const audit = db.collection('audit')
|
||||
const data = {
|
||||
ts: Date.now(),
|
||||
calc: args.calc,
|
||||
result: args.calcResult
|
||||
}
|
||||
|
||||
audit.insert(data, (err, result) => {
|
||||
if (err) {
|
||||
cb(err)
|
||||
return
|
||||
}
|
||||
cb(null, {result: result.toString()})
|
||||
})
|
||||
}
|
||||
|
||||
function list (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const audit = db.collection('audit')
|
||||
audit.find({}, {limit: 10}).toArray((err, docs) => {
|
||||
if (err) {
|
||||
cb(err)
|
||||
return
|
||||
}
|
||||
cb(null, {list: docs})
|
||||
})
|
||||
}
|
||||
|
||||
return { append, list }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
const { AUDITSERVICE_SERVICE_PORT } = process.env
|
||||
|
||||
module.exports = wiring
|
||||
|
||||
function wiring (service) {
|
||||
const server = restify.createServer()
|
||||
|
||||
server.use(restify.bodyParser())
|
||||
|
||||
server.post('/append', (req, res, next) => {
|
||||
service.append(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
res.send(result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.get('/list', (req, res, next) => {
|
||||
service.list(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
res.send(200, result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.listen(AUDITSERVICE_SERVICE_PORT, '0.0.0.0', () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
fuge_global:
|
||||
dns_enabled: true
|
||||
dns_host: 127.0.0.1
|
||||
dns_port: 53053
|
||||
dns_suffix: svc.cluster.local
|
||||
dns_namespace: micro
|
||||
tail: true
|
||||
monitor: true
|
||||
monitor_excludes:
|
||||
- '**/node_modules/**'
|
||||
- '**/.git/**'
|
||||
- '*.log'
|
||||
adderservice:
|
||||
type: process
|
||||
path: ../adderservice
|
||||
run: node index.js
|
||||
ports:
|
||||
- main=8080
|
||||
webapp:
|
||||
type: process
|
||||
path: ../webapp
|
||||
run: npm start
|
||||
ports:
|
||||
- main=3000
|
||||
auditservice:
|
||||
type: process
|
||||
path: ../auditservice
|
||||
run: 'node index.js'
|
||||
ports:
|
||||
- main=8081
|
||||
mongo:
|
||||
image: mongo
|
||||
type: container
|
||||
ports:
|
||||
- main=27017:27017
|
||||
@@ -0,0 +1,50 @@
|
||||
var express = require('express')
|
||||
var path = require('path')
|
||||
var favicon = require('serve-favicon')
|
||||
var logger = require('morgan')
|
||||
var cookieParser = require('cookie-parser')
|
||||
var bodyParser = require('body-parser')
|
||||
|
||||
var index = require('./routes/index')
|
||||
var users = require('./routes/users')
|
||||
var add = require('./routes/add')
|
||||
var audit = require('./routes/audit')
|
||||
|
||||
var app = express()
|
||||
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
// uncomment after placing your favicon in /public
|
||||
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
|
||||
app.use(logger('dev'))
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
app.use(cookieParser())
|
||||
app.use(express.static(path.join(__dirname, 'public')))
|
||||
|
||||
app.use('/', index)
|
||||
app.use('/users', users)
|
||||
app.use('/add', add)
|
||||
app.use('/audit', audit)
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
var err = new Error('Not Found')
|
||||
err.status = 404
|
||||
next(err)
|
||||
})
|
||||
|
||||
// error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {}
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500)
|
||||
res.render('error')
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var app = require('../app');
|
||||
var debug = require('debug')('webapp:server');
|
||||
var http = require('http');
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
|
||||
var port = normalizePort(process.env.PORT || '3000');
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
* Create HTTP server.
|
||||
*/
|
||||
|
||||
var server = http.createServer(app);
|
||||
|
||||
/**
|
||||
* Listen on provided port, on all network interfaces.
|
||||
*/
|
||||
|
||||
server.listen(port);
|
||||
server.on('error', onError);
|
||||
server.on('listening', onListening);
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
*/
|
||||
|
||||
function normalizePort(val) {
|
||||
var port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
return val;
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
// port number
|
||||
return port;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "error" event.
|
||||
*/
|
||||
|
||||
function onError(error) {
|
||||
if (error.syscall !== 'listen') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
var bind = typeof port === 'string'
|
||||
? 'Pipe ' + port
|
||||
: 'Port ' + port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
console.error(bind + ' requires elevated privileges');
|
||||
process.exit(1);
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
console.error(bind + ' is already in use');
|
||||
process.exit(1);
|
||||
break;
|
||||
default:
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "listening" event.
|
||||
*/
|
||||
|
||||
function onListening() {
|
||||
var addr = server.address();
|
||||
var bind = typeof addr === 'string'
|
||||
? 'pipe ' + addr
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "webapp",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "~1.16.0",
|
||||
"concordant": "^0.2.1",
|
||||
"cookie-parser": "~1.4.3",
|
||||
"debug": "~2.6.0",
|
||||
"ejs": "~2.5.5",
|
||||
"express": "~4.14.1",
|
||||
"morgan": "~1.7.0",
|
||||
"mu": "^2.1.2",
|
||||
"restify": "^4.3.0",
|
||||
"serve-favicon": "~2.3.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #00B7FF;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
'use strict'
|
||||
|
||||
const { Router } = require('express')
|
||||
const restify = require('restify')
|
||||
const { dns } = require('concordant')()
|
||||
const router = Router()
|
||||
var clients
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
res.render('add', { first: 0, second: 0, result: 0 })
|
||||
})
|
||||
|
||||
router.post('/calculate', resolve, respond)
|
||||
|
||||
function resolve (req, res, next) {
|
||||
if (clients) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
const adderservice = `_main._tcp.adderservice.micro.svc.cluster.local`
|
||||
const auditservice = `_main._tcp.auditservice.micro.svc.cluster.local`
|
||||
dns.resolve(adderservice, (err, locs) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const adder = `${host}:${port}`
|
||||
dns.resolve(auditservice, (err, locs) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const audit = `${host}:${port}`
|
||||
clients = {
|
||||
adder: restify.createJSONClient({url: `http://${adder}`}),
|
||||
audit: restify.createJSONClient({url: `http://${audit}`})
|
||||
}
|
||||
next()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function respond (req, res, next) {
|
||||
const { first, second } = req.body
|
||||
clients.adder.get(
|
||||
`/add/${first}/${second}`,
|
||||
(err, svcReq, svcRes, data) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
|
||||
const { result } = data
|
||||
clients.audit.post('/append', {
|
||||
calc: first + '+' + second,
|
||||
calcResult: result
|
||||
}, (err) => {
|
||||
if (err) console.error(err)
|
||||
})
|
||||
|
||||
res.render('add', { first, second, result })
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
const { Router } = require('express')
|
||||
const restify = require('restify')
|
||||
const { dns } = require('concordant')()
|
||||
const router = Router()
|
||||
var client
|
||||
|
||||
router.get('/', resolve, respond)
|
||||
|
||||
function resolve (req, res, next) {
|
||||
if (client) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
const auditservice = `_main._tcp.auditservice.micro.svc.cluster.local`
|
||||
dns.resolve(auditservice, (err, locs) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
client = restify.createJSONClient(`http://${host}:${port}`)
|
||||
})
|
||||
}
|
||||
|
||||
function respond (req, res, next) {
|
||||
client.get('/list', (err, svcReq, svcRes, data) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
res.render('audit', data)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.render('index', { title: 'Express' })
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET users listing. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.send('respond with a resource')
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Add</title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Add it up!</h1>
|
||||
<form id='calc-form' action='/add/calculate' method='post'>
|
||||
<input type='text' id='first', name='first' value=<%= first %>></input>
|
||||
<input type='text' id='second', name='second' value=<%= second %>></input>
|
||||
</form>
|
||||
<button type="submit" form="calc-form" value="Submit">Submit</button>
|
||||
<h2>result = <%= result %></h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Audit</title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Calculation History</h1>
|
||||
<ul>
|
||||
<% list.forEach(function (el) { %>
|
||||
<li>at: <%= new Date(el.ts).toLocaleString() %>, calculated: <%= el.calc %>, result: <%= el.result %></li>
|
||||
<% }) %>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,3 @@
|
||||
<h1><%= message %></h1>
|
||||
<h2><%= error.status %></h2>
|
||||
<pre><%= error.stack %></pre>
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%= title %></title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= title %></h1>
|
||||
<p>Welcome to <%= title %></p>
|
||||
</body>
|
||||
</html>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "adderservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
|
||||
function respond (req, res, next) {
|
||||
const result = (parseInt(req.params.first, 10) +
|
||||
parseInt(req.params.second, 10)).toString()
|
||||
console.log('adding numbers!')
|
||||
res.send(result)
|
||||
next()
|
||||
}
|
||||
|
||||
const server = restify.createServer()
|
||||
server.get('/add/:first/:second', respond)
|
||||
|
||||
server.listen(8080, () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
cd webapp && npm start
|
||||
cd adderservice && node service
|
||||
@@ -0,0 +1,48 @@
|
||||
var express = require('express')
|
||||
var path = require('path')
|
||||
var favicon = require('serve-favicon')
|
||||
var logger = require('morgan')
|
||||
var cookieParser = require('cookie-parser')
|
||||
var bodyParser = require('body-parser')
|
||||
|
||||
var index = require('./routes/index')
|
||||
var users = require('./routes/users')
|
||||
var add = require('./routes/add')
|
||||
|
||||
var app = express()
|
||||
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
// uncomment after placing your favicon in /public
|
||||
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
|
||||
app.use(logger('dev'))
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
app.use(cookieParser())
|
||||
app.use(express.static(path.join(__dirname, 'public')))
|
||||
|
||||
app.use('/', index)
|
||||
app.use('/users', users)
|
||||
app.use('/add', add)
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
var err = new Error('Not Found')
|
||||
err.status = 404
|
||||
next(err)
|
||||
})
|
||||
|
||||
// error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {}
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500)
|
||||
res.render('error')
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var app = require('../app');
|
||||
var debug = require('debug')('webapp:server');
|
||||
var http = require('http');
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
|
||||
var port = normalizePort(process.env.PORT || '3000');
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
* Create HTTP server.
|
||||
*/
|
||||
|
||||
var server = http.createServer(app);
|
||||
|
||||
/**
|
||||
* Listen on provided port, on all network interfaces.
|
||||
*/
|
||||
|
||||
server.listen(port);
|
||||
server.on('error', onError);
|
||||
server.on('listening', onListening);
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
*/
|
||||
|
||||
function normalizePort(val) {
|
||||
var port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
return val;
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
// port number
|
||||
return port;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "error" event.
|
||||
*/
|
||||
|
||||
function onError(error) {
|
||||
if (error.syscall !== 'listen') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
var bind = typeof port === 'string'
|
||||
? 'Pipe ' + port
|
||||
: 'Port ' + port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
console.error(bind + ' requires elevated privileges');
|
||||
process.exit(1);
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
console.error(bind + ' is already in use');
|
||||
process.exit(1);
|
||||
break;
|
||||
default:
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "listening" event.
|
||||
*/
|
||||
|
||||
function onListening() {
|
||||
var addr = server.address();
|
||||
var bind = typeof addr === 'string'
|
||||
? 'pipe ' + addr
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "webapp",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "~1.16.0",
|
||||
"cookie-parser": "~1.4.3",
|
||||
"debug": "~2.6.0",
|
||||
"ejs": "~2.5.5",
|
||||
"express": "~4.14.1",
|
||||
"morgan": "~1.7.0",
|
||||
"restify": "^4.3.0",
|
||||
"serve-favicon": "~2.3.2"
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #00B7FF;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
const { Router } = require('express')
|
||||
const restify = require('restify')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
res.render('add', { first: 0, second: 0, result: 0 })
|
||||
})
|
||||
|
||||
router.post('/calculate', function (req, res, next) {
|
||||
const client = restify.createStringClient({
|
||||
url: 'http://localhost:8080'
|
||||
})
|
||||
const {first, second} = req.body
|
||||
client.get(
|
||||
`/add/${first}/${second}`,
|
||||
(err, svcReq, svcRes, result) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
res.render('add', { first, second, result })
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.render('index', { title: 'Express' })
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET users listing. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.send('respond with a resource')
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Add</title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Add it up!</h1>
|
||||
<form id='calc-form' action='/add/calculate' method='post'>
|
||||
<input type='text' id='first', name='first' value=<%= first %>></input>
|
||||
<input type='text' id='second', name='second' value=<%= second %>></input>
|
||||
</form>
|
||||
<button type="submit" form="calc-form" value="Submit">Submit</button>
|
||||
<h2>result = <%= result %></h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,3 @@
|
||||
<h1><%= message %></h1>
|
||||
<h2><%= error.status %></h2>
|
||||
<pre><%= error.stack %></pre>
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%= title %></title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= title %></h1>
|
||||
<p>Welcome to <%= title %></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "adderservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
|
||||
function respond (req, res, next) {
|
||||
const result = (parseInt(req.params.first, 10) +
|
||||
parseInt(req.params.second, 10)).toString()
|
||||
console.log('adding numbers!')
|
||||
res.send(result)
|
||||
next()
|
||||
}
|
||||
|
||||
const server = restify.createServer()
|
||||
server.get('/add/:first/:second', respond)
|
||||
|
||||
server.listen(8080, () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
fuge_global:
|
||||
tail: true
|
||||
monitor: true
|
||||
monitor_excludes:
|
||||
- '**/node_modules/**'
|
||||
- '**/.git/**'
|
||||
- '*.log'
|
||||
adderservice:
|
||||
type: process
|
||||
path: ../adderservice
|
||||
run: 'node service.js'
|
||||
ports:
|
||||
- main=8080
|
||||
webapp:
|
||||
type: process
|
||||
path: ../webapp
|
||||
run: 'npm start'
|
||||
ports:
|
||||
- main=3000
|
||||
@@ -0,0 +1,19 @@
|
||||
fuge_global:
|
||||
tail: true
|
||||
monitor: true
|
||||
monitor_excludes:
|
||||
- '**/node_modules/**'
|
||||
- '**/.git/**'
|
||||
- '*.log'
|
||||
adderservice:
|
||||
type: node
|
||||
path: ../adderservice
|
||||
run: 'node service.js'
|
||||
ports:
|
||||
- main=8080
|
||||
webapp:
|
||||
type: process
|
||||
path: ../webapp
|
||||
run: 'npm start'
|
||||
ports:
|
||||
- main=3000
|
||||
@@ -0,0 +1,48 @@
|
||||
var express = require('express')
|
||||
var path = require('path')
|
||||
var favicon = require('serve-favicon')
|
||||
var logger = require('morgan')
|
||||
var cookieParser = require('cookie-parser')
|
||||
var bodyParser = require('body-parser')
|
||||
|
||||
var index = require('./routes/index')
|
||||
var users = require('./routes/users')
|
||||
var add = require('./routes/add')
|
||||
|
||||
var app = express()
|
||||
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
// uncomment after placing your favicon in /public
|
||||
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
|
||||
app.use(logger('dev'))
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
app.use(cookieParser())
|
||||
app.use(express.static(path.join(__dirname, 'public')))
|
||||
|
||||
app.use('/', index)
|
||||
app.use('/users', users)
|
||||
app.use('/add', add)
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
var err = new Error('Not Found')
|
||||
err.status = 404
|
||||
next(err)
|
||||
})
|
||||
|
||||
// error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {}
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500)
|
||||
res.render('error')
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var app = require('../app');
|
||||
var debug = require('debug')('webapp:server');
|
||||
var http = require('http');
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
|
||||
var port = normalizePort(process.env.PORT || '3000');
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
* Create HTTP server.
|
||||
*/
|
||||
|
||||
var server = http.createServer(app);
|
||||
|
||||
/**
|
||||
* Listen on provided port, on all network interfaces.
|
||||
*/
|
||||
|
||||
server.listen(port);
|
||||
server.on('error', onError);
|
||||
server.on('listening', onListening);
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
*/
|
||||
|
||||
function normalizePort(val) {
|
||||
var port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
return val;
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
// port number
|
||||
return port;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "error" event.
|
||||
*/
|
||||
|
||||
function onError(error) {
|
||||
if (error.syscall !== 'listen') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
var bind = typeof port === 'string'
|
||||
? 'Pipe ' + port
|
||||
: 'Port ' + port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
console.error(bind + ' requires elevated privileges');
|
||||
process.exit(1);
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
console.error(bind + ' is already in use');
|
||||
process.exit(1);
|
||||
break;
|
||||
default:
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "listening" event.
|
||||
*/
|
||||
|
||||
function onListening() {
|
||||
var addr = server.address();
|
||||
var bind = typeof addr === 'string'
|
||||
? 'pipe ' + addr
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "webapp",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "~1.16.0",
|
||||
"cookie-parser": "~1.4.3",
|
||||
"debug": "~2.6.0",
|
||||
"ejs": "~2.5.5",
|
||||
"express": "~4.14.1",
|
||||
"morgan": "~1.7.0",
|
||||
"restify": "^4.3.0",
|
||||
"serve-favicon": "~2.3.2"
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #00B7FF;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
const { Router } = require('express')
|
||||
const restify = require('restify')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
res.render('add', { first: 0, second: 0, result: 0 })
|
||||
})
|
||||
|
||||
router.post('/calculate', function (req, res, next) {
|
||||
const client = restify.createStringClient({
|
||||
url: 'http://localhost:8080'
|
||||
})
|
||||
const {first, second} = req.body
|
||||
client.get(
|
||||
`/add/${first}/${second}`,
|
||||
(err, svcReq, svcRes, result) => {
|
||||
if (err) {
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
res.render('add', { first, second, result })
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.render('index', { title: 'Express' })
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,9 @@
|
||||
var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
/* GET users listing. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.send('respond with a resource')
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Add</title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Add it up!</h1>
|
||||
<form id='calc-form' action='/add/calculate' method='post'>
|
||||
<input type='text' id='first', name='first' value=<%= first %>></input>
|
||||
<input type='text' id='second', name='second' value=<%= second %>></input>
|
||||
</form>
|
||||
<button type="submit" form="calc-form" value="Submit">Submit</button>
|
||||
<h2>result = <%= result %></h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,3 @@
|
||||
<h1><%= message %></h1>
|
||||
<h2><%= error.status %></h2>
|
||||
<pre><%= error.stack %></pre>
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%= title %></title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= title %></h1>
|
||||
<p>Welcome to <%= title %></p>
|
||||
</body>
|
||||
</html>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const wiring = require('./wiring')
|
||||
const service = require('./service')()
|
||||
|
||||
|
||||
wiring(service)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user