Skip to content

Commit

Permalink
Merge pull request #27 from youngeek-0410/feat-26/config-CORS
Browse files Browse the repository at this point in the history
fixed CORS settings
  • Loading branch information
fuu38 authored Dec 17, 2023
2 parents 0b46e67 + 493c576 commit 46f5f34
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lambda/questions/GET/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"encoding/json"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

type Question struct {
QuestionID int `json:"question_id" dynamodbav:"question_id"`
Statement string `json:"statement" dynamodbav:"statement"`
QuestionID int `json:"question_id" dynamodbav:"question_id"`
Statement string `json:"statement" dynamodbav:"statement"`
}

type Response struct {
Expand Down Expand Up @@ -58,21 +59,20 @@ func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.A

return events.APIGatewayProxyResponse{
StatusCode: 200,
Body: string(jsonResponse),
Headers: map[string]string{"Content-Type": "application/json"},
Body: string(jsonResponse),
Headers: map[string]string{"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"},
}, nil
}

func serverErrorResponse(err error) (events.APIGatewayProxyResponse, error) {
fmt.Println(err.Error())
return events.APIGatewayProxyResponse{
StatusCode: 500,
Body: `{"message": "Internal Server Error"}`,
Headers: map[string]string{"Content-Type": "application/json"},
Body: `{"message": "Internal Server Error"}`,
Headers: map[string]string{"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"},
}, nil
}


func main() {
lambda.Start(handler)
}
2 changes: 2 additions & 0 deletions lambda/room/POST/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.A
return events.APIGatewayProxyResponse{
Body: string(responseBody),
StatusCode: http.StatusCreated,
Headers: map[string]string{"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"},
}, nil
}

Expand All @@ -62,6 +63,7 @@ func createEmptyResponseWithStatus(statuCode int) events.APIGatewayProxyResponse
return events.APIGatewayProxyResponse{
Body: "",
StatusCode: statuCode,
Headers: map[string]string{"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"},
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/candle-backend-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export class CandleBackendStack extends cdk.Stack {
// Create API Gateway
const api = new apigateway.RestApi(this, 'CandleBackendApi', {
restApiName: 'CandleBackendApi',
defaultCorsPreflightOptions: {
allowOrigins: apigateway.Cors.ALL_ORIGINS,
allowHeaders: apigateway.Cors.DEFAULT_HEADERS,
allowMethods: apigateway.Cors.ALL_METHODS,
},
});

const questionTable = new cdk.aws_dynamodb.Table(this, 'CandleBackendQuestionTable', {
Expand Down Expand Up @@ -58,6 +63,7 @@ export class CandleBackendStack extends cdk.Stack {
environment: {
TABLE_NAME: questionTable.tableName,
},

});
questionTable.grantReadWriteData(questionsGETHandler);
questions.addMethod('GET', new apigateway.LambdaIntegration(questionsGETHandler));
Expand Down

0 comments on commit 46f5f34

Please sign in to comment.