Skip to content

Commit

Permalink
feat: enable CORS on APIG
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-krail committed Feb 10, 2024
1 parent ba2694f commit e9d968e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions functions/counter/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions functions/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use lambda_http::{
run, service_fn, Body, Error, Request, RequestExt, Response,
};
use lambda_runtime::tower::ServiceBuilder;
use std::{env, time::Duration};
use tower_http::cors::{AllowOrigin, CorsLayer};
use tracing::info;

Expand All @@ -30,7 +29,7 @@ async fn main() -> Result<(), Error> {
// Create the DynamoDB client
let ddb_client = DynamoDbClient::new(&aws_sdk_config);
// Get table name from environment
let table_name = env::var("TABLE_NAME").expect("Missing TABLE_NAME env var");
let table_name = std::env::var("TABLE_NAME").expect("Missing TABLE_NAME env var");

run(ServiceBuilder::new()
.layer(
Expand All @@ -39,7 +38,6 @@ async fn main() -> Result<(), Error> {
.allow_origin(AllowOrigin::exact("https://seankrail.dev".parse().unwrap()))
// Uncomment below for development
// .allow_origin(AllowOrigin::any())
.max_age(Duration::from_secs(60 * 60)),
)
.service(service_fn(|event: Request| async {
handle_request(event, &ddb_client, &table_name).await
Expand Down
7 changes: 7 additions & 0 deletions lib/counter-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RetentionDays } from "aws-cdk-lib/aws-logs";
import { Construct } from "constructs";

const DOMAIN_NAME = "api.seankrail.dev";
const ORIGIN = "https://seankrail.dev";

export class CounterStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
Expand Down Expand Up @@ -63,5 +64,11 @@ export class CounterStack extends Stack {
const counter = api.root.addResource("count").addResource("{counter}");
counter.addMethod("GET");
counter.addMethod("POST");
counter.addCorsPreflight({
allowMethods: ["GET", "POST"],
allowOrigins: [ORIGIN],
// Uncomment below for development
// allowOrigins: ["*"],
});
}
}

0 comments on commit e9d968e

Please sign in to comment.