Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 1fc04a4

Browse files
committed
handler: setup cors on non-OPTIONS
1 parent 4f18d27 commit 1fc04a4

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

infra/api_gateway.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "allowed_origins" {
2+
type = "list"
3+
default = ["localhost:3333", "deckdeckgo.com"]
4+
}
5+
16
resource "aws_api_gateway_rest_api" "lambda-api" {
27
name = "deckdeckgo-handler-rest-api"
38
}
@@ -98,7 +103,7 @@ resource "aws_api_gateway_integration_response" "options_integration_response" {
98103
response_parameters = {
99104
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
100105
"method.response.header.Access-Control-Allow-Methods" = "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
101-
"method.response.header.Access-Control-Allow-Origin" = "'*'"
106+
"method.response.header.Access-Control-Allow-Origin" = "'${join(",",var.allowed_origins)}'"
102107
}
103108
}
104109

infra/handler/Main.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import qualified Data.Aeson as Aeson
2121
import qualified Network.AWS as Aws
2222
import qualified Network.AWS.DynamoDB as DynamoDB
2323
import qualified Network.Wai.Handler.Lambda as Lambda
24+
import qualified Network.Wai.Middleware.Cors as Cors
2425
import qualified Servant as Servant
2526
import qualified System.Random as Random
2627

@@ -99,7 +100,7 @@ main = do
99100
env <- Aws.newEnv Aws.Discover
100101

101102
liftIO $ putStrLn "Booted!"
102-
Lambda.run $ Servant.serve api (server env)
103+
Lambda.run $ Cors.simpleCors $ Servant.serve api (server env)
103104

104105
server :: Aws.Env -> Servant.Server API
105106
server env = serveDecks :<|> serveSlides

infra/handler/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
- text
1616
- unliftio
1717
- unordered-containers
18+
- wai-cors
1819
- wai-lambda
1920

2021
ghc-options:

infra/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ exports.handler = function(event, context, callback) {
8585

8686
// Parse the response from the Haskell process
8787
const response = JSON.parse(fs.readFileSync(responseFile, 'utf8'));
88-
console.log("Parsed response");
88+
console.log("Parsed response: " + JSON.stringify(response));
8989

9090
// Clean up the directory and finally reply
9191
console.log("cleaning up");

0 commit comments

Comments
 (0)