-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.rb
40 lines (30 loc) · 1010 Bytes
/
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
#!/usr/bin/env ruby
require 'sinatra'
require 'date'
set :host_authorization, { permitted_hosts: [] }
before { response.headers['Access-Control-Allow-Origin'] = '*' }
options "*" do
response.headers["Allow"] = "HEAD,GET,PUT,POST,DELETE,OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept"
200
end
get('/') { send_file File.expand_path('index.html', settings.public_folder) }
get('/date') {
content_type :json, 'charset' => 'utf-8'
today = Date.today
render_data(today.month, today.day)
}
get('/date/:month/:day') {
content_type :json, 'charset' => 'utf-8'
render_data(params[:month], params[:day])
}
def render_data(month, day)
filename = "#{'%02d' % month.to_i}-#{'%02d' % day.to_i}.json"
path = File.expand_path(File.join('data', filename), settings.public_folder)
if params['callback']
data = File.read(path)
"#{params['callback']}(#{data})"
else
send_file path
end
end