BASE Backend APIs Setup Guide
Introduction
This documentation provides a comprehensive guide to setting up a local development environment for the BASE Main API. It includes instructions for repository setup, environment configuration, running the application, troubleshooting, and additional utilities.
Getting Started
Prerequisites
Before deploying, ensure that you have the following installed on your system:
- Git – to clone the repository.
- Docker and Docker Compose – for containerized development.
- Pipenv – to manage your Python virtual environment and dependencies.
Pipenv and Dependency Management
Pipenv is used for managing project dependencies, ensuring consistent environments across development, testing, and production. It combines the functionality of pip
and virtualenv
, streamlining the process of dependency management.
Pipfile
andPipfile.lock
:- The
Pipfile
lists the project's dependencies. - The
Pipfile.lock
file contains the exact versions of each dependency, including sub-dependencies, creating a deterministic build. This lock file is crucial for reproducible builds, preventing issues caused by version mismatches.
- The
- Installation:
- After installing Pipenv, use
pipenv install
to create a virtual environment and install the dependencies specified in thePipfile.lock
. - If
Pipfile.lock
is absent, Pipenv will resolve the dependencies fromPipfile
and create the lock file.
- After installing Pipenv, use
IDE Setup and Python Language Server Protocol (LSP)
For optimal development, it is recommended to use an Integrated Development Environment (IDE) that supports the Python Language Server Protocol (LSP).
- Python LSP:
- The Python LSP provides features like code completion, error checking, go-to-definition, and refactoring, enhancing developer productivity.
- It enables real-time analysis of Python code, providing instant feedback and improving code quality.
- Recommended IDEs:
- Visual Studio Code (VS Code) with the Python extension.
- PyCharm.
- Sublime Text with LSP plugins.
- Any IDE supporting LSP and having a good python language server implementation.
Cloning the Repository
Clone the repository along with its submodules:
git clone --recurse-submodules https://gitlab.com/b1866/coldtivate/backend-monorepo
cd backend-monorepo
Configuring the Docker Environment
Several Docker Compose files define different environment configurations:
docker-compose.yml
: Base configuration shared across all environments.docker-compose.dev.yml
: Development-specific settings.docker-compose.prod.yml
: Production-specific settings.docker-compose.override.yml
: Custom override configuration that supplements or modifies the base settings.
To inspect the final configuration after merging, use:
For more details, refer to the Docker Compose documentation.
Choosing an Environment
To set up an environment, copy the contents of the desired configuration file into docker-compose.override.yml
.
Setting Up the Application Environment
Environment Variables
The Django application requires environment files to manage settings:
.env.dev
for development.env.prod
for production
These files are not included in the repository. To obtain them, contact a project team member. Alternatively, use .env.dev_example
as a template and modify it as needed.
Running the Application with Docker
Building and Starting the Application
Ensure Docker and Docker Compose are installed, then run:
Applying Database Migrations
Execute database migrations with:
Setting Up Initial Data
Create system roles:
Load initial fixture data:
docker-compose run web pipenv run python manage.py loaddata countries user crops storage operation markets
Create a superuser account:
Troubleshooting
Common Issues
- Cloning errors: Ensure you have the correct repository and submodule access.
- Build failures: Try running
docker system prune
before rebuilding. If issues persist, rebuild without using the cache:
Resetting the Database
Caution: This action will permanently delete all database data.
Stop all running containers:
Identify the database volume:
Remove the corresponding database volume:
Follow the setup steps in the "Running the Application with Docker" section to recreate the database.
Additional Utilities
Running Tests
Execute tests inside the container:
Accessing the Django Shell
To open an interactive Django shell:
Running Celery Tasks Manually
Execute Celery tasks inside the container:
docker-compose run celery pipenv run python manage.py shell --command="from base.apps.prediction.tasks.ml4 import prediction_calls, india_markets_data_db_insert; india_markets_data_db_insert(); prediction_calls();"
Running Python Scripts in Containers
Execute a Python script inside a running container:
Accessing the Application
Once the application is running, it will be available at:
- API Base URL: http://localhost:8000
- Django Admin Panel: http://localhost:8000/admin
Notes & Best Practices
- Regularly update dependencies and rebuild containers when necessary.
- Monitor container logs using:
- Ensure
.env
files remain secure and are never committed to the repository. - Consider setting up automated backups for database persistence.
For further assistance, refer to the official Django and Docker documentation.
Alternative Setup for Local Debugging
The following scripts facilitate deployment and local testing of the monorepo services, independent of the platform and without requiring extensive local setup.
-
./scripts/environment-deployment-management.sh deploy
: It's typically used to set up a complete working instance. -
./scripts/environment-deployment-management.sh compose up --build -d web celery
: This command builds and starts theweb
andcelery
services using Docker Compose. It is used for testing changes in the base application or core application components.
These scripts enable comprehensive testing of the entire monorepo service set, regardless of the target platform, and without the need for complex local configuration.