-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrack_cors_roda.rb
55 lines (42 loc) · 1.17 KB
/
rack_cors_roda.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
# frozen_string_literal: true
require 'roda'
require 'rack/cors'
class RackCorsRoda < Roda
use Rack::Cors, debug: true, logger: Logger.new(STDOUT) do
allowed_methods = %i[get post put delete options head]
allow do
origins 'https://my-frontend-app.netlify.com'
resource '*', headers: :any, methods: allowed_methods
end
end
plugin :default_headers,
'Strict-Transport-Security'=>'max-age=16070400;',
'X-Content-Type-Options'=>'nosniff',
'X-Frame-Options'=>'deny',
'X-XSS-Protection'=>'1; mode=block'
plugin :halt
plugin :hash_routes
plugin :head
plugin :request_headers
plugin :json, content_type: 'text/plain'
plugin :json_parser
route do |r|
r.root do
'hello rack-cors + roda app'
end
r.hash_routes
end
hash_branch('pizza') do |r|
r.post do
# POST /pizza/toppings
# { "topping": "cheese" }
r.is 'toppings' do
params = JSON.parse(r.body.read)
r.halt(400, error: 'no params!') unless params
topping = params['topping']
r.halt(400, error: 'no topping!') unless params['topping']
{ your_topping: topping }
end
end
end
end