Skip to content

Commit 87d9e8e

Browse files
committed
Initial commit
0 parents  commit 87d9e8e

File tree

18 files changed

+7797
-0
lines changed

18 files changed

+7797
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Example Voting App
2+
==================
3+
4+
This is an example Docker app with multiple services. It is run with Docker Compose and uses Docker Networking to connect containers together.
5+
6+
7+
Running
8+
-------
9+
10+
Since this app makes use of Compose's experimental networking support, it must be started with:
11+
12+
$ cd vote-apps/
13+
$ docker-compose --x-networking up -d
14+
15+
The app will be running on port 5000 on your Docker host, and the results will be on port 5001.

vote-apps/docker-compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
voting-app:
2+
build: ./voting-app/.
3+
volumes:
4+
- ./voting-app:/app
5+
ports:
6+
- "5000:80"
7+
8+
redis:
9+
image: redis
10+
ports: ["6379"]
11+
12+
worker:
13+
build: ./worker
14+
15+
db:
16+
image: postgres:9.4
17+
volumes:
18+
- "myvolume:/var/lib/postgresql/data"
19+
20+
result-app:
21+
build: ./result-app/.
22+
volumes:
23+
- ./result-app:/app
24+
ports:
25+
- "5001:80"

vote-apps/result-app/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM node:0.10
2+
3+
RUN mkdir /app
4+
WORKDIR /app
5+
6+
ADD package.json /app/package.json
7+
RUN npm install && npm ls
8+
RUN mv /app/node_modules /node_modules
9+
10+
ADD . /app
11+
12+
ENV PORT 80
13+
EXPOSE 80
14+
15+
CMD ["node", "server.js"]

vote-apps/result-app/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "result-app",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "MIT",
11+
"dependencies": {
12+
"body-parser": "^1.14.1",
13+
"cookie-parser": "^1.4.0",
14+
"express": "^4.13.3",
15+
"method-override": "^2.3.5",
16+
"async": "^1.5.0",
17+
"pg": "^4.4.3",
18+
"socket.io": "^1.3.7"
19+
}
20+
}

vote-apps/result-app/server.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var express = require('express'),
2+
async = require('async'),
3+
pg = require("pg"),
4+
cookieParser = require('cookie-parser'),
5+
bodyParser = require('body-parser'),
6+
methodOverride = require('method-override'),
7+
app = express(),
8+
server = require('http').Server(app),
9+
io = require('socket.io')(server);
10+
11+
io.set('transports', ['polling']);
12+
13+
var port = process.env.PORT || 4000;
14+
15+
io.sockets.on('connection', function (socket) {
16+
17+
socket.emit('message', { text : 'Welcome!' });
18+
19+
socket.on('subscribe', function (data) {
20+
socket.join(data.channel);
21+
});
22+
});
23+
24+
async.retry(
25+
{times: 1000, interval: 1000},
26+
function(callback) {
27+
pg.connect('postgres://postgres@voteapps_db_1/postgres', function(err, client, done) {
28+
if (err) {
29+
console.error("Failed to connect to db");
30+
}
31+
callback(err, client);
32+
});
33+
},
34+
function(err, client) {
35+
if (err) {
36+
return console.err("Giving up");
37+
}
38+
console.log("Connected to db");
39+
getVotes(client);
40+
}
41+
);
42+
43+
function getVotes(client) {
44+
client.query('SELECT vote, COUNT(id) AS count FROM votes GROUP BY vote', [], function(err, result) {
45+
if (err) {
46+
console.error("Error performing query: " + err);
47+
} else {
48+
var data = result.rows.reduce(function(obj, row) {
49+
obj[row.vote] = row.count;
50+
return obj;
51+
}, {});
52+
io.sockets.emit("scores", JSON.stringify(data));
53+
}
54+
55+
setTimeout(function() {getVotes(client) }, 1000);
56+
});
57+
}
58+
59+
app.use(cookieParser());
60+
app.use(bodyParser());
61+
app.use(methodOverride('X-HTTP-Method-Override'));
62+
app.use(function(req, res, next) {
63+
res.header("Access-Control-Allow-Origin", "*");
64+
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
65+
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
66+
next();
67+
});
68+
69+
app.use(express.static(__dirname + '/views'));
70+
71+
app.get('/', function (req, res) {
72+
res.sendFile(path.resolve(__dirname + '/views/index.html'));
73+
});
74+
75+
server.listen(port, function () {
76+
var port = server.address().port;
77+
console.log('App running on port ' + port);
78+
});

vote-apps/result-app/views/app.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var app = angular.module('catsvsdogs', []);
2+
var socket = io.connect({transports:['polling']});
3+
4+
var bg1 = document.getElementById('background-stats-1');
5+
var bg2 = document.getElementById('background-stats-2');
6+
7+
app.controller('statsCtrl', function($scope){
8+
var animateStats = function(a,b){
9+
if(a+b>0){
10+
var percentA = a/(a+b)*100;
11+
var percentB = 100-percentA;
12+
bg1.style.width= percentA+"%";
13+
bg2.style.width = percentB+"%";
14+
}
15+
};
16+
17+
$scope.aPercent = 50;
18+
$scope.bPercent = 50;
19+
20+
var updateScores = function(){
21+
socket.on('scores', function (json) {
22+
data = JSON.parse(json);
23+
var a = parseInt(data.a || 0);
24+
var b = parseInt(data.b || 0);
25+
26+
animateStats(a, b);
27+
28+
$scope.$apply(function() {
29+
if(a + b > 0){
30+
$scope.aPercent = a/(a+b) * 100;
31+
$scope.bPercent = b/(a+b) * 100;
32+
$scope.total = a + b
33+
}
34+
});
35+
});
36+
};
37+
38+
var init = function(){
39+
document.body.style.opacity=1;
40+
updateScores();
41+
};
42+
socket.on('message',function(data){
43+
init();
44+
});
45+
});

vote-apps/result-app/views/index.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html ng-app="catsvsdogs">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Cats vs Dogs -- Result</title>
6+
<base href="/index.html">
7+
<meta name = "viewport" content = "width=device-width, initial-scale = 1.0">
8+
<meta name="keywords" content="docker-compose, docker, stack">
9+
<meta name="author" content="Tutum dev team">
10+
<link rel='stylesheet' href='/stylesheets/style.css' />
11+
</head>
12+
<body ng-controller="statsCtrl" >
13+
<div id="background-stats">
14+
<div id="background-stats-1">
15+
</div><!--
16+
--><div id="background-stats-2">
17+
</div>
18+
</div>
19+
<div id="content-container">
20+
<div id="content-container-center">
21+
<div id="choice">
22+
<div class="choice cats">
23+
<div class="label">Cats</div>
24+
<div class="stat">{{aPercent | number:1}}%</div>
25+
</div>
26+
<div class="divider"></div>
27+
<div class="choice dogs">
28+
<div class="label">Dogs</div>
29+
<div class="stat">{{bPercent | number:1}}%</div>
30+
</div>
31+
</div>
32+
</div>
33+
</div>
34+
<div id="result" ng-if="total > 100">
35+
{{total}} votes
36+
</div>
37+
<script src="socket.io.js"></script>
38+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
39+
<script src="app.js"></script>
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)