-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Peng Xiao <[email protected]>
- Loading branch information
1 parent
7417c5b
commit 80696ea
Showing
42 changed files
with
97,083 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- mode: ruby -*- | ||
# vi: set ft=ruby : | ||
|
||
Vagrant.require_version ">= 1.6.0" | ||
|
||
boxes = [ | ||
{ | ||
:name => "docker-host", | ||
:eth1 => "192.168.205.10", | ||
:mem => "1024", | ||
:cpu => "1" | ||
} | ||
] | ||
|
||
Vagrant.configure(2) do |config| | ||
|
||
config.vm.box = "centos/7" | ||
boxes.each do |opts| | ||
config.vm.define opts[:name] do |config| | ||
config.vm.hostname = opts[:name] | ||
config.vm.provider "vmware_fusion" do |v| | ||
v.vmx["memsize"] = opts[:mem] | ||
v.vmx["numvcpus"] = opts[:cpu] | ||
end | ||
config.vm.provider "virtualbox" do |v| | ||
v.customize ["modifyvm", :id, "--memory", opts[:mem]] | ||
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]] | ||
end | ||
config.vm.network :private_network, ip: opts[:eth1] | ||
end | ||
end | ||
config.vm.synced_folder "./labs", "/home/vagrant/labs" | ||
config.vm.provision "shell", privileged: true, path: "./setup.sh" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Example Voting App | ||
================== | ||
|
||
This is an example Docker app with multiple services. It is run with Docker Compose and uses Docker Networking to connect containers together. You will need Docker Compose 1.6 or later. | ||
|
||
More info at https://blog.docker.com/2015/11/docker-toolbox-compose/ | ||
|
||
Architecture | ||
----- | ||
|
||
* A Python webapp which lets you vote between two options | ||
* A Redis queue which collects new votes | ||
* A Java worker which consumes votes and stores them in… | ||
* A Postgres database backed by a Docker volume | ||
* A Node.js webapp which shows the results of the voting in real time | ||
|
||
 | ||
|
||
Running | ||
------- | ||
|
||
Run in this directory: | ||
|
||
$ docker-compose up | ||
|
||
The app will be running on port 5000 on your Docker host, and the results will be on port 5001. | ||
|
||
Docker Hub images | ||
----------------- | ||
|
||
Docker Hub images for services in this app are built automatically from master: | ||
|
||
- [docker/example-voting-app-voting-app](https://hub.docker.com/r/docker/example-voting-app-voting-app/) | ||
- [docker/example-voting-app-result-app](https://hub.docker.com/r/docker/example-voting-app-result-app/) | ||
- [docker/example-voting-app-worker](https://hub.docker.com/r/docker/example-voting-app-worker/) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
version: "3" | ||
|
||
services: | ||
voting-app: | ||
build: ./voting-app/. | ||
volumes: | ||
- ./voting-app:/app | ||
ports: | ||
- "5000:80" | ||
links: | ||
- redis | ||
networks: | ||
- front-tier | ||
- back-tier | ||
|
||
result-app: | ||
build: ./result-app/. | ||
volumes: | ||
- ./result-app:/app | ||
ports: | ||
- "5001:80" | ||
links: | ||
- db | ||
networks: | ||
- front-tier | ||
- back-tier | ||
|
||
worker: | ||
build: ./worker | ||
links: | ||
- db | ||
- redis | ||
networks: | ||
- back-tier | ||
|
||
redis: | ||
image: redis | ||
ports: ["6379"] | ||
networks: | ||
- back-tier | ||
|
||
db: | ||
image: postgres:9.4 | ||
volumes: | ||
- "db-data:/var/lib/postgresql/data" | ||
networks: | ||
- back-tier | ||
|
||
volumes: | ||
db-data: | ||
|
||
networks: | ||
front-tier: | ||
back-tier: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM node:0.10 | ||
|
||
RUN mkdir /app | ||
WORKDIR /app | ||
|
||
ADD package.json /app/package.json | ||
RUN npm install && npm ls | ||
RUN mv /app/node_modules /node_modules | ||
|
||
ADD . /app | ||
|
||
ENV PORT 80 | ||
EXPOSE 80 | ||
|
||
CMD ["node", "server.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "result-app", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"body-parser": "^1.14.1", | ||
"cookie-parser": "^1.4.0", | ||
"express": "^4.13.3", | ||
"method-override": "^2.3.5", | ||
"async": "^1.5.0", | ||
"pg": "^4.4.3", | ||
"socket.io": "^1.3.7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
var express = require('express'), | ||
async = require('async'), | ||
pg = require("pg"), | ||
cookieParser = require('cookie-parser'), | ||
bodyParser = require('body-parser'), | ||
methodOverride = require('method-override'), | ||
app = express(), | ||
server = require('http').Server(app), | ||
io = require('socket.io')(server); | ||
|
||
io.set('transports', ['polling']); | ||
|
||
var port = process.env.PORT || 4000; | ||
|
||
io.sockets.on('connection', function (socket) { | ||
|
||
socket.emit('message', { text : 'Welcome!' }); | ||
|
||
socket.on('subscribe', function (data) { | ||
socket.join(data.channel); | ||
}); | ||
}); | ||
|
||
async.retry( | ||
{times: 1000, interval: 1000}, | ||
function(callback) { | ||
pg.connect('postgres://postgres@db/postgres', function(err, client, done) { | ||
if (err) { | ||
console.error("Failed to connect to db"); | ||
} | ||
callback(err, client); | ||
}); | ||
}, | ||
function(err, client) { | ||
if (err) { | ||
return console.err("Giving up"); | ||
} | ||
console.log("Connected to db"); | ||
getVotes(client); | ||
} | ||
); | ||
|
||
function getVotes(client) { | ||
client.query('SELECT vote, COUNT(id) AS count FROM votes GROUP BY vote', [], function(err, result) { | ||
if (err) { | ||
console.error("Error performing query: " + err); | ||
} else { | ||
var data = result.rows.reduce(function(obj, row) { | ||
obj[row.vote] = row.count; | ||
return obj; | ||
}, {}); | ||
io.sockets.emit("scores", JSON.stringify(data)); | ||
} | ||
|
||
setTimeout(function() {getVotes(client) }, 1000); | ||
}); | ||
} | ||
|
||
app.use(cookieParser()); | ||
app.use(bodyParser()); | ||
app.use(methodOverride('X-HTTP-Method-Override')); | ||
app.use(function(req, res, next) { | ||
res.header("Access-Control-Allow-Origin", "*"); | ||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | ||
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS"); | ||
next(); | ||
}); | ||
|
||
app.use(express.static(__dirname + '/views')); | ||
|
||
app.get('/', function (req, res) { | ||
res.sendFile(path.resolve(__dirname + '/views/index.html')); | ||
}); | ||
|
||
server.listen(port, function () { | ||
var port = server.address().port; | ||
console.log('App running on port ' + port); | ||
}); |
Oops, something went wrong.