-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
54 lines (45 loc) · 1.36 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
# frozen_string_literal: true
require 'sciolyff'
require 'cgi'
run lambda { |env|
req = Rack::Request.new(env)
unless req.path_info.match? %r{\A/api\z}
return [200,
{ 'Content-Type' => 'text/html' },
File.open('playground.html')]
end
res = Rack::Response.new(nil, 400, { 'Access-Control-Allow-Origin' => '*' })
body = req.body.read
# Ignore params in body because there are too many, leading to RangeError
params = CGI.parse(req.query_string)
type = params['type']&.first
hide_raw = params['hide_raw']&.first
color = params['color']&.first
validator = SciolyFF::Validator.new canonical: false
res.body =
if body.empty?
{ message: 'Please POST in SciolyFF (JSON or YAML)' }
elsif validator.valid? body
res.status = 200
{ html: SciolyFF::Interpreter.new(body).to_html(
hide_raw: (hide_raw == 'true'), color: color
) }
else
{ log: validator.last_log }
end
res.content_type =
if type == 'html'
str = res.body.values.first
unless res.body[:html]
str = '<meta charset="utf-8"/>'\
"<pre style=\"font-size: 16px;\">#{str}</pre>"
end
res.body = [str]
'text/html'
else
res.body[:log] = res.body[:log].split("\n") if res.body[:log]
res.body = [res.body.to_json]
'application/json'
end
res.finish
}