-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.rb
55 lines (47 loc) · 1.36 KB
/
hello.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
#!/usr/bin/env ruby
require 'goliath'
require 'erb'
require 'uri'
require 'em-synchrony/activerecord'
require 'yajl'
db = URI.parse(ENV['DATABASE_URL'] || 'http://localhost')
if db.scheme == 'postgres' # This section makes Heroku work
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'
)
else # And this is for my local environment
environment = ENV['DATABASE_URL'] ? 'production' : 'development'
db = YAML.load(ERB.new(File.read('config/database.yml')).result)[environment]
ActiveRecord::Base.establish_connection(db)
end
class User < ActiveRecord::Base
end
class HelloWorld < Goliath::API
def response(env)
page = ERB.new(File.read('template.erb')).result
[200, {}, page]
end
end
class CreateUser < Goliath::API
def response(env)
user = User.create(first_name: params['first_name'])
[200, {}, user.first_name]
end
end
class PostHelloWorld < Goliath::API
use Goliath::Rack::Params
def response(env)
user = User.find(params['id'])
[200, {}, "hello post world! #{params}, #{user.first_name}"]
end
end
class RackRoutes < Goliath::API
get "/hello_world", HelloWorld
post "/hello_world", PostHelloWorld
post "/users", CreateUser
end