Skip to content

Commit c66df67

Browse files
committed
Update the swarm-demo-frontend to v5
[refs %fe270e7659a4]
1 parent 3e8836d commit c66df67

13 files changed

+410
-18
lines changed

swarm/swarm-demo-frontend/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
FROM ruby:2.4.3-alpine
22

3+
RUN apk --no-cache add build-base postgresql-dev
4+
35
WORKDIR /tmp
46
COPY Gemfile* ./
57
RUN bundle install

swarm/swarm-demo-frontend/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
source 'https://rubygems.org'
22

33
gem 'sinatra'
4+
gem 'pg'

swarm/swarm-demo-frontend/Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ GEM
22
remote: https://rubygems.org/
33
specs:
44
mustermann (1.0.1)
5+
pg (1.0.0)
56
rack (2.0.3)
67
rack-protection (2.0.0)
78
rack
@@ -16,6 +17,7 @@ PLATFORMS
1617
ruby
1718

1819
DEPENDENCIES
20+
pg
1921
sinatra
2022

2123
BUNDLED WITH

swarm/swarm-demo-frontend/app.rb

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
require 'sinatra'
2+
require 'erb'
3+
require 'pg'
24

35
set :bind, '0.0.0.0'
46
set :port, 8080
57

8+
AppState = {}
9+
AppState[:total_requests] = 0
10+
611
get '/' do
7-
<<-BODY
8-
<html>
9-
<body>
10-
<h1>Docker Swarm Demo</h1>
11-
<p>This page was served by <b>#{Socket.gethostname}</b></p>
12-
</body>
13-
</html>
14-
BODY
12+
AppState[:total_requests] += 1
13+
14+
manager = RequestManager.new
15+
manager.store_request(request.ip, '/', Socket.gethostname, Time.now)
16+
17+
erb :index, locals: { requests: manager.get_requests(25), total_requests: AppState[:total_requests] }
18+
end
19+
20+
class RequestManager
21+
22+
def store_request(ip, path, host, requested_at)
23+
sql = "INSERT INTO requests (ip, path, host, requested_at) VALUES ($1, $2, $3, $4)"
24+
res = conn.exec(sql, [ip, path, host, requested_at])
25+
end
26+
27+
def get_requests(limit = 100)
28+
sql = "SELECT ip, path, host, requested_at FROM requests ORDER BY id DESC LIMIT $1;"
29+
res = conn.exec(sql, [limit])
30+
res.map do |row|
31+
OpenStruct.new(row.to_h)
32+
end
33+
end
34+
35+
private
36+
37+
def conn
38+
@conn ||= PG::Connection.open(ENV['POSTGRES_HOST'], 5432, '', '', ENV['POSTGRES_DB'],
39+
ENV['POSTGRES_USER'], ENV['POSTGRES_PASSWORD'])
40+
end
1541
end
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
version: "3.3"
2+
13
services:
2-
version: "3.3"
4+
frontend:
5+
image: jfahrer/swarm-demo-frontend:v5
6+
build:
7+
context: .
8+
ports:
9+
- 8080:8080
10+
volumes:
11+
- .:/app
12+
environment:
13+
- POSTGRES_USER=demo
14+
- POSTGRES_PASSWORD=secret
15+
- POSTGRES_DB=demo
16+
- POSTGRES_HOST=db
317

4-
services:
5-
frontend:
6-
image: jfahrer/swarm-demo-frontend:v4
7-
build:
8-
context: .
9-
ports:
10-
- 8080:8080
11-
volumes:
12-
- .:/app
18+
db:
19+
image: jfahrer/swarm-demo-db:v1
20+
environment:
21+
- POSTGRES_USER=demo
22+
- POSTGRES_PASSWORD=secret
23+
- POSTGRES_DB=demo

swarm/swarm-demo-frontend/public/css/bootstrap.min.css

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

swarm/swarm-demo-frontend/public/fonts/glyphicons-halflings-regular.svg

Lines changed: 288 additions & 0 deletions
Loading
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)