Code files

This commit is contained in:
Akhil
2017-07-31 11:33:31 +05:30
parent 073a63ebdd
commit b9ae409ec0
801 changed files with 17535 additions and 0 deletions
@@ -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>