Giter VIP home page Giter VIP logo

cfm_backend's Introduction

CFM_Backend

Community Fridge Map

GitHub contributors GitHub commit activity (dev)

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/


Pre-Requisites

  1. 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)
  2. 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
  3. Python 3 - Install Python 3
  4. Docker - Install Docker

Setup Local Database Connection

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

  1. Start a local DynamoDB service
    $ docker compose up
    # OR if you want to run it in the background:
    $ docker compose up -d
  2. Create tables
    $ ./scripts/create_local_dynamodb_tables.py
  3. cd CommunityFridgeMapApi/
  4. sam build --use-container
  5. Load data into your local Dynamodb tables
    1. Fridge Data: sam local invoke LoadFridgeDataFunction --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network
  6. Get data from your local Dynamodb tables
    1. aws dynamodb scan --table-name fridge_dev --endpoint-url http://localhost:4566

Build and Test Locally

Confirm that the following requests work for you

  1. cd CommunityFridgeMapApi/
  2. sam build --use-container
  3. sam local invoke HelloWorldFunction --event events/event.json
    • response: {"statusCode": 200, "body": "{\"message\": \"hello world\"}"}
  4. sam local start-api
  5. curl http://localhost:3000/hello
    • response: {"message": "hello world"}

API

Choose your favorite API platform for using APIs. Recommend: https://www.postman.com/

Fridge

One Time Use

  1. 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
  2. GET Fridge: sam local invoke FridgesFunction --event events/local-event-get-fridge.json --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network
  3. 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
  4. 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

Local Server

  1. Start Server: sam local start-api --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network
  2. GET Fridge: Go to http://localhost:3000/v1/fridges/{fridgeId}
  3. GET Fridges: Go to http://localhost:3000/v1/fridges
  4. Get Fridges Filter By Tag: http://localhost:3000/v1/fridges?tag={TAG}
  5. POST Fridge Example:
curl --location --request POST 'http://127.0.0.1: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": "[email protected]",
        "instagram": "https://www.instagram.com/les_communityfridge",
        "website": "https://linktr.ee/lescommunityfridge"
    },
    "notes": "notes",
    "photoUrl": "url.com"
}'

Fridge Report

One Time Use

  1. 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
  2. Query Data: aws dynamodb scan --table-name fridge_report_dev --endpoint-url http://localhost:8000

Local Server

  1. Start Server: sam local start-api --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network
  2. 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}'

Image

  1. Start local SAM API sam local start-api --parameter-overrides ParameterKey=Environment,ParameterValue=local ParameterKey=Stage,ParameterValue=dev --docker-network cfm-network
  2. 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

Tests are defined in the tests folder in this project. Use PIP to install the test dependencies and run tests.

CFM_BACKEND$ cd CommunityFridgeMapApi
CommunityFridgeMapApi$ pip install -r tests/requirements.txt --user
# unit test
CommunityFridgeMapApi$ python -m pytest tests/unit -v

To test with coverage

CommunityFridgeMapApi$ coverage run -m pytest tests/unit -v
CommunityFridgeMapApi$ coverage report
CommunityFridgeMapApi$ coverage html

MacOS:

CommunityFridgeMapApi$ open -a "Google Chrome" htmlcov/index.html

Windows:

CommunityFridgeMapApi$ start "Google Chrome" htmlcov/index.html

Useful AWS SAM commands

  1. sam validate -t template.yaml
  2. sam build --use-container
    • Use this command before running the backend if you updated the code
  3. sam local generate-event apigateway aws-proxy --method GET --path document --body "" > local-event.json
    • Use this command to generate a REST API event

Useful Dynamodb Commands

  1. aws dynamodb scan --table-name fridge_{stage} --endpoint-url http://localhost:4566
  2. aws dynamodb scan --table-name fridge_report_{stage} --endpoint-url http://localhost:4566

Useful formatting Command

CFM_BACKEND$ bash .git/hooks/pre-commit

Resources

Project Documentation

cfm_backend's People

Contributors

1jeanpaul1 avatar jaronaearle avatar collectivefocus avatar abhinav1912 avatar 0916dhkim avatar sbeesmiley avatar mischbee avatar bernardm avatar

Stargazers

SKZ avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.