Skip to content

Commit

Permalink
[#114] Add CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastro committed Aug 20, 2022
1 parent 6066324 commit 8ec224d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions coffer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ library
, tomland
, unordered-containers
, validation-selective
, wai-cors
, warp
default-language: Haskell2010

Expand Down
18 changes: 17 additions & 1 deletion lib/Web/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import Data.Maybe (fromMaybe)
import Data.Proxy
import Data.Text qualified as T
import Data.Text.IO qualified as TIO
import Network.HTTP.Types (hContentType)
import Network.Wai.Handler.Warp (run)
import Network.Wai.Middleware.Cors
(CorsResourcePolicy(..), cors, simpleCorsResourcePolicy, simpleMethods)
import Servant.Server (serve)
import System.Environment (lookupEnv)
import System.Exit (exitFailure)
Expand Down Expand Up @@ -41,6 +44,19 @@ runServer = do
configPath <- getConfigPath
-- TODO: try sending config via api.
config <- readConfig configPath
run 8081 do
run 8081 $ corsMiddleware do
serve (Proxy :: Proxy API) do
makeServer \_token -> reportErrors . runBackendIO' . runCommand config
where
-- Allow cross-origin requests from Swagger Editor/UI.
corsMiddleware = cors $ const $ Just simpleCorsResourcePolicy
{ corsRequestHeaders = [hContentType, "token"]
, corsMethods = "DELETE" : "PUT" : simpleMethods
, corsOrigins = Just
( [ "https://petstore.swagger.io"
, "https://editor.swagger.io"
, "https://editor-next.swagger.io"
]
, False
)
}
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ library:
- tomland
- unordered-containers
- validation-selective
- wai-cors
- warp

executables:
Expand Down

0 comments on commit 8ec224d

Please sign in to comment.