Skip to content

Commit 99dcd8a

Browse files
author
remi Taylor
committed
Make Flex Cloud SQL app sample more clear
- add descriptive comments - move logic onto their own lines to help clarity - Hashing visitor's IP - Fetching recent visits from the database
1 parent 693f483 commit 99dcd8a

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

appengine/cloudsql/app.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
# [END connect]
2626

2727
get "/" do
28+
# Store a hash of the visitor's ip address
29+
visitor_ip = Digest::SHA256.hexdigest request.ip
30+
2831
# Save visit in database
29-
DB[:visits].insert(
30-
user_ip: Digest::SHA256.hexdigest(request.ip),
31-
timestamp: Time.now
32-
)
32+
DB[:visits].insert user_ip: visitor_ip, timestamp: Time.now
33+
34+
# Retrieve the latest 10 visit records from the database
35+
visits = DB[:visits].order(Sequel.desc(:timestamp)).limit(10)
3336

3437
response.write "Last 10 visits:\n"
3538

36-
DB[:visits].order(Sequel.desc(:timestamp)).limit(10).each do |visit|
39+
visits.each do |visit|
3740
response.write "Time: #{visit[:timestamp]} Addr: #{visit[:user_ip]}\n"
3841
end
3942

0 commit comments

Comments
 (0)