Enerdit Backend is the server-side component of the Enerdit application, designed to handle user authentication, energy audit data management, and provide a RESTful API for the frontend. It is built using Django and Django REST Framework.
- User authentication with JWT tokens
- Management of energy audit data
- RESTful API for frontend integration
- Support for multiple building types and compartments
- Energy consumption calculations and recommendations
- Django: A high-level Python web framework for rapid development.
- Django REST Framework: A powerful toolkit for building Web APIs.
- PostgreSQL: A powerful, open-source object-relational database system.
- Django CORS Headers: For handling Cross-Origin Resource Sharing (CORS).
- Django Environ: For managing environment variables.
- Gunicorn: A Python WSGI HTTP Server for UNIX, used to serve the application.
- Uvicorn: A lightning-fast ASGI server for Python, suitable for asynchronous applications.
To set up the Enerdit backend, follow these steps:
-
Clone the repository:
git clone https://github.com/yourusername/enerdit-backend.git
-
Navigate to the project directory:
cd enerdit-backend
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install the dependencies:
pip install -r requirements.txt
-
Create a
.env
file in the root directory and add the following environment variables:SECRET_KEY=your_secret_key DEBUG=True DATABASE_URL=postgres://user:password@localhost:5432/yourdbname
-
Run database migrations:
python manage.py migrate
-
Create a superuser (optional):
python manage.py createsuperuser
To run the application, use the following command:
python manage.py runserver
This will start the development server at http://127.0.0.1:8000/
.
For production, you can run the application using Uvicorn. Use the following command:
python -m uvicorn Enerdit.asgi:application --host 0.0.0.0 --port 8000
Alternatively, you can use Gunicorn to serve your application with Uvicorn workers. This is recommended for production environments. Use the following command:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker Enerdit.asgi:application --host 0.0.0.0 --port 8000
-w 4
: This option specifies the number of worker processes. Adjust this based on your server's CPU cores.-k uvicorn.workers.UvicornWorker
: This option tells Gunicorn to use Uvicorn as the worker class.
The following are the main API endpoints available:
-
Authentication
POST /auth/login/
: Log in a user and receive a JWT token.POST /auth/signup/
: Register a new user.
-
Energy Audit
POST /energy-audit/
: Create a new energy audit report.
The backend includes the following key models:
- User: Represents the user of the application.
- Building: Represents a building with attributes like type, number of floors, and energy consumption.
- Compartment: Represents a compartment within a building, containing appliances.
- Appliance: Represents an appliance with attributes like power rating and usage time.