-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
91 lines (72 loc) · 2.11 KB
/
web.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
require 'rubygems'
require 'sinatra'
require 'mongoid'
#require 'json/pure'
Mongoid.load!("config/mongoid.yml")
disable :protection
set :show_exceptions, true
configure :production do
end
class Request
include Mongoid::Document
field :m, type: String # REQUEST_METHOD
field :a, type: String # HTTP_USER_AGENT,
field :r, type: String # REMOTE_ADDR
field :t, type: DateTime # Time.now
field :q, type: Hash # rack.request.query_hash
field :c, type: Hash # rack.request.cookie_hash"
field :p, type: String # REQUEST_PATH
field :h, type: String # HTTP_HOST
field :e, type: Hash
field :x, type: Hash # Http Headers
end
def catcher(path, opts={}, &block)
post(path, opts, &block)
put(path, opts, &block)
get(path, opts, &block)
delete(path, opts, &block)
head(path, opts, &block)
end
get '/data/latest' do
content_type 'application/json'
Request.only((params[:only] || 't,m,a,r,q,c,p,h,x,j').split(','))
.where(params[:query] || {})
.limit(params[:limit] || 10)
.skip(params[:skip] || 0)
.order_by(:_id,:desc).map { |row| row.attributes }.to_json
end
catcher '/*' do
headers({
'Access-Control-Allow-Origin' => '*'
})
attrib = {
t: Time.now,
m: request.env["REQUEST_METHOD"],
a: request.env["HTTP_USER_AGENT"],
r: request.env["REMOTE_ADDR"],
q: request.env["rack.request.query_hash"],
c: request.env["rack.request.cookie_hash"],
p: request.env["REQUEST_PATH"],
h: request.env["HTTP_HOST"],
b: request.env["rack.request.form_vars"],
x: {}
}
begin
attrib[:j] = JSON.parse(request.env["rack.request.form_vars"])
rescue
end
request.env.select do |k,v|
attrib[:x][k[5..-1]] = v if k.start_with?("HTTP_") and not ["COOKIE","HTTP_ACCEPT","HTTP_HOST","HTTP_USER_AGENT","HTTP_VERSION"].include?(k)
end
Request.create(attrib)
{
"text/xml" => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>saved</request>\n",
"application/javascript" => "{\"request\":\"saved\"}"
}.each do |content,resp|
if request.accept? content
content_type content
return resp
end
end
return "saved"
end