Add environment configuration and Docker support

- Created .env.example for environment variable management.
- Added Dockerfile for containerization of the application.
- Updated settings.py to load configuration from environment variables, enhancing security and flexibility.
- Removed hardcoded values in settings.py for database, email, and other configurations.
This commit is contained in:
ar.azizan 2025-05-05 18:08:37 +03:30
parent 567fd46741
commit 6037058471
3 changed files with 87 additions and 82 deletions

28
.env.example Normal file
View File

@ -0,0 +1,28 @@
SECRET_KEY=your_secret_key_here
DEBUG=True
DB_ENGINE=django.db.backends.postgresql
DB_NAME=iralex
DB_USER=iralex
DB_PASSWORD=thestrongpassword!!!!!2
DB_HOST=ira-lex.postgres.database.azure.com
DB_PORT=5432
EMAIL_HOST=smtp.gmail.com
EMAIL_HOST_USER=no-reply@ira-lex.com
EMAIL_HOST_PASSWORD=@Iralex2022
EMAIL_PORT=465
EMAIL_USE_TLS=False
EMAIL_USE_SSL=True
ADMIN_NAME_1=arm
ADMIN_EMAIL_1=ahmadarm.1396@gmail.com
ADMIN_NAME_2=Ehsan
ADMIN_EMAIL_2=ehsan.ghechisaz82@gmail.com
LOG_FILE=warning.log
LANGUAGE_CODE=en-us
TIME_ZONE=UTC
USE_I18N=True
USE_L10N=True
USE_TZ=False
STATIC_URL=/media/
STATIC_ROOT=media/
DEFAULT_AUTO_FIELD=django.db.models.BigAutoField
DEFAULT_CHARSET=UTF-8

20
Dockerfile Normal file
View File

@ -0,0 +1,20 @@
# Use the official Python image from the Docker Hub
FROM python:3.10
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file into the container
COPY requirements.txt .
# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt
# Copy the entire project into the container
COPY . .
# Expose the port the app runs on
EXPOSE 8000
# Command to run the application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

View File

@ -9,10 +9,10 @@ https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os.path
import os
from pathlib import Path
import sentry_sdk
#
# sentry_sdk.init(
# dsn="https://a8f112c882bbe6413c5eb0ed95dbd30d@o4506274962735104.ingest.sentry.io/4506455551311872",
# # Set traces_sample_rate to 1.0 to capture 100%
@ -27,20 +27,37 @@ import sentry_sdk
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
BASE_URL = 'https://ira-lex.com/app'
API_URL = 'https://api.ira-lex.com/'
BASE_URL = os.getenv('BASE_URL', 'https://ira-lex.com/app')
API_URL = os.getenv('API_URL', 'https://api.ira-lex.com/')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure--4xi=5)6zmp=)-xk^t25u!@7)8=xiw*$jlmxt(-kwzw^8t#i78'
# Load environment variables
SECRET_KEY = os.getenv('SECRET_KEY', 'default_secret_key')
DEBUG = os.getenv('DEBUG', 'True') == 'True'
DATABASES = {
'default': {
'ENGINE': os.getenv('DB_ENGINE', 'django.db.backends.postgresql'),
'NAME': os.getenv('DB_NAME', 'iralex'),
'USER': os.getenv('DB_USER', 'iralex'),
'PASSWORD': os.getenv('DB_PASSWORD', 'thestrongpassword!!!!!2'),
'HOST': os.getenv('DB_HOST', 'ira-lex.postgres.database.azure.com'),
'PORT': os.getenv('DB_PORT', '5432'),
}
}
EMAIL_HOST = os.getenv('EMAIL_HOST', 'smtp.gmail.com')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', 'no-reply@ira-lex.com')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', '@Iralex2022')
EMAIL_PORT = os.getenv('EMAIL_PORT', '465')
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS', 'False') == 'True'
EMAIL_USE_SSL = os.getenv('EMAIL_USE_SSL', 'True') == 'True'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ADMINS = [
(os.getenv('ADMIN_NAME_1', 'arm'), os.getenv('ADMIN_EMAIL_1', 'ahmadarm.1396@gmail.com')),
(os.getenv('ADMIN_NAME_2', 'Ehsan'), os.getenv('ADMIN_EMAIL_2', 'ehsan.ghechisaz82@gmail.com'))
]
ADMINS = [('arm', 'ahmadarm.1396@gmail.com'), ('Ehsan', 'ehsan.ghechisaz82@gmail.com')]
ALLOWED_HOSTS = [os.environ["WEBSITE_HOSTNAME"]] if "WEBSITE_HOSTNAME" in os.environ else ["*"]
ALLOWED_HOSTS = [os.getenv("WEBSITE_HOSTNAME", "*")]
# Application definition
@ -122,42 +139,6 @@ WSGI_APPLICATION = 'detective_book.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': "iralex",
"USER": "iralex",
"PASSWORD": "thestrongpassword!!!!!2",
# "PASSWORD": "1234strongpassword",
"HOST": "ira-lex.postgres.database.azure.com",
#"HOST" : "iralexbackup.postgres.database.azure.com",
"PORT": 5432
}
}
#
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': 'apple',
# 'USER': 'john',
# 'PASSWORD': 'banana',
# 'HOST': '4.194.210.96', # or the hostname where your PostgreSQL server is running
# 'PORT': '5432', # leave empty to use the default PostgreSQL port (5432)
# }
# }
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql_psycopg2',
# 'NAME': "dbiralex",
# "USER": "iralex",
# "PASSWORD": "thestrongpassword!!!",
# "HOST": "ls-c7b309f8603c680d66fb59b34e3827bf39ec0b45.crolvxjsee43.ap-southeast-1.rds.amazonaws.com",
# "PORT": 5432
# }
# }
LOGGING = {
'version': 1,
# The version number of our log
@ -175,7 +156,7 @@ LOGGING = {
'level': 'WARNING',
'class': 'logging.FileHandler',
'formatter': 'timestamp',
'filename': BASE_DIR / 'warning.log',
'filename': os.path.join(BASE_DIR, os.getenv('LOG_FILE', 'warning.log')),
},
},
# A logger for WARNING which has a handler called 'file'. A logger can have multiple handler
@ -212,55 +193,31 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = os.getenv('LANGUAGE_CODE', 'en-us')
TIME_ZONE = 'UTC'
TIME_ZONE = os.getenv('TIME_ZONE', 'UTC')
USE_I18N = True
USE_I18N = os.getenv('USE_I18N', 'True') == 'True'
USE_L10N = True
USE_L10N = os.getenv('USE_L10N', 'True') == 'True'
USE_TZ = False
USE_TZ = os.getenv('USE_TZ', 'False') == 'True'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_URL = os.getenv('STATIC_URL', '/media/')
STATIC_ROOT = os.path.join(BASE_DIR, os.getenv('STATIC_ROOT', 'media/'))
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = os.getenv('DEFAULT_AUTO_FIELD', 'django.db.models.BigAutoField')
DEFAULT_CHARSET = "UTF-8"
# EMAIL_HOST = "smtp-relay.gmail.com"
# EMAIL_HOST_USER = "info@ira-lex.com"
# EMAIL_HOST_PASSWORD = "A7Jq<8Mb"
# EMAIL_PORT = 465
# EMAIL_USE_TLS = False
# EMAIL_USE_SSL = True
# EMAIL_HOST = "smtp-relay.gmail.com"
# EMAIL_HOST_USER = "info@ira-lex.com"
# EMAIL_HOST_PASSWORD = "qzfhjdefivnqjxzg"
# EMAIL_PORT = 465
# EMAIL_USE_TLS = False
# EMAIL_USE_SSL = True
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "no-reply@ira-lex.com"
EMAIL_HOST_PASSWORD = "@Iralex2022"
# EMAIL_HOST_PASSWORD = "Iralex2022"
# EMAIL_HOST_USER = "info@ira-lex.com"
EMAIL_PORT = 465
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
DEFAULT_CHARSET = os.getenv('DEFAULT_CHARSET', 'UTF-8')
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
CORS_ALLOWED_ORIGINS = ["https://ira-lex.com",
"https://api.ira-lex.com"]
CORS_ALLOWED_ORIGINS = os.getenv('CORS_ALLOWED_ORIGINS', '["https://ira-lex.com", "https://api.ira-lex.com"]')
# CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_BROKER_URL = 'amqp://iralex:20232019364@localhost:5672/iravhost'