Skip to content

Commit 8b67cda

Browse files
committed
Connect to the database postgres and store requests inside the database
[refs #f34238344b71]
1 parent 791c6a3 commit 8b67cda

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

developing/webapp/app.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
require 'sinatra'
2+
require 'pg'
23

34
set :bind, '0.0.0.0'
45
set :port, 9494
56

67
get '/' do
7-
'Hello world!'
8+
conn = PG::Connection.open(ENV['POSTGRES_HOST'], 5432, '', '', ENV['POSTGRES_DB'],
9+
ENV['POSTGRES_USER'], ENV['POSTGRES_PASSWORD'])
10+
sql = "INSERT INTO requests (ip, path, host, requested_at) VALUES ($1, $2, $3, $4)"
11+
res = conn.exec(sql, [request.ip, '/', Socket.gethostname, Time.now])
12+
conn.finish
13+
'Hello, World!'
814
end

developing/webapp/docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ services:
55
image: jfahrer/webapp-ruby:latest
66
build:
77
context: .
8+
environment:
9+
- POSTGRES_USER=demo
10+
- POSTGRES_PASSWORD=secret
11+
- POSTGRES_DB=demo
12+
- POSTGRES_HOST=pg
813
volumes:
914
- .:/app
1015
ports:

0 commit comments

Comments
 (0)