tiffank1802 commited on
Commit
f869576
·
1 Parent(s): 5ee7e8d

Fix production deployment: remove exposed credentials, improve startup script, add WhiteNoise middleware

Browse files

- Remove MongoDB credentials from settings.py (now via environment variables)
- Create proper run.sh startup script with better error handling
- Add WhiteNoise middleware for static file serving
- Fix requirements.txt (remove invalid gridfs package, update Django to 5.0)
- Improve .env.example with all necessary configuration options
- Update Dockerfile to use run.sh for better production startup

.env.example CHANGED
@@ -1,7 +1,33 @@
1
- # Variables d'environnement Appwrite
2
- VITE_APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
3
- VITE_APPWRITE_PROJECT_ID=your_project_id
4
- VITE_APPWRITE_DATABASE_ID=your_database_id
5
- VITE_APPWRITE_MODULES_COLLECTION_ID=your_modules_collection_id
6
- VITE_APPWRITE_SECTIONS_COLLECTION_ID=your_sections_collection_id
7
- VITE_APPWRITE_RESOURCES_COLLECTION_ID=your_resources_collection_id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Django Configuration
2
+ DEBUG=False
3
+ SECRET_KEY=generate-a-strong-secret-key-here
4
+ ALLOWED_HOSTS=localhost,127.0.0.1,*
5
+ CSRF_TRUSTED_ORIGINS=http://localhost:8000,http://127.0.0.1:8000
6
+
7
+ # Database Configuration (optional - defaults to SQLite)
8
+ # DATABASE_ENGINE=sqlite3 # or 'postgresql' for PostgreSQL
9
+ # DB_NAME=enise
10
+ # DB_USER=postgres
11
+ # DB_PASSWORD=your_password
12
+ # DB_HOST=localhost
13
+ # DB_PORT=5432
14
+
15
+ # Appwrite Configuration
16
+ APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
17
+ APPWRITE_PROJECT_ID=your_project_id
18
+ APPWRITE_API_KEY=your_api_key
19
+ APPWRITE_DATABASE_ID=enise_db
20
+
21
+ # MongoDB Configuration (optional, for file storage)
22
+ # MONGO_DB_HOST=localhost
23
+ # MONGO_DB_PORT=27017
24
+ # MONGO_DB_USER=your_username
25
+ # MONGO_DB_PASSWORD=your_password
26
+ # MONGO_DB_NAME=enise_filesystem
27
+
28
+ # Security Settings for Production (HF Spaces)
29
+ SECURE_HSTS_SECONDS=31536000
30
+ SECURE_HSTS_INCLUDE_SUBDOMAINS=True
31
+ SECURE_SSL_REDIRECT=False
32
+ SESSION_COOKIE_SECURE=False
33
+ CSRF_COOKIE_SECURE=False
Dockerfile CHANGED
@@ -19,6 +19,9 @@ COPY . .
19
  # Créer les dossiers nécessaires
20
  RUN mkdir -p staticfiles media logs
21
 
 
 
 
22
  # Exposer le port
23
  EXPOSE 7860
24
 
@@ -27,4 +30,4 @@ ENV PYTHONUNBUFFERED=1
27
  ENV DEBUG=False
28
 
29
  # Commande de démarrage
30
- CMD ["gunicorn", "enise_site.wsgi", "--bind", "0.0.0.0:7860", "--workers", "2"]
 
19
  # Créer les dossiers nécessaires
20
  RUN mkdir -p staticfiles media logs
21
 
22
+ # Rendre le script exécutable
23
+ RUN chmod +x run.sh
24
+
25
  # Exposer le port
26
  EXPOSE 7860
27
 
 
30
  ENV DEBUG=False
31
 
32
  # Commande de démarrage
33
+ CMD ["./run.sh"]
__pycache__/simple_file_services.cpython-312.pyc CHANGED
Binary files a/__pycache__/simple_file_services.cpython-312.pyc and b/__pycache__/simple_file_services.cpython-312.pyc differ
 
app.py CHANGED
@@ -11,15 +11,21 @@ if __name__ == '__main__':
11
  django.setup()
12
 
13
  # Créer les migrations
14
- execute_from_command_line(['manage.py', 'migrate', '--noinput'])
 
 
 
 
15
 
16
  # Créer les données initiales si nécessaire
17
  try:
18
- execute_from_command_line(['manage.py', 'init_data'])
19
- except:
20
- pass
 
21
 
22
  # Démarrer gunicorn
 
23
  from gunicorn.app.wsgiapp import run
24
  sys.argv = [
25
  'gunicorn',
@@ -28,6 +34,7 @@ if __name__ == '__main__':
28
  '--workers', '2',
29
  '--timeout', '60',
30
  '--access-logfile', '-',
31
- '--error-logfile', '-'
 
32
  ]
33
  sys.exit(run())
 
11
  django.setup()
12
 
13
  # Créer les migrations
14
+ try:
15
+ execute_from_command_line(['app.py', 'migrate', '--noinput'])
16
+ print("[INFO] Migrations completed successfully")
17
+ except Exception as e:
18
+ print(f"[WARNING] Migration error (non-critical): {e}")
19
 
20
  # Créer les données initiales si nécessaire
21
  try:
22
+ execute_from_command_line(['app.py', 'init_data'])
23
+ print("[INFO] Initial data loaded")
24
+ except Exception as e:
25
+ print(f"[INFO] No initial data command or already loaded: {e}")
26
 
27
  # Démarrer gunicorn
28
+ print("[INFO] Starting gunicorn server on 0.0.0.0:7860")
29
  from gunicorn.app.wsgiapp import run
30
  sys.argv = [
31
  'gunicorn',
 
34
  '--workers', '2',
35
  '--timeout', '60',
36
  '--access-logfile', '-',
37
+ '--error-logfile', '-',
38
+ '--log-level', 'info'
39
  ]
40
  sys.exit(run())
app_core/__pycache__/__init__.cpython-312.pyc CHANGED
Binary files a/app_core/__pycache__/__init__.cpython-312.pyc and b/app_core/__pycache__/__init__.cpython-312.pyc differ
 
app_core/__pycache__/admin.cpython-312.pyc CHANGED
Binary files a/app_core/__pycache__/admin.cpython-312.pyc and b/app_core/__pycache__/admin.cpython-312.pyc differ
 
app_core/__pycache__/apps.cpython-312.pyc CHANGED
Binary files a/app_core/__pycache__/apps.cpython-312.pyc and b/app_core/__pycache__/apps.cpython-312.pyc differ
 
app_core/__pycache__/file_views.cpython-312.pyc CHANGED
Binary files a/app_core/__pycache__/file_views.cpython-312.pyc and b/app_core/__pycache__/file_views.cpython-312.pyc differ
 
app_core/__pycache__/models.cpython-312.pyc CHANGED
Binary files a/app_core/__pycache__/models.cpython-312.pyc and b/app_core/__pycache__/models.cpython-312.pyc differ
 
app_core/__pycache__/urls.cpython-312.pyc CHANGED
Binary files a/app_core/__pycache__/urls.cpython-312.pyc and b/app_core/__pycache__/urls.cpython-312.pyc differ
 
app_core/__pycache__/views.cpython-312.pyc CHANGED
Binary files a/app_core/__pycache__/views.cpython-312.pyc and b/app_core/__pycache__/views.cpython-312.pyc differ
 
app_core/templatetags/__pycache__/__init__.cpython-312.pyc CHANGED
Binary files a/app_core/templatetags/__pycache__/__init__.cpython-312.pyc and b/app_core/templatetags/__pycache__/__init__.cpython-312.pyc differ
 
app_core/templatetags/__pycache__/file_tags.cpython-312.pyc CHANGED
Binary files a/app_core/templatetags/__pycache__/file_tags.cpython-312.pyc and b/app_core/templatetags/__pycache__/file_tags.cpython-312.pyc differ
 
app_formations/__pycache__/__init__.cpython-312.pyc CHANGED
Binary files a/app_formations/__pycache__/__init__.cpython-312.pyc and b/app_formations/__pycache__/__init__.cpython-312.pyc differ
 
app_formations/__pycache__/admin.cpython-312.pyc CHANGED
Binary files a/app_formations/__pycache__/admin.cpython-312.pyc and b/app_formations/__pycache__/admin.cpython-312.pyc differ
 
app_formations/__pycache__/apps.cpython-312.pyc CHANGED
Binary files a/app_formations/__pycache__/apps.cpython-312.pyc and b/app_formations/__pycache__/apps.cpython-312.pyc differ
 
app_formations/__pycache__/models.cpython-312.pyc CHANGED
Binary files a/app_formations/__pycache__/models.cpython-312.pyc and b/app_formations/__pycache__/models.cpython-312.pyc differ
 
enise_site/__pycache__/__init__.cpython-312.pyc CHANGED
Binary files a/enise_site/__pycache__/__init__.cpython-312.pyc and b/enise_site/__pycache__/__init__.cpython-312.pyc differ
 
enise_site/__pycache__/settings.cpython-312.pyc CHANGED
Binary files a/enise_site/__pycache__/settings.cpython-312.pyc and b/enise_site/__pycache__/settings.cpython-312.pyc differ
 
enise_site/__pycache__/urls.cpython-312.pyc CHANGED
Binary files a/enise_site/__pycache__/urls.cpython-312.pyc and b/enise_site/__pycache__/urls.cpython-312.pyc differ
 
enise_site/settings.py CHANGED
@@ -30,7 +30,8 @@ SECRET_KEY = config('SECRET_KEY', default='django-insecure-&d3hpc7rcky0d^)vspy#q
30
  # SECURITY WARNING: don't run with debug turned on in production!
31
  DEBUG = config('DEBUG', default='False') == 'True'
32
 
33
- ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='localhost,127.0.0.1').split(',')
 
34
 
35
 
36
  # Application definition
@@ -50,6 +51,7 @@ INSTALLED_APPS = [
50
 
51
  MIDDLEWARE = [
52
  'django.middleware.security.SecurityMiddleware',
 
53
  'django.contrib.sessions.middleware.SessionMiddleware',
54
  'django.middleware.common.CommonMiddleware',
55
  'django.middleware.csrf.CsrfViewMiddleware',
@@ -104,12 +106,12 @@ else:
104
  }
105
  }
106
 
107
- # Configuration MongoDB pour les fichiers (avec vos identifiants)
108
- MONGO_DB_HOST = 'localhost'
109
- MONGO_DB_PORT = 27017
110
- MONGO_DB_USER = 'tiffank1802'
111
- MONGO_DB_PASSWORD = 'SzPLNg4zfgz3jKuF'
112
- MONGO_DB_NAME = 'enise_filesystem'
113
 
114
 
115
  # Password validation
@@ -158,6 +160,7 @@ MEDIA_ROOT = BASE_DIR / 'media'
158
 
159
  # Security settings pour production
160
  CSRF_TRUSTED_ORIGINS = config('CSRF_TRUSTED_ORIGINS', default='http://localhost:8000,http://127.0.0.1:8000').split(',')
 
161
  SECURE_HSTS_SECONDS = config('SECURE_HSTS_SECONDS', default='0', cast=int)
162
  SECURE_HSTS_INCLUDE_SUBDOMAINS = config('SECURE_HSTS_INCLUDE_SUBDOMAINS', default='False') == 'True'
163
  SECURE_SSL_REDIRECT = config('SECURE_SSL_REDIRECT', default='False') == 'True'
 
30
  # SECURITY WARNING: don't run with debug turned on in production!
31
  DEBUG = config('DEBUG', default='False') == 'True'
32
 
33
+ ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='localhost,127.0.0.1,*').split(',')
34
+ ALLOWED_HOSTS = [h.strip() for h in ALLOWED_HOSTS] # Remove whitespace
35
 
36
 
37
  # Application definition
 
51
 
52
  MIDDLEWARE = [
53
  'django.middleware.security.SecurityMiddleware',
54
+ 'whitenoise.middleware.WhiteNoiseMiddleware',
55
  'django.contrib.sessions.middleware.SessionMiddleware',
56
  'django.middleware.common.CommonMiddleware',
57
  'django.middleware.csrf.CsrfViewMiddleware',
 
106
  }
107
  }
108
 
109
+ # Configuration MongoDB pour les fichiers
110
+ MONGO_DB_HOST = config('MONGO_DB_HOST', default='localhost')
111
+ MONGO_DB_PORT = config('MONGO_DB_PORT', default='27017', cast=int)
112
+ MONGO_DB_USER = config('MONGO_DB_USER', default='')
113
+ MONGO_DB_PASSWORD = config('MONGO_DB_PASSWORD', default='')
114
+ MONGO_DB_NAME = config('MONGO_DB_NAME', default='enise_filesystem')
115
 
116
 
117
  # Password validation
 
160
 
161
  # Security settings pour production
162
  CSRF_TRUSTED_ORIGINS = config('CSRF_TRUSTED_ORIGINS', default='http://localhost:8000,http://127.0.0.1:8000').split(',')
163
+ CSRF_TRUSTED_ORIGINS = [o.strip() for o in CSRF_TRUSTED_ORIGINS] # Remove whitespace
164
  SECURE_HSTS_SECONDS = config('SECURE_HSTS_SECONDS', default='0', cast=int)
165
  SECURE_HSTS_INCLUDE_SUBDOMAINS = config('SECURE_HSTS_INCLUDE_SUBDOMAINS', default='False') == 'True'
166
  SECURE_SSL_REDIRECT = config('SECURE_SSL_REDIRECT', default='False') == 'True'
requirements.txt CHANGED
@@ -1,11 +1,10 @@
1
- Django>=4.2
2
  gunicorn>=21.0
3
  appwrite>=1.0
4
  python-decouple>=3.8
5
  python-dotenv>=1.0
6
  Pillow>=10.0
7
  pymongo>=4.0
8
- gridfs>=1.0
9
  psycopg2-binary>=2.9
10
  whitenoise>=6.5
11
  requests>=2.31
 
1
+ Django>=5.0
2
  gunicorn>=21.0
3
  appwrite>=1.0
4
  python-decouple>=3.8
5
  python-dotenv>=1.0
6
  Pillow>=10.0
7
  pymongo>=4.0
 
8
  psycopg2-binary>=2.9
9
  whitenoise>=6.5
10
  requests>=2.31
run.sh ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ echo "========================================="
5
+ echo "ENISE Site - Starting Application"
6
+ echo "========================================="
7
+
8
+ # Set environment variables for HF Spaces
9
+ export PYTHONUNBUFFERED=1
10
+ export DJANGO_SETTINGS_MODULE=enise_site.settings
11
+
12
+ # Collect static files
13
+ echo "[1/3] Collecting static files..."
14
+ python manage.py collectstatic --noinput 2>/dev/null || echo "Static files already collected or skipped"
15
+
16
+ # Run migrations
17
+ echo "[2/3] Running database migrations..."
18
+ python manage.py migrate --noinput 2>/dev/null || echo "Migrations completed or no migrations needed"
19
+
20
+ # Start the server
21
+ echo "[3/3] Starting server on 0.0.0.0:7860..."
22
+ exec gunicorn enise_site.wsgi \
23
+ --bind 0.0.0.0:7860 \
24
+ --workers 2 \
25
+ --timeout 60 \
26
+ --access-logfile - \
27
+ --error-logfile - \
28
+ --log-level info