git.iko.soy/lynxjs-elm/compiler
on master help sign in
get Access-Control-Allow-Origin working for /quotes endpoint
commit3722eb67ab06f0fa7673ae1223876e0bf3992d14 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 5 Apr 2022, 04:40 (5 years ago) parent1455e11
2 files changed+8−16
worker/src/Cors.hs+5−13 worker/src/Endpoint/Quotes.hs+3−3
worker/src/Cors.hs+5−13
@@ -1,7 +1,6 @@
11{-# OPTIONS_GHC -Wall #-}
22module Cors
33 ( allow
4− , open
54 )
65 where
76
@@ -9,7 +8,7 @@ module Cors
98import qualified Data.HashSet as HashSet
109import Network.URI (parseURI)
1110import Snap.Core (Snap, Method, method)
12−import Snap.Util.CORS (CORSOptions(..), HashableMethod(..), OriginList(..), applyCORS, mkOriginSet)
11+import Snap.Util.CORS (CORSOptions(..), HashableMethod(..), OriginList(Origins), applyCORS, mkOriginSet)
1312
1413
1514
@@ -18,15 +17,7 @@ import Snap.Util.CORS (CORSOptions(..), HashableMethod(..), OriginList(..), appl
1817
1918allow :: Method -> [String] -> Snap () -> Snap ()
2019allow method_ origins snap =
21− applyCORS (toOptions method_ (toOriginList origins)) $
22− method method_ $
23− snap
24−
25−
26−open :: Method -> Snap () -> Snap ()
27−open method_ snap =
28− applyCORS (toOptions method_ Everywhere) $
29− method method_ $
20+ applyCORS (toOptions method_ origins) $ method method_ $
3021 snap
3122
3223
@@ -34,9 +25,10 @@ open method_ snap =
3425-- TO OPTIONS
3526
3627
37−toOptions :: (Monad m) => Method -> OriginList -> CORSOptions m
38−toOptions method_ allowedOrigins =
28+toOptions :: (Monad m) => Method -> [String] -> CORSOptions m
29+toOptions method_ origins =
3930 let
31+ allowedOrigins = toOriginList origins
4032 allowedMethods = HashSet.singleton (HashableMethod method_)
4133 in
4234 CORSOptions
worker/src/Endpoint/Quotes.hs+3−3
@@ -12,7 +12,6 @@ import GHC.Prim
1212import GHC.ST (ST(ST), runST)
1313import qualified System.Random as Random
1414
15−import qualified Cors
1615import Snap.Core
1716
1817
@@ -22,8 +21,9 @@ import Snap.Core
2221
2322endpoint :: Snap ()
2423endpoint =
25− Cors.open GET $
26− do modifyResponse $ setContentType "application/json"
24+ do modifyResponse
25+ $ setContentType "application/json"
26+ . setHeader "Access-Control-Allow-Origin" "*"
2727 writeBS =<< liftIO getQuote
2828
2929