This application is dockerized. Take a look at Dockerfile to see how it works.
A very simple docker-compose.yml has been included to support local development and deployment.
-
Install the version of Python specified in pyproject.toml pyenv is one popular option for installing Python, or asdf.
- If using pyenv run
pyenv local <version>
to ensure that version will be used in subsequent steps
- If using pyenv run
-
Ensure that
python -V
andpython3 -V
are picking up that version.- If not, run
pyenv init -
and/or restart your shell to ensure it was run automatically
- If not, run
-
After installing and activating the right version of Python, install poetry and follow the instructions to add poetry to your path if necessary.
curl -sSL https://install.python-poetry.org | python3 -
-
You'll also need Docker Desktop
- If you haven't done local development before you'll need to execute the migrations and seed the DB with data using the steps in database-local-usage.md
- Run
make init-opensearch
setup the OpenSearch Container - Run
make populate-search-opportunities
to push data previously seeded in the DB into the search index
If your DB or OpenSearch end up in an odd place, you can reset all the persistent storage using make volume-recreate
- Make sure you have Docker Desktop installed & running.
- Run
make setup-local
to install dependencies - Run
make init start
to build the image and start the container. - Navigate to
localhost:8080/docs
to access the Swagger UI. - Run
make run-logs
to see the logs of the running API container - Run
make stop
when you are done to delete the container.
make test
will run all of the tests. Additional arguments can be passed to this command which will be passed to pytest like so: make test args="tests/api/route -v"
which would run all tests in the route folder with verbosity increased. See the Pytest Docs for more details on CLI flags you can set.
make clean-volumes
will spin down the docker containers + delete the volumes.
make volume-recreate
Deletes the volumes and then re-initializes the persistant portions of the stack. This can be useful to reset your DB, or fix any bad states your local environment may have gotten into.
See the Makefile for a full list of commands you can run.
The make console
command initializes a Python REPL environment pre-configured with database connectivity. This allows developers to perform database queries, utilize factories for data generation, and interact with the application's models directly.
- Writing a query:
dbs.query(Opportunity).all()
- Saving some factory generated data to the db:
f.OpportunityFactory.create()
Several components like tests, linting, and scripts can be run either inside of the Docker container, or outside on your native machine. Running in Docker is the default, but on some machines like the M1 Mac, running natively may be desirable for performance reasons.
You can switch which way many of these components are run by setting the PY_RUN_APPROACH
env variable in your terminal.
export PY_RUN_APPROACH=local
will run these components nativelyexport PY_RUN_APPROACH=docker
will run these within Docker
Note that even with the native mode, many components like the DB and API will only ever run in Docker, and you should always make sure that any implementations work within docker.
Running in the native/local approach may require additional packages to be installed on your machine to get working.
Most configuration options are managed by environment variables.
Environment variables for local development are stored in the local.env file. This file is automatically loaded when running. If running within Docker, this file is specified as an env_file
in the docker-compose file, and loaded by a script automatically when running most other components outside the container.
Any environment variables specified directly in the docker-compose file will take precedent over those specified in the local.env file.
This API uses a very simple ApiKey authentication approach which requires the caller to provide a static key. This is specified with the API_AUTH_TOKEN
environment variable.
The API can be run in debug mode that allows for remote attach debugging (currently only supported from VSCode) to the container.
-
Requirements:
- VSCode Python extension
- Updated Poetry with the
debugpy
dev package inpyproject.toml
-
See
./vscode/launch.json
which has the debug config. (NamedAPI Remote Attach
) -
Start the server in debug mode via
make start-debug
ormake start-debug run-logs
.- This will start the
main-app
service with port 5678 exposed.
- This will start the
-
The server will start in waiting mode, waiting for you to attach the debugger (see
/src/app.py
) before continuing to run. -
Go to your VSCode debugger window and run the
API Remote Attach
option -
You should now be able to hit set breakpoints throughout the API
Now that you're up and running, read the application docs to familiarize yourself with the application.