-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
47 lines (38 loc) · 1.03 KB
/
server.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
require 'rubygems'
require 'bundler'
require 'cgi'
require 'open-uri'
require 'json'
Bundler.require
require './generator'
require './fetcher'
ROOT_HTML = File.read('index.html').freeze
CACHE =
if ENV['MEMCACHEDCLOUD_SERVERS']
Dalli::Client.new \
ENV['MEMCACHEDCLOUD_SERVERS'].split(','),
username: ENV['MEMCACHEDCLOUD_USERNAME'],
password: ENV['MEMCACHEDCLOUD_PASSWORD']
else
Dalli::Client.new
end
class Server
def self.call(env)
request_method = env['REQUEST_METHOD']
path = env['PATH_INFO']
query = CGI.parse(env['QUERY_STRING'])
if request_method == 'GET' && path == '/feed.json' && query.key?('source')
begin
result = Fetcher.call(query['source'][0])
return [200, { 'Content-Type' => 'application/json' }, [result]]
rescue => e
puts "Error! #{e}"
return [500, {}, []]
end
end
if request_method == 'GET' && path == '/'
return [200, { 'Content-Type' => 'text/html' }, [ROOT_HTML]]
end
return [500, {}, []]
end
end