Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,39 @@ It will also auto-generate user votes over time for the questions there.

If you're curious about the technologies used in the server and client,
see their respective `README.md` files.

To run tests against a DynamoDB instance running [locally](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html), make sure
you got [`docker`](https://docs.docker.com/engine/install/) and
[`AWS CLI`](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions) installed, then hit:

```console
$ cd server
$ ./run-dynamodb-local.sh
```

This will also spin a [Web UI](https://github.com/aaronshaf/dynamodb-admin?tab=readme-ov-file)
for your local DynamoDB instance.

You can now run tests with:

```sh
AWS_DEFAULT_REGION=us-east-1 \
AWS_ACCESS_KEY_ID=lorem \
AWS_SECRET_ACCESS_KEY=ipsum \
AWS_ENDPOINT_URL=http://localhost:8000 \
Comment on lines +49 to +52
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we set it up so that there's just one environment variable to set (like USE_DYNAMODB=test), and when it's set like that then we set the others accordingly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this setup is possible: we will need to check the USE_DYNAMODB variable and, if it is set to test, construct the config accordingly using the builder. Let me look into into it.

cargo t -- --ignored
```

Assuming you are staying in the `server` directory, to run the back-end application against
your local DynamoDB instance, hit:

```sh
AWS_DEFAULT_REGION=us-east-1 \
AWS_ACCESS_KEY_ID=lorem \
AWS_SECRET_ACCESS_KEY=ipsum \
USE_DYNAMODB=1 \
AWS_ENDPOINT_URL=http://localhost:8000 \
cargo run
```

Note that you can omit setting those dummy values for `AWS_DEFAULT_REGION`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` in case you've got AWS profile(s) [configured](https://docs.aws.amazon.com/cli/latest/reference/configure/) on your workstation.
4 changes: 3 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target
/target
/dynamodb-data
Makefile
85 changes: 85 additions & 0 deletions server/run-dynamodb-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bash

if ! [ -x "$(command -v docker)" ]; then
echo '❌ Error: please make sure docker is installed and is in the PATH' >&2
exit 1
fi

if ! [ -x "$(command -v aws)" ]; then
echo '❌ Error: please make sure AWS CLI is installed and is in the PATH' >&2
exit 1
fi

# AWS CLI wants us to either run `aws configure` or provide the three essential variables
# from the environment. Most of us will have aws profile(s) configured on the workstation,
# but this should not be a requirement to be able to spin up and query a DynamoDB Local
# instance. This is why we are setting those variables in the current shell. Note that
# the credential values themselves do not matter, it is rather the fact that they _are_ set.
#
# see: https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-envvars.html#envvars-set
export AWS_ACCESS_KEY_ID=lorem
export AWS_SECRET_ACCESS_KEY=ipsum
export AWS_DEFAULT_REGION=us-east-1

DYNAMODB_CONTAINER_NAME=dynamodb-local
DYNAMODB_ADMIN_CONTAINER_NAME=dynamodb-admin
ENDPOINT_URL=http://localhost:8000

docker ps | grep ${DYNAMODB_CONTAINER_NAME} >/dev/null &&
echo "🚫 Container \"${DYNAMODB_CONTAINER_NAME}\" with DynamoDB Local service is already running." && exit 0

echo "🖴 Preparing volumes for DynamoDB..."
rm -rf dynamodb-data
mkdir dynamodb-data

echo "🚀 Spinning up a container with DynamoDB..."
(
docker run --rm -d -v ./dynamodb-data:/home/dynamodblocal/data -p 127.0.0.1:8000:8000 \
-w /home/dynamodblocal --name ${DYNAMODB_CONTAINER_NAME} amazon/dynamodb-local:latest \
-jar DynamoDBLocal.jar -sharedDb -dbPath ./data
) >/dev/null

while ! (AWS_ACCESS_KEY_ID=lorem AWS_SECRET_ACCESS_KEY=ipsum aws dynamodb list-tables --endpoint-url ${ENDPOINT_URL} --region us-east-1 >/dev/null); do
echo "⏳ Waiting for the database to start accepting connections..."
done

echo "🗒️ Creating 'events' table..."
aws dynamodb create-table \
--table-name events \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--endpoint-url ${ENDPOINT_URL} >/dev/null

aws dynamodb update-time-to-live \
--table-name events \
--time-to-live-specification Enabled=true,AttributeName=expire \
--endpoint-url ${ENDPOINT_URL} >/dev/null

echo "🗒️ Creating 'questions' table and 🚄 GSI..."
aws dynamodb create-table \
--table-name questions \
--attribute-definitions AttributeName=id,AttributeType=S \
AttributeName=eid,AttributeType=S \
AttributeName=votes,AttributeType=N \
--key-schema AttributeName=id,KeyType=HASH \
--global-secondary-indexes 'IndexName=top,KeySchema=[{AttributeName=eid,KeyType=HASH},{AttributeName=votes,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[answered,hidden]}' \
--billing-mode PAY_PER_REQUEST \
--endpoint-url ${ENDPOINT_URL} >/dev/null

aws dynamodb update-time-to-live \
--table-name questions \
--time-to-live-specification Enabled=true,AttributeName=expire \
--endpoint-url ${ENDPOINT_URL} >/dev/null

echo "✅ Container \"${DYNAMODB_CONTAINER_NAME}\" with DynamoDB Local is ready!"

docker ps | grep ${DYNAMODB_ADMIN_CONTAINER_NAME} >/dev/null &&
echo "🚫 Container "${DYNAMODB_ADMIN_CONTAINER_NAME}" with DynamoDB Admin service is already running." &&
exit 0

echo "🚀 Spinning up a container with DynamoDB Admin..."
(docker run -d --rm --net host --name ${DYNAMODB_ADMIN_CONTAINER_NAME} aaronshaf/dynamodb-admin) >/dev/null
echo "🔎 DynamoDB Admin is available at http://localhost:8001"

echo "✅ Done!"
11 changes: 4 additions & 7 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ async fn main() -> Result<(), Error> {
.with_env_filter(EnvFilter::from_default_env())
.without_time(/* cloudwatch does that */).init();

#[cfg(debug_assertions)]
let backend = {
let backend = if !cfg!(debug_assertions) || std::env::var_os("USE_DYNAMODB").is_some() {
let config = aws_config::load_from_env().await;
Backend::Dynamo(aws_sdk_dynamodb::Client::new(&config))
} else {
use rand::prelude::SliceRandom;
use serde::Deserialize;
use std::time::Duration;
Expand Down Expand Up @@ -202,11 +204,6 @@ async fn main() -> Result<(), Error> {
});
state
};
#[cfg(not(debug_assertions))]
let backend = {
let config = aws_config::load_from_env().await;
Backend::Dynamo(aws_sdk_dynamodb::Client::new(&config))
};

let app = Router::new()
.route("/api/event", post(new::new))
Expand Down