File tree Expand file tree Collapse file tree 1 file changed +15
-15
lines changed Expand file tree Collapse file tree 1 file changed +15
-15
lines changed Original file line number Diff line number Diff line change 19
19
server = WEBrick ::HTTPServer . new :Port => port , :DocumentRoot => root
20
20
21
21
server . mount_proc '/comments.json' do |req , res |
22
- comments = JSON . parse ( File . read ( './comments.json' ) )
23
-
24
- if req . request_method == 'POST'
25
- # Assume it's well formed
26
- comment = { }
27
- req . query . each do |key , value |
28
- comment [ key ] = value . force_encoding ( 'UTF-8' )
22
+ comments = JSON . parse ( File . read ( './comments.json' ) )
23
+
24
+ if req . request_method == 'POST'
25
+ # Assume it's well formed
26
+ comment = { }
27
+ req . query . each do |key , value |
28
+ comment [ key ] = value . force_encoding ( 'UTF-8' )
29
+ end
30
+ comments << comment
31
+ File . write ( './comments.json' , JSON . pretty_generate ( comments , :indent => ' ' ) )
29
32
end
30
- comments << comment
31
- File . write ( './comments.json' , JSON . pretty_generate ( comments , :indent => ' ' ) )
32
- end
33
-
34
- # always return json
35
- res [ 'Content-Type' ] = 'application/json'
36
- res [ 'Cache-Control' ] = 'no-cache'
37
- res . body = JSON . generate ( comments )
33
+
34
+ # always return json
35
+ res [ 'Content-Type' ] = 'application/json'
36
+ res [ 'Cache-Control' ] = 'no-cache'
37
+ res . body = JSON . generate ( comments )
38
38
end
39
39
40
40
trap 'INT' do server . shutdown end
You can’t perform that action at this time.
0 commit comments