Skip to content

Commit

Permalink
Update docker-compose.yml to include AWS configuration for S3 and Dyn…
Browse files Browse the repository at this point in the history
…amoDB, updated post and get routes to fix location header
  • Loading branch information
brokoli777 committed Jul 23, 2024
1 parent 0cd5538 commit 2dec181
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 9 deletions.
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ services:
- HTPASSWD_FILE=tests/.htpasswd
# Use the LOG_LEVEL set in the host environment, or default to info
- LOG_LEVEL=${LOG_LEVEL:-info}
- AWS_REGION=us-east-1
# Use the LocalStack endpoint vs. AWS for S3 AWS SDK clients.
# NOTE: we use Docker's internal network to the localstack container
- AWS_S3_ENDPOINT_URL=http://localstack:4566
# Use the DynamoDB local endpoint vs. AWS for DynamoDB AWS SDK clients.
- AWS_DYNAMODB_ENDPOINT_URL=http://dynamodb-local:8000
# This S3 bucket and DynamoDB table need to get created first, see
# local-aws-setup.sh. We'll default to 'fragments' as the name, unless
# something else is defined in the env.
- AWS_S3_BUCKET_NAME=${AWS_S3_BUCKET_NAME:-fragments}
- AWS_DYNAMODB_TABLE_NAME=${AWS_DYNAMODB_TABLE_NAME:-fragments}
# Ports to publish
ports:
- '8080:8080'
Expand All @@ -29,3 +40,18 @@ services:
# Run the database in memory, see:
# https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html
command: ['-jar', 'DynamoDBLocal.jar', '-inMemory']

# LocalStack for S3, see https://docs.localstack.cloud/get-started/#docker-compose
# Interact via awscli-local, see https://docs.localstack.cloud/integrations/aws-cli/#installation
localstack:
# https://hub.docker.com/r/localstack/localstack
image: localstack/localstack
ports:
- '4566:4566'
environment:
# See https://docs.localstack.cloud/localstack/configuration/ and
# https://hub.docker.com/r/localstack/localstack for config details.
# We only want to run S3
- SERVICES=s3
# We're always working in us-east-1
- DEFAULT_REGION=us-east-1
49 changes: 49 additions & 0 deletions scripts/local-aws-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh

# Setup steps for working with LocalStack and DynamoDB local instead of AWS.
# Assumes aws cli is installed and LocalStack and DynamoDB local are running.

# Setup AWS environment variables
echo "Setting AWS environment variables for LocalStack"

echo "AWS_ACCESS_KEY_ID=test"
export AWS_ACCESS_KEY_ID=test

echo "AWS_SECRET_ACCESS_KEY=test"
export AWS_SECRET_ACCESS_KEY=test

echo "AWS_SESSION_TOKEN=test"
export AWS_SESSION_TOKEN=test

export AWS_DEFAULT_REGION=us-east-1
echo "AWS_DEFAULT_REGION=us-east-1"

# Wait for LocalStack to be ready, by inspecting the response from healthcheck
echo 'Waiting for LocalStack S3...'
until (curl --silent http://localhost:4566/_localstack/health | grep "\"s3\": \"\(running\|available\)\"" > /dev/null); do
sleep 5
done
echo 'LocalStack S3 Ready'

# Create our S3 bucket with LocalStack
echo "Creating LocalStack S3 bucket: fragments"
aws --endpoint-url=http://localhost:4566 s3api create-bucket --bucket fragments

# Setup DynamoDB Table with dynamodb-local, see:
# https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/getting-started-step-1.html
echo "Creating DynamoDB-Local DynamoDB table: fragments"
aws --endpoint-url=http://localhost:8000 \
dynamodb create-table \
--table-name fragments \
--attribute-definitions \
AttributeName=ownerId,AttributeType=S \
AttributeName=id,AttributeType=S \
--key-schema \
AttributeName=ownerId,KeyType=HASH \
AttributeName=id,KeyType=RANGE \
--provisioned-throughput \
ReadCapacityUnits=10,WriteCapacityUnits=5

# Wait until the Fragments table exists in dynamodb-local, so we can use it, see:
# https://awscli.amazonaws.com/v2/documentation/api/latest/reference/dynamodb/wait/table-exists.html
aws --endpoint-url=http://localhost:8000 dynamodb wait table-exists --table-name fragments
2 changes: 1 addition & 1 deletion src/routes/api/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = (req, res, next) => {
} else {

// Return the data with original content type it had
res.status(200).send(data.toString());
res.status(200).header('Content-Type', fragment.mimeType).send(data.toString());
}
});
})
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = (req, res) => {
fragment.save().then(() => {

//Setting Location Header
const hostName = process.env.API_URL || req.headers.host;
const hostName = req.headers.host;
const locationURL = new URL("/v1/fragments/" + fragment.id, `http://${hostName}`);
res.location(locationURL.toString());

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/404.hurl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Check if
#Check if invalid route returns 404 response
GET http://localhost:8080/no-such-route

#We should get a 404 response
HTTP/1.0 404
HTTP/1.1 404
6 changes: 3 additions & 3 deletions tests/integration/post-fragments-json.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Content-Type: application/json
[BasicAuth]
user1@email.com:password1

`{"key": "value"}`
`{"hi": "something"}`

# We expect to get back an HTTP 201
HTTP/1.1 201
Expand All @@ -24,6 +24,6 @@ GET {{url}}
user1@email.com:password1

HTTP/1.1 200
Content-Type: application/json
Content-Type: application/json; charset=utf-8
[Asserts]
body == '{"key": "value"}'
body == "{\"hi\": \"something\"}"
2 changes: 1 addition & 1 deletion tests/integration/post-fragments.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ GET {{url}}
user1@email.com:password1

HTTP/1.1 200
Content-Type: text/plain
Content-Type: text/plain; charset=utf-8
Content-Length: 19
[Asserts]
body == "This is a fragment!"
2 changes: 1 addition & 1 deletion tests/unit/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('POST /v1/fragments', () => {
request(app).post('/v1/fragments').auth('invalid@email.com', 'incorrect_password').expect(401));

// Using a valid username/password pair should save a new fragment and return a successful response
test('authenticated users get a fragments array', async () => {
test('authenticated users get successful response when creating valid fragment', async () => {
const res = await request(app)
.post('/v1/fragments')
.auth('user1@email.com', 'password1')
Expand Down

0 comments on commit 2dec181

Please sign in to comment.