Skip to content

Commit 1ac90bb

Browse files
committed
added setup_integration.py
1 parent a541ca1 commit 1ac90bb

File tree

2 files changed

+514
-0
lines changed

2 files changed

+514
-0
lines changed

README.md

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,42 @@ docker exec -it backend python manage.py migrate
6464

6565
The API will be available at http://localhost:8000/api/
6666

67+
## Setup Integration Script
68+
69+
To simplify the integration of the Supabase template into your existing Python project, you can use the provided setup script. This script will copy all necessary files and configurations from the template into your project.
70+
71+
### Features
72+
73+
- **Copies all necessary files and folders** from the template to your project
74+
- **Merges Pipfiles** if both your project and the template have them
75+
- **Preserves existing files** in your project while adding new ones
76+
- **Handles configuration files** like `.env`, `docker-compose.yml`, etc.
77+
78+
### Usage
79+
80+
1. **Run the Setup Script**:
81+
After cloning the repository, navigate to the project directory and run the script:
82+
83+
```bash
84+
python setup_integration.py /path/to/your/existing/project
85+
```
86+
87+
This will copy all files and directories (except the script itself) to your project, including:
88+
89+
- `.env.example` as `.env`
90+
- `docker-compose.yml`
91+
- `config` folder (for Prometheus)
92+
- `database` folder
93+
- `docker` folder (containing Dockerfile)
94+
- All other necessary files
95+
96+
2. **Pipfile Merging**:
97+
We suggest using Pipfile to manage dependencies in your project. If your existing project has a `Pipfile` and the template also has one, the script will attempt to merge them, preserving your existing dependencies while adding the ones required by the template.
98+
99+
3. **Follow the Remaining Setup Steps**:
100+
After running the script, follow the remaining setup steps in the README to configure your project.
101+
102+
67103
## Authentication Flow
68104

69105
1. Users authenticate through Supabase (OAuth, email/password, OTP)
@@ -74,10 +110,201 @@ The API will be available at http://localhost:8000/api/
74110
## API Rate Limiting & Credit System
75111

76112
The template includes:
113+
77114
- Per-user rate limiting via Django REST Framework throttling
78115
- Credit-based usage tracking for premium features
79116
- API endpoints to check remaining credits
80117

118+
## Managing Credits for API Endpoints
119+
120+
### Using CreditUsageRate
121+
122+
The `CreditUsageRate` model allows administrators to define credit costs for different API endpoints. Here's how to use it:
123+
124+
1. **Create a New Credit Usage Rate**:
125+
You can create a new credit usage rate for an endpoint in the Django shell or through the admin interface:
126+
127+
```python
128+
from apps.credits.models import CreditUsageRate
129+
130+
new_rate = CreditUsageRate.objects.create(
131+
endpoint_path='/api/resource/', # The API endpoint
132+
credits_per_request=5, # Set the number of credits for this endpoint
133+
description='Cost for accessing the resource endpoint',
134+
is_active=True # Set to True to make it active
135+
)
136+
```
137+
138+
2. **Update Existing Credit Usage Rates**:
139+
If you need to change the credit cost for an existing endpoint, fetch the instance and update it:
140+
141+
```python
142+
existing_rate = CreditUsageRate.objects.get(endpoint_path='/api/resource/')
143+
existing_rate.credits_per_request = 3 # Set new credit cost
144+
existing_rate.save() # Save changes
145+
```
146+
147+
3. **Admin Interface**:
148+
You can also manage credit usage rates through the Django admin interface by navigating to the `Credit Usage Rates` section.
149+
150+
## Integrating the Complete SaaS Backend into an Existing Python Project
151+
152+
To transform your existing Python project into a SaaS-ready API with monitoring, task queues, and credit management, follow these comprehensive steps:
153+
154+
1. **Clone the Repository**:
155+
Clone the entire repository into a directory adjacent to your existing project:
156+
157+
```bash
158+
git clone https://github.com/your-org/django-supabase-template.git
159+
```
160+
161+
2. **Merge Project Structure**:
162+
Instead of just copying the backend directory, you'll want to integrate the entire project structure:
163+
164+
- Copy the `docker-compose.yml` file to your project root
165+
- Copy the `config/prometheus.yml` directory to your project root
166+
- Copy the `.github/workflows` directory for CI/CD pipelines
167+
- Copy the `backend` directory to your project
168+
169+
3. **Configure Docker Compose**:
170+
Modify the `docker-compose.yml` file to match your project's needs. The file includes services for:
171+
172+
- Django backend
173+
- Redis for caching and Celery
174+
- Celery for background tasks
175+
- Prometheus for monitoring
176+
177+
Ensure the volume mappings and service names align with your project structure.
178+
179+
4. **Install Dependencies**:
180+
Add the required dependencies to your project:
181+
182+
```bash
183+
pip install -r backend/requirements.txt
184+
```
185+
186+
5. **Configure Environment Variables**:
187+
Create a `.env` file based on the `.env.example` provided and configure:
188+
189+
- Supabase credentials
190+
- Redis connection details
191+
- Other service configurations
192+
193+
6. **Integrate Apps into Your Django Project**:
194+
In your existing Django project's `settings.py`, add the necessary apps and configurations:
195+
196+
```python
197+
INSTALLED_APPS = [
198+
# Existing apps
199+
...,
200+
# New apps
201+
'apps.credits',
202+
'apps.authentication',
203+
'apps.users',
204+
'django_prometheus',
205+
]
206+
207+
# Add Prometheus middleware
208+
MIDDLEWARE = [
209+
'django_prometheus.middleware.PrometheusBeforeMiddleware',
210+
# Your existing middleware
211+
...,
212+
'django_prometheus.middleware.PrometheusAfterMiddleware',
213+
]
214+
215+
# Configure Celery
216+
CELERY_BROKER_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
217+
CELERY_RESULT_BACKEND = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
218+
```
219+
220+
7. **Configure Authentication**:
221+
Set up Supabase authentication in your project:
222+
223+
```python
224+
# Add to settings.py
225+
REST_FRAMEWORK = {
226+
'DEFAULT_AUTHENTICATION_CLASSES': [
227+
'apps.authentication.authentication.SupabaseAuthentication',
228+
# Your existing authentication classes
229+
],
230+
}
231+
232+
# Supabase settings
233+
SUPABASE_URL = os.environ.get('SUPABASE_URL')
234+
SUPABASE_KEY = os.environ.get('SUPABASE_KEY')
235+
```
236+
237+
8. **Set Up Celery**:
238+
Create or modify your `celery.py` file to include task queues:
239+
240+
```python
241+
# celery.py
242+
from __future__ import absolute_import, unicode_literals
243+
import os
244+
from celery import Celery
245+
246+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
247+
248+
app = Celery('your_project')
249+
app.config_from_object('django.conf:settings', namespace='CELERY')
250+
app.autodiscover_tasks()
251+
```
252+
253+
9. **Run Migrations**:
254+
Apply the database migrations:
255+
256+
```bash
257+
python manage.py migrate
258+
```
259+
260+
10. **Create Custom Endpoints**:
261+
Extend the existing views or create new ones for your specific business logic:
262+
263+
```python
264+
# your_app/views.py
265+
from apps.credits.throttling import CreditBasedThrottle
266+
from rest_framework.views import APIView
267+
from rest_framework.response import Response
268+
269+
class YourCustomEndpoint(APIView):
270+
throttle_classes = [CreditBasedThrottle]
271+
272+
def post(self, request):
273+
# Your custom logic here
274+
return Response({"result": "success"})
275+
```
276+
277+
11. **Configure Credit Usage Rates**:
278+
Set up credit costs for your custom endpoints:
279+
280+
```bash
281+
python manage.py shell
282+
```
283+
284+
```python
285+
from apps.credits.models import CreditUsageRate
286+
CreditUsageRate.objects.create(endpoint_path='/api/your-endpoint/', credits_per_request=5)
287+
```
288+
289+
12. **Start the Full Stack**:
290+
Launch the entire application stack using Docker Compose:
291+
292+
```bash
293+
docker-compose up -d
294+
```
295+
296+
13. **Access Services**:
297+
298+
- Django API: http://localhost:8000/api/
299+
- Django Admin: http://localhost:8000/admin/
300+
- Prometheus: http://localhost:9090/
301+
302+
14. **Deploy Your SaaS API**:
303+
Use the included CI/CD workflows to deploy your application to your preferred hosting provider.
304+
305+
15. **Monitor and Scale**:
306+
Use Prometheus metrics to monitor usage and performance, and scale your services as needed.
307+
81308
## Monitoring & Error Tracking
82309

83310
- Prometheus metrics exposed at `/metrics`
@@ -86,6 +313,7 @@ The template includes:
86313
## Deployment
87314

88315
The project is fully Dockerized and includes CI/CD pipeline configurations for:
316+
89317
- GitHub Actions
90318
- GitLab CI
91319

0 commit comments

Comments
 (0)