A community fridge is a decentralized resource where businesses and individuals can donate perishable food. There are dozens of fridges hosted by volunteers across the country.
Fridge Finder is project sponsored by Collective Focus, a community organization in Brooklyn, New York. Our goal is to make it easy for people to find fridge locations and get involved with food donation programs in their community. We are building a responsive, mobile first, multi-lingual web application with administrative controls for fridge maintainers. To join the project read our contributing guidelines and code of conduct. The application will be deployed to https://www.fridgefinder.app/
- AWS CLI - Install the AWS CLI
- You DO NOT have to create an AWS account to use AWS CLI for this project, skip these steps if you don't want to create an AWS account
- AWS CLI looks for credentials when using it, but doesn't validate. So will need to set some fake one. But the region name matters, use any valid region name.
$ aws configure $ AWS Access Key ID: [ANYTHING YOU WANT] $ AWS Secret Access Key: [ANYTHING YOUR HEART DESIRES] $ Default region nam: us-east-1 $ Default output format [None]: (YOU CAN SKIP)
- SAM CLI - Install the SAM CLI
- You DO NOT need to create an aws account to use SAM CLI for this project, skip these steps if you don't want to create an aws account
- Python 3 - Install Python 3
- Docker - Install Docker
Guide that was used: https://betterprogramming.pub/how-to-deploy-a-local-serverless-application-with-aws-sam-b7b314c3048c
Follow these steps to get Dynamodb running locally
- Start a local DynamoDB service
$ docker compose up # OR if you want to run it in the background: $ docker compose up -d - Create tables
$ ./scripts/create_local_dynamodb_tables.py
cd CommunityFridgeMapApi/sam build --use-container- Load data into your local Dynamodb tables
- Fridge Data:
sam local invoke LoadFridgeDataFunction --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network
- Fridge Data:
- Get data from your local Dynamodb tables
aws dynamodb scan --table-name fridge_dev --endpoint-url http://localhost:4566
Confirm that the following requests work for you
cd CommunityFridgeMapApi/sam build --use-containersam local invoke HelloWorldFunction --event events/event.json- response:
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}
- response:
sam local start-apicurl http://localhost:3000/hello- response:
{"message": "hello world"}
- response:
Choose your favorite API platform for using APIs. Recommend: https://www.postman.com/
- POST Fridge:
sam local invoke FridgesFunction --event events/local-post-fridge-event.json --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network - GET Fridges:
sam local invoke FridgesFunction --event events/local-event-get-fridges.json --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network - GET Fridges Filter By Tag:
sam local invoke FridgesFunction --event events/local-event-get-fridges-with-tag.json --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network
- Start Server:
sam local start-api --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network - GET Fridges: Go to http://localhost:3000/v1/fridges
- GET Fridge: Go to http://localhost:3000/v1/fridges/{fridgeId}
- Get Fridges Filter By Tag: http://localhost:3000/v1/fridges?tag={TAG}
- POST Fridge Example:
curl --location --request POST 'http://localhost:3000/v1/fridges' --header 'Content-Type: application/json' --data-raw '{
"name": "LES Community Fridge #2",
"verified": false,
"location": {
"name": "testing",
"street": "466 Grand Street",
"city": "New York",
"state": "NY",
"zip": "10002",
"geoLat": 40.715207,
"geoLng": -73.983748
},
"maintainer": {
"name": "name",
"organization": "org",
"phone": "1234567890",
"email": "test@test.com",
"instagram": "https://www.instagram.com/les_communityfridge",
"website": "https://linktr.ee/lescommunityfridge"
},
"notes": "notes",
"photoUrl": "url.com"
}'
- POST FridgeReport:
sam local invoke FridgeReportFunction --event events/local-fridge-report-event.json --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network- [OPTIONAL] Generate custom event Example:
sam local generate-event apigateway aws-proxy --method POST --path document --body "{\"condition\": \"working\", \"foodPercentage\": 0}" > events/local-fridge-report-event-2.json- Add
"fridgeId": "{FRIDGEID}"to pathParameter in generated file
- Add
- [OPTIONAL] Generate custom event Example:
- Query Data:
aws dynamodb scan --table-name fridge_report_dev --endpoint-url http://localhost:4566
- Start Server:
sam local start-api --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network - Make a POST Request to:
http://127.0.0.1:3000/v1/fridges/{fridgeId}/reports- Example:
curl --location --request POST 'http://127.0.0.1:3000/v1/fridges/thefriendlyfridge/reports' --header 'Content-Type: application/json' --data-raw '{"condition": "good", "foodPercentage": 1}'
- Example:
- Start local SAM API
sam local start-api --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network - Upload image (replace
<file-path>with your actual image path like"@/home/user/Downloads/sample.webp")curl --request POST \ --url http://localhost:3000/v1/photo \ --header 'Content-Type: image/webp' \ --data-binary "@<file-path>"
Tests are defined in the tests folder in this project. Create Virtual Environment, use PIP to install the test dependencies, and run tests.
CFM_BACKEND$ cd CommunityFridgeMapApi
# Create and activate virtual environment
CommunityFridgeMapApi$ python3 -m venv myenv
CommunityFridgeMapApi$ source myenv/bin/activate
# Install dependencies (note: virtual environment is now active)
(myenv) CommunityFridgeMapApi$ pip install -r tests/requirements.txt
# Run unit tests
(myenv) CommunityFridgeMapApi$ python -m pytest tests/unit -v
# To test with coverage
(myenv) coverage run -m pytest tests/unit -v
(myenv) coverage report
(myenv) coverage html
#MacOs:
(myenv) open -a "Google Chrome" htmlcov/index.html
#Windows:
(myenv) start "Google Chrome" htmlcov/index.html
# When finished, deactivate virtual environment
(myenv) CommunityFridgeMapApi$ deactivate
CommunityFridgeMapApi$sam validate -t template.yamlsam build --use-container- Use this command before running the backend if you updated the code
sam local generate-event apigateway aws-proxy --method GET --path document --body "" > local-event.json- Use this command to generate a REST API event
aws dynamodb scan --table-name fridge_{stage} --endpoint-url http://localhost:4566aws dynamodb scan --table-name fridge_report_{stage} --endpoint-url http://localhost:4566
CFM_BACKEND$ bash .git/hooks/pre-commitProject Documentation