forked from javallone/regexper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
84 lines (75 loc) · 1.89 KB
/
config.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
root = Pathname.new(__FILE__).dirname
$LOAD_PATH << root + 'lib'
$stdout.sync = true
require 'bundler'
require 'regexper'
require 'json'
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
use Rack::Logger
use Rack::CommonLogger
map '/assets' do
environment = Sprockets::Environment.new
environment.append_path root.join('app/assets/javascripts')
environment.append_path root.join('app/assets/stylesheets')
environment.append_path root.join('app/assets/images')
run environment
end
map '/parse' do
run lambda { |env|
request = Rack::Request.new(env)
regexp = request.body.read.force_encoding('UTF-8')
headers = {
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
}
begin
[
200,
headers,
[
JSON.generate({
:raw_expr => regexp,
:structure => Regexper.parse(regexp).to_obj
}, :max_nesting => 1000)
]
]
rescue Regexper::ParseError => error
env['rack.logger'].error("Parse failed: (exception=\"#{error}\") (input=\"#{regexp}\")")
[
400,
headers,
[
JSON.generate({
:error => error.to_s
})
]
]
rescue JSON::NestingError => error
env['rack.logger'].error("Excessive nesting: (exception=\"#{error}\") (input=\"#{regexp}\")")
[
500,
headers,
[
JSON.generate({
:error => "Regexp contains excessive nesting (error: #{error.to_s})"
})
]
]
rescue Exception => exception
env['rack.logger'].error("Unexpected error: #{exception}")
[
500,
headers,
[
JSON.generate({
:error => 'Server error'
})
]
]
end
}
end
use Rack::Static, :urls => ["/"], :root => root.join("public"), :index => "index.html"
run lambda { |env|
[204, {}, []]
}