forked from avalanche123/cassandra-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
102 lines (78 loc) · 2.07 KB
/
app.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
92
93
94
95
96
97
98
99
100
101
102
# encoding: utf-8
require 'logger'
require 'rubygems'
require 'bundler/setup'
require 'sinatra/base'
require 'rack/cors'
require 'rack/parser'
require 'cassandra'
require 'json'
$: << File.expand_path('../', __FILE__)
class App < Sinatra::Base
configure do
set :root, File.expand_path('../', __FILE__) + '/app'
enable :static
disable :views
disable :method_override
disable :protection
end
use Rack::Cors do
allow do
origins /.*/
resource '/*',
:methods => [:get, :post, :put, :delete, :options],
:expose => ['Location'],
:headers => :any
end
end
use Rack::Parser, :content_types => {
'application/json' => JSON.method(:load)
}
get '/events/?' do
status 200
content_type 'text/event-stream'
headers 'Connection' => 'keep-alive'
stream(:keep_open) do |out|
stream_events(out)
end
end
get '/hosts/?' do
status 200
content_type 'application/json'
json_dump(cluster.hosts)
end
get '/keyspaces/?' do
status 200
content_type 'application/json'
json_dump(cluster.keyspaces)
end
get '/consistencies/' do
status 200
content_type 'application/json'
json_dump(Cassandra::CONSISTENCIES)
end
post '/execute/?' do
content_type 'application/json'
statement = params['statement']
statement.strip!
statement.chomp!(";")
options = {
:consistency => :one,
:trace => false
}
if params['options']
options[:trace] = !!params['options']['trace'] if params['options'].has_key?('trace')
options[:consistency] = params['options']['consistency'].to_sym if params['options'].has_key?('consistency') && Cassandra::CONSISTENCIES.include?(params['options']['consistency'].to_sym)
end
defer(session.execute_async(statement, options))
end
get '*' do
File.read(File.join(settings.public_folder, 'main.html'))
end
end
require 'app/helpers/async'
require 'app/helpers/json'
require 'app/helpers/sse'
App.helpers App::Helpers::Async
App.helpers App::Helpers::JSON
App.helpers App::Helpers::SSE