-
Notifications
You must be signed in to change notification settings - Fork 0
/
gettogether.rb
135 lines (113 loc) · 3.53 KB
/
gettogether.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'sinatra'
require "sinatra/reloader" if development?
require 'active_record'
require 'digest/sha1'
require 'pry'
require 'uri'
require 'open-uri'
require 'bcrypt'
# require 'nokogiri'
###########################################################
# Configuration
###########################################################
set :public_folder, File.dirname(__FILE__) + '/public'
db = URI.parse('postgres://gtgztuwtqlcxzw:4HAFe2VjQShWUyy94qVQpLCGMg@ec2-54-235-152-22.compute-1.amazonaws.com:5432/dkg1uaf18h609')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
# ActiveRecord::Base.establish_connection(
# :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
# :database => 'gettogether',
# :encoding => 'utf8'
# )
# Handle potential connection pool timeout issues
after do
ActiveRecord::Base.connection.close
end
# turn off root element rendering in JSON
ActiveRecord::Base.include_root_in_json = false
###########################################################
# Models
###########################################################
# Models to Access the database through ActiveRecord.
# Define associations here if need be
# http://guides.rubyonrails.org/association_basics.html
class User < ActiveRecord::Base
attr_accessible :username, :identifier, :password, :salt
validates :password, presence: true
before_save do |record|
record.identifier = Digest::SHA1.hexdigest(username)
end
end
###########################################################
# Authentication
###########################################################
enable :sessions
post "/signup" do
data = JSON.parse(request.body.read)
username = data['username']
password = data['password']
password_salt = BCrypt::Engine.generate_salt
password_hash = BCrypt::Engine.hash_secret(password, password_salt)
if User.find_by_username(username)
{success: false, message: 'Username already exists'}.to_json
else
user = User.create(username: username, salt: password_salt, password: password_hash)
{success: true, id: user.identifier}.to_json
end
end
post "/login" do
data = JSON.parse(request.body.read)
username = data['username']
password = data['password']
p 'login', username, password
user = User.find_by_username(username)
if user != nil
password_hash = BCrypt::Engine.hash_secret(password, user[:salt])
if user[:password] == password_hash
{success: true, id: user.identifier}.to_json
else
{success: false, message: 'Incorrect username or password'}.to_json
end
else
p 'login: user not found'
{success: false, message: 'User not found'}.to_json
end
end
###########################################################
# Routes
###########################################################
get "/" do
redirect '/index.html'
end
get '/signup' do
redirect '/index.html'
end
get '/login' do
redirect '/index.html'
end
###########################################################
# Utility
###########################################################
def read_url_head url
head = ""
url.open do |u|
begin
line = u.gets
next if line.nil?
head += line
break if line =~ /<\/head>/
end until u.eof?
end
head + "</html>"
end
def get_url_title url
# Nokogiri::HTML.parse( read_url_head url ).title
result = read_url_head(url).match(/<title>(.*)<\/title>/)
result.nil? ? "" : result[1]
end