This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
/
rack.ru
128 lines (100 loc) · 3.29 KB
/
rack.ru
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
require 'pp'
require 'sails'
puts "Connecting to the database"
#Sails::Utils.connect_db
module Sails
class RackAdapter
def initialize(rails_app)
@rails = rails_app
end
def connect
@remote ||= Remote.connect
end
def call(env)
response = @rails.call env
return response unless (200..299).include? response[0] || response[2].to_s != 'OK'
response[2].close # release rails, let it work for other requests
connect # to the remote
@wave = nil
1/0 unless env['PATH_INFO'] =~ /^\/waves\/(.+)\/([0-9]+)$/
name = $1
version = $2.to_i
@wave = @remote[name]
elapsed = 0
interval = 0.1
timer = EventMachine::PeriodicTimer.new(interval) do
next if (elapsed += interval) <= 5 && @remote[name].newest_version <= version
timer.cancel
body = []
if @remote[name].newest_version > version
@wave = @remote[name]
#version = @wave.newest_version
until version == @remote[name].newest_version
delta = @wave[version + 1]
version = delta.version
delta.operations.each do |operation|
if operation.is_a? Operations::Mutate
unless operation.document_id == 'conversation'
blip = @wave.blips[operation.document_id]
parent = blip.parent_blip
parent = if parent
"'#{parent.name}'"
else
'undefined'
end
authors = blip.authors.map {|author| author.to_html }
body << "update_blip('#{blip.name}', #{parent}, '#{authors.join(', ')}', \"#{escape_js blip.to_xml}\");"
end # unless
elsif operation.is_a? Operations::RemoveUser
operation.who.each do |who|
body << "remove_user('#{who}');"
end # each
else#if operation.is_a? Operations::AddUser
operation.who.each do |who|
html = who.to_s
html = who.to_html if who.respond_to? :to_html
body << "add_user('#{who}', '#{html}');"
end # each
end # if
end # each
end # until
end # if
body << "at_version(#{version});"
env['async.callback'].call [200, {
'Cache-Control' => 'no-cache',
'Content-Type' => 'text/javascript; charset=utf-8',
}, body.uniq.join("\n")]
env['rack.errors'].write %{%s - %s [%s] "%s %s%s %s" %d\n} % [
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
env["REMOTE_USER"] || "-",
Time.now.strftime("%d/%b/%Y %H:%M:%S"),
env["REQUEST_METHOD"],
env["PATH_INFO"],
env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
env["HTTP_VERSION"],
200 ]
end # on elapsed
[-1, {}, []]
end # def call
# data = "<script type=\"text/javascript\">
# document.getElementById('data').innerHTML = \"#{escape_js wave.to_xml}\";
# document.getElementById('version').innerHTML = \"#{version}\";
#</script>"
#yield "<script type=\"text/javascript\">window.location.reload();</script>"
def escape text
text.gsub('<', '<').gsub('>', '>')
end
def escape_js text
text.gsub('\\', '\\\\').gsub('"', '\\"')
end
end # class
end # module
use Rack::CommonLogger
rails_app = Rack::Adapter::Rails.new(:root => './rails')
mapping = {'/ajax' => Sails::RackAdapter.new(rails_app),
'/' => rails_app}
if File.exists? File.join(File.dirname(__FILE__), 'doc')
mapping['/doc'] = Rack::File.new('./doc')
end
app = Rack::URLMap.new(mapping)
run app