From 0b8a57d3a13bb517920f01f8b444122d54f3acde Mon Sep 17 00:00:00 2001 From: Jessica Gadling Date: Thu, 17 Aug 2023 16:57:37 -0700 Subject: [PATCH] [chore] Support developing and debugging in a docker container (#31) * Our main branch is called main. * Support debugging and developing in docker. * Update readme. * fix --- .github/workflows/entities-push-tests.yml | 2 +- entities/.devcontainer/devcontainer.json | 23 +++++++++++++++++++++++ entities/.vscode/launch.json | 21 +++++++++++++++++++++ entities/.vscode/tasks.json | 15 +++++++++++++++ entities/README.md | 9 +++++++++ entities/api/main.py | 16 +++++----------- 6 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 entities/.devcontainer/devcontainer.json create mode 100644 entities/.vscode/launch.json create mode 100644 entities/.vscode/tasks.json diff --git a/.github/workflows/entities-push-tests.yml b/.github/workflows/entities-push-tests.yml index 880a97f6..3eab6902 100644 --- a/.github/workflows/entities-push-tests.yml +++ b/.github/workflows/entities-push-tests.yml @@ -3,7 +3,7 @@ name: Entities tests on: push: branches: - - trunk + - main paths: - "entities/**" pull_request: diff --git a/entities/.devcontainer/devcontainer.json b/entities/.devcontainer/devcontainer.json new file mode 100644 index 00000000..8cc399f2 --- /dev/null +++ b/entities/.devcontainer/devcontainer.json @@ -0,0 +1,23 @@ +{ + "name": "Entities devcontainer", + "dockerComposeFile": [ + "../docker-compose.yml" + ], + "service": "entities", + "workspaceFolder": "/czid-platformics/entities", + "shutdownAction": "echo shutting down", + "customizations": { + "vscode": { + "settings": { + "extensions.verifySignature": false + }, + "extensions": [ + "ms-python.python", + "ms-python.vscode-pylance", + "ms-python.black-formatter", + "matangover.mypy", + "GitHub.copilot" + ] + } + } +} diff --git a/entities/.vscode/launch.json b/entities/.vscode/launch.json new file mode 100644 index 00000000..f1394e3d --- /dev/null +++ b/entities/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Entities", + "type": "python", + "request": "launch", + "module": "api.main", + // Stop the normal webserver so we can start a new one attached to our debugger. + "preLaunchTask": "stopservices", + // Turn the normal server back on when we're done debugging. + "postDebugTask": "startservices", + "jinja": true, + "justMyCode": true + } + ] +} + diff --git a/entities/.vscode/tasks.json b/entities/.vscode/tasks.json new file mode 100644 index 00000000..0713234d --- /dev/null +++ b/entities/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "stopservices", + "command": "supervisorctl", + "args": ["-c", "/czid-platformics/entities/etc/supervisord.conf", "stop", "all"] + }, + { + "label": "startservices", + "command": "supervisorctl", + "args": ["-c", "/czid-platformics/entities/etc/supervisord.conf", "start", "all"] + } + ] +} diff --git a/entities/README.md b/entities/README.md index c6cb8cb2..46ff689b 100644 --- a/entities/README.md +++ b/entities/README.md @@ -31,3 +31,12 @@ docker compose exec entities bash export PLATFORMICS_AUTH_TOKEN=$(./cli/gqlcli.py auth generate-token 111 --project 444:admin --expiration 3600) ./cli/gqlcli.py samples list ``` + +### Debugging in VSCode: +- Install the 'Dev Containers' and 'Docker' VSCode extensions +- Open VSCode at the entities directory +- Click "reopen in container" when VSCode suggests it. +- You're now editing code directly in the container! This is handy because all of the python packages used by the app are installed in the container and type checking will work properly. +- You can set breakpoints and click the "debug/play" icon in VSCode to step through your code. + - **Note** that you'll generally have to make a request (via cli/browser/???) to actually trigger the section of code you're debugging. + diff --git a/entities/api/main.py b/entities/api/main.py index 55f3af08..65c19196 100644 --- a/entities/api/main.py +++ b/entities/api/main.py @@ -1,23 +1,17 @@ import typing -from database.connect import AsyncDB import database.models as db import strawberry import uvicorn from cerbos.sdk.client import CerbosClient from cerbos.sdk.model import Principal +from database.connect import AsyncDB from fastapi import Depends, FastAPI from strawberry.fastapi import GraphQLRouter -from thirdparty.strawberry_sqlalchemy_mapper import ( - StrawberrySQLAlchemyMapper, -) -from api.core.gql_loaders import EntityLoader, get_base_loader +from thirdparty.strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper -from api.core.deps import ( - get_auth_principal, - get_cerbos_client, - get_engine, -) +from api.core.deps import get_auth_principal, get_cerbos_client, get_engine +from api.core.gql_loaders import EntityLoader, get_base_loader from api.core.settings import APISettings ###################### @@ -95,6 +89,6 @@ def get_app() -> FastAPI: app = get_app() if __name__ == "__main__": - config = uvicorn.Config("example:app", host="0.0.0.0", port=8008, log_level="info") + config = uvicorn.Config("api.main:app", host="0.0.0.0", port=8008, log_level="info") server = uvicorn.Server(config) server.run()