Browse Source

Exeternalisation de la conf dans un .env

Création d'un Dockerfile
Mise à jour du README et requirements
main
scayac 6 months ago
parent
commit
2f96b06440
  1. 1
      .gitignore
  2. 36
      Dockerfile
  3. 18
      README.md
  4. 6
      default.env
  5. 28
      ia_prof/settings.py
  6. 3
      requirements.txt

1
.gitignore vendored

@ -2,3 +2,4 @@ venv
db.sqlite3 db.sqlite3
__pycache__ __pycache__
migrations migrations
.env

36
Dockerfile

@ -0,0 +1,36 @@
# Utilise une image Python officielle
FROM python:3.12-slim
# Variables d'environnement
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Installer les dépendances système
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
apache2 \
apache2-dev \
&& rm -rf /var/lib/apt/lists/*
# Installer mod_wsgi
RUN pip install mod_wsgi
# Créer le dossier de l'application
WORKDIR /code
# Copier les fichiers de l'application
COPY . /code/
# Installer les dépendances Python
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
# Collecte des fichiers statiques
RUN python manage.py collectstatic --noinput
# Exposer le port 8000
EXPOSE 8000
# Commande de lancement avec mod_wsgi-express
CMD mod_wsgi-express start-server --port 8000 --url-alias /static /code/static /code/ia_prof/wsgi.py

18
README.md

@ -77,17 +77,15 @@ IAProf
python manage.py runserver python manage.py runserver
``` ```
## Configuration de la clé OpenAI 7. Créez un fichier `.env` à la racine du projet (ou copiez `default.env` puis renommez-le en `.env`) et renseignez vos variables sensibles :
```
La clé API OpenAI doit être définie dans le fichier `ia_prof/settings.py` : DJANGO_SECRET_KEY=<votre_clé_secrète>
OPENAI_API_KEY=<votre_clé_openai>
```python DJANGO_DEBUG=0
OPENAI_API_KEY = "votre_clé_openai" DJANGO_ALLOWED_HOSTS=127.0.0.1,localhost
``` ```
## Utilisation ## Utilisation
- Rendez-vous sur `http://127.0.0.1:8000/login` pour accéder à la page de connexion. - Rendez-vous sur `http://127.0.0.1:8000/login` pour accéder à la page de connexion.
- Après connexion, vous serez redirigé vers la vue « Hello World ». - Après connexion, vous serez redirigé vers la vue de connexion.
Dans le code, utilisez-la via `settings.OPENAI_API_KEY` pour sécuriser et centraliser la configuration.

6
default.env

@ -0,0 +1,6 @@
# Fichier .env pour Django (à placer à la racine du projet)
DJANGO_SECRET_KEY=
OPENAI_API_KEY=
DJANGO_DEBUG=1
DJANGO_ALLOWED_HOSTS=*

28
ia_prof/settings.py

@ -1,25 +1,18 @@
"""
Django settings for IAProf project.
Generated by 'django-admin startproject' using Django 4.x.
"""
import os import os
from pathlib import Path from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Charger les variables d'environnement depuis un fichier .env si présent
BASE_DIR = Path(__file__).resolve().parent.parent from dotenv import load_dotenv
# Quick-start development settings - unsuitable for production load_dotenv()
# See https://docs.djangoproject.com/en/4.x/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # Build paths inside the project like this: BASE_DIR / 'subdir'.
SECRET_KEY = 'your-secret-key' BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [] SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', '')
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '')
DEBUG = os.environ.get('DJANGO_DEBUG', '') == '1'
ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', '').split(',') if not DEBUG else []
# Application definition # Application definition
@ -112,5 +105,4 @@ STATIC_URL = '/static/'
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.x/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.x/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
OPENAI_API_KEY = "your-openai-api-key"

3
requirements.txt

@ -3,4 +3,5 @@ djangorestframework==3.14.0
openai==0.27.0 openai==0.27.0
PyPDF2 PyPDF2
openai openai
pdfplumber pdfplumber
python-dotenv
Loading…
Cancel
Save