Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thedersen committed Apr 12, 2020
0 parents commit b08a596
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@app
test-cors

@cors
allowOrigins https://example.com
allowMethods GET,POST,OPTIONS
allowHeaders Authorization,X-Custom-Header
exposeHeaders Content-Length,X-Custom-Header
maxAge 7200
allowCredentials true

@http
get /foo

@macros
architect/macro-http-api
arc-macro-cors
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
sam.yaml
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src/http
.arc
sam.json
sam.yaml
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# arc-macro-cors

Enable CORS for [Architect](https://arc.codes) HTTP APIs.

## Ok..How?!

Install:

`npm i arc-macro-cors`

And add to your .arc-file for default CORS settings:

```arc
@app
myapp
@cors
@http
get /
get /foo
post /foo
@macros
architect/macro-http-api #Required until HTTP APIs are default in arc
arc-macro-cors
```
Or configure specific CORS settings:

```arc
@app
myapp
@cors
allowOrigins https://example.com
allowMethods GET,POST,OPTIONS
allowHeaders Authorization,X-Custom-Header
exposeHeaders Content-Length,X-Custom-Header
maxAge 7200
allowCredentials true
@http
get /
get /foo
post /foo
@macros
architect/macro-http-api #Required until HTTP APIs are default in arc
arc-macro-cors
```

See [AWS::Serverless::HttpApi/HttpApiCorsConfiguration](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-httpapi-httpapicorsconfiguration.html) for more information.
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "arc-macro-cors",
"version": "1.0.0",
"description": "Enable CORS for Architect HTTP APIs",
"main": "src/macros/arc-macro-cors.js",
"dependencies": {
"@architect/utils": "^1.5.2"
},
"scripts": {
"lint": "xo --fix"
},
"repository": {
"type": "git",
"url": "git+https://github.com/thedersen/arc-macro-cors.git"
},
"xo": {
"prettier": true,
"space": 2
},
"prettier": {
"trailingComma": "es5"
},
"author": "Thomas Pedersen <[email protected]>",
"license": "MIT",
"devDependencies": {
"@architect/macro-http-api": "^1.0.0",
"xo": "^0.28.1"
}
}
246 changes: 246 additions & 0 deletions sam.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "Exported by architect/[email protected] on 2020-04-12T19:56:04.132Z",
"Resources": {
"GetFoo": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"CodeUri": "./src/http/get-foo",
"Runtime": "nodejs12.x",
"MemorySize": 1152,
"Timeout": 5,
"Environment": {
"Variables": {
"ARC_ROLE": {
"Ref": "Role"
},
"ARC_CLOUDFORMATION": {
"Ref": "AWS::StackName"
},
"ARC_APP_NAME": "test-cors",
"ARC_HTTP": "aws_proxy",
"NODE_ENV": "staging",
"SESSION_TABLE_NAME": "jwe"
}
},
"Role": {
"Fn::Sub": [
"arn:aws:iam::${AWS::AccountId}:role/${roleName}",
{
"roleName": {
"Ref": "Role"
}
}
]
},
"Events": {
"GetFooEvent": {
"Type": "HttpApi",
"Properties": {
"Path": "/foo",
"Method": "GET",
"ApiId": {
"Ref": "TestCorsApi"
}
}
}
}
}
},
"GetIndex": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"CodeUri": "/Users/thomas/n/lib/node_modules/@architect/architect/node_modules/@architect/http-proxy/dist",
"Runtime": "nodejs12.x",
"MemorySize": 1152,
"Timeout": 5,
"Environment": {
"Variables": {
"ARC_ROLE": {
"Ref": "Role"
},
"ARC_CLOUDFORMATION": {
"Ref": "AWS::StackName"
},
"ARC_APP_NAME": "test-cors",
"ARC_HTTP": "aws_proxy",
"NODE_ENV": "staging",
"SESSION_TABLE_NAME": "jwe"
}
},
"Role": {
"Fn::Sub": [
"arn:aws:iam::${AWS::AccountId}:role/${roleName}",
{
"roleName": {
"Ref": "Role"
}
}
]
},
"Events": {
"GetIndexEvent": {
"Type": "HttpApi",
"Properties": {
"Path": "/",
"Method": "GET",
"ApiId": {
"Ref": "TestCorsApi"
}
}
}
}
}
},
"StaticBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": "PublicRead",
"WebsiteConfiguration": {
"IndexDocument": "index.html",
"ErrorDocument": "404.html"
}
}
},
"Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
},
"Policies": [
{
"PolicyName": "ArcGlobalPolicy",
"PolicyDocument": {
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
}
]
}
},
"GetStatic": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"CodeUri": "/Users/thomas/dev/oss/arc/arc-macro-cors/node_modules/@architect/http-proxy/dist",
"Runtime": "nodejs12.x",
"MemorySize": 1152,
"Timeout": 5,
"Environment": {
"Variables": {
"ARC_ROLE": {
"Ref": "Role"
},
"ARC_CLOUDFORMATION": {
"Ref": "AWS::StackName"
},
"ARC_APP_NAME": "test-cors",
"ARC_HTTP": "aws_proxy",
"NODE_ENV": "staging",
"SESSION_TABLE_NAME": "jwe",
"ARC_STATIC_SPA": "false"
}
},
"Role": {
"Fn::Sub": [
"arn:aws:iam::${AWS::AccountId}:role/${roleName}",
{
"roleName": {
"Ref": "Role"
}
}
]
},
"Events": {
"GetStaticEvent": {
"Type": "HttpApi",
"Properties": {
"Path": "/_static/{proxy+}",
"Method": "GET",
"ApiId": {
"Ref": "TestCorsApi"
}
}
}
}
}
},
"TestCorsApi": {
"Type": "AWS::Serverless::HttpApi",
"Properties": {
"FailOnWarnings": true,
"CorsConfiguration": {
"AllowOrigins": [
"https://example.com"
],
"AllowHeaders": [
"Authorization",
"X-Custom-Header"
],
"ExposeHeaders": [
"Content-Length",
"X-Custom-Header"
],
"AllowMethods": [
"GET",
"POST",
"OPTIONS"
],
"MaxAge": 7200,
"AllowCredentials": true
}
}
}
},
"Outputs": {
"BucketURL": {
"Description": "Bucket URL",
"Value": {
"Fn::Sub": [
"http://${bukkit}.s3-website-${AWS::Region}.amazonaws.com",
{
"bukkit": {
"Ref": "StaticBucket"
}
}
]
}
},
"HTTP": {
"Description": "API Gateway",
"Value": {
"Fn::Sub": [
"https://${idx}.execute-api.${AWS::Region}.amazonaws.com",
{
"idx": {
"Ref": "TestCorsApi"
}
}
]
}
}
}
}
5 changes: 5 additions & 0 deletions src/http/get-foo/.arc-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@aws
runtime nodejs12.x
# memory 1152
# timeout 30
# concurrency 1
Loading

0 comments on commit b08a596

Please sign in to comment.