From 30576a9c6e063cceeed1d83d661ae7de41c2ef99 Mon Sep 17 00:00:00 2001 From: vickytechkey Date: Sun, 18 Oct 2026 20:55:45 +0530 Subject: [PATCH] feat: initialize Django payment service project structure, configurations, and CI/CD workflow --- .forgejo/workflows/beta.yml | 98 ++++++++++----------- .gitignore | 33 +++++++ config/__init__.py | 0 config/asgi.py | 16 ++++ config/settings.py | 150 ++++++++++++++++++++++++++++++++ config/urls.py | 23 +++++ config/wsgi.py | 16 ++++ manage.py | 22 +++++ payments/__init__.py | 0 payments/admin.py | 3 + payments/apps.py | 5 ++ payments/migrations/__init__.py | 0 payments/models.py | 3 + payments/tests.py | 3 + payments/urls.py | 5 ++ payments/views.py | 3 + paymentservice.service | 16 ++++ requirements.txt | 7 ++ 18 files changed, 349 insertions(+), 54 deletions(-) create mode 100644 .gitignore create mode 100644 config/__init__.py create mode 100644 config/asgi.py create mode 100644 config/settings.py create mode 100644 config/urls.py create mode 100644 config/wsgi.py create mode 100644 manage.py create mode 100644 payments/__init__.py create mode 100644 payments/admin.py create mode 100644 payments/apps.py create mode 100644 payments/migrations/__init__.py create mode 100644 payments/models.py create mode 100644 payments/tests.py create mode 100644 payments/urls.py create mode 100644 payments/views.py create mode 100644 paymentservice.service create mode 100644 requirements.txt diff --git a/.forgejo/workflows/beta.yml b/.forgejo/workflows/beta.yml index e60d5bc..baeaba7 100644 --- a/.forgejo/workflows/beta.yml +++ b/.forgejo/workflows/beta.yml @@ -33,23 +33,24 @@ jobs: "logs_collected": { "files": { "collect_list": [ - { "file_path": "/home/ubuntu/searchengine/logs/app.log", "log_group_name": "/searchengine/app", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, - { "file_path": "/home/ubuntu/searchengine/logs/error.log", "log_group_name": "/searchengine/error", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, - { "file_path": "/home/ubuntu/searchengine/logs/requests.log", "log_group_name": "/searchengine/requests", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, - { "file_path": "/home/ubuntu/searchengine/logs/gunicorn-access.log", "log_group_name": "/searchengine/gunicorn-access", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, - { "file_path": "/home/ubuntu/searchengine/logs/gunicorn-error.log", "log_group_name": "/searchengine/gunicorn-error", "log_stream_name": "{instance_id}", "retention_in_days": 30 } + { "file_path": "/home/ubuntu/paymentservice/logs/app.log", "log_group_name": "/paymentservice/app", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/paymentservice/logs/error.log", "log_group_name": "/paymentservice/error", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/paymentservice/logs/requests.log", "log_group_name": "/paymentservice/requests", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/paymentservice/logs/gunicorn-access.log", "log_group_name": "/paymentservice/gunicorn-access", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/paymentservice/logs/gunicorn-error.log", "log_group_name": "/paymentservice/gunicorn-error", "log_stream_name": "{instance_id}", "retention_in_days": 30 } ] } } } } EOF + cat << 'EOF' > tmp/nginx-proxy.conf server { - server_name betasearch-api.tradhox.com search-api.tradhox.com; + server_name betapayments.tradhox.com; location / { - proxy_pass http://127.0.0.1:8087; + proxy_pass http://127.0.0.1:8088; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -57,15 +58,15 @@ jobs: } listen 443 ssl; - ssl_certificate /etc/letsencrypt/live/betapartner-api.tradhox.com/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/betapartner-api.tradhox.com/privkey.pem; + ssl_certificate /etc/letsencrypt/live/betapayments.tradhox.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/betapayments.tradhox.com/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; } server { listen 80; - server_name betasearch-api.tradhox.com search-api.tradhox.com; + server_name betapayments.tradhox.com; return 301 https://$host$request_uri; } EOF @@ -82,7 +83,7 @@ jobs: chmod 600 ~/.ssh/id_rsa # Copy app archive and configuration files - scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json tmp/nginx-proxy.conf searchengine.service ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/ + scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json tmp/nginx-proxy.conf paymentservice.service ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/ # Execute deployment commands on remote server ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }} << 'EOF' @@ -90,61 +91,50 @@ jobs: # ── 1. Install Host Dependencies ────────────────────────────── sudo apt-get update - sudo apt-get install -y python3-pip python3-venv python3-dev pkg-config libpq-dev build-essential nginx + sudo apt-get install -y python3-pip python3-venv python3-dev pkg-config libpq-dev build-essential nginx certbot python3-certbot-nginx - # ── 2. Install Typesense Server Binary (Arch Detected) ───────── - if ! command -v typesense-server &> /dev/null; then - ARCH=$(dpkg --print-architecture) - echo "Installing Typesense Server v27.1 for architecture: ${ARCH}..." - curl -O https://dl.typesense.org/releases/27.1/typesense-server-27.1-${ARCH}.deb - sudo dpkg -i typesense-server-27.1-${ARCH}.deb - rm -f typesense-server-27.1-${ARCH}.deb - fi + # ── 2. Extract codebase to target directory ─────────────────── + sudo mkdir -p /home/ubuntu/paymentservice + sudo chown -R ubuntu:ubuntu /home/ubuntu/paymentservice + tar -xzf /tmp/app.tar.gz -C /home/ubuntu/paymentservice + mkdir -p /home/ubuntu/paymentservice/logs + sudo chown -R ubuntu:ubuntu /home/ubuntu/paymentservice/logs - # Ensure Typesense directory & config exists - sudo mkdir -p /var/lib/typesense - cat << 'TYPESENSE_CONF' | sudo tee /etc/typesense/typesense-server.ini - api-key = vignesh_typesense_master_key_2026 - data-dir = /var/lib/typesense - log-dir = /var/log/typesense - api-port = 8108 - listen-address = 127.0.0.1 - TYPESENSE_CONF - - sudo systemctl enable typesense-server - sudo systemctl restart typesense-server || true - - # ── 3. Extract codebase to target directory ─────────────────── - sudo mkdir -p /home/ubuntu/searchengine - sudo chown -R ubuntu:ubuntu /home/ubuntu/searchengine - tar -xzf /tmp/app.tar.gz -C /home/ubuntu/searchengine - mkdir -p /home/ubuntu/searchengine/logs - sudo chown -R ubuntu:ubuntu /home/ubuntu/searchengine/logs - - # ── 4. Set up python virtual environment & dependencies ──────── - cd /home/ubuntu/searchengine + # ── 3. Set up python virtual environment & dependencies ──────── + cd /home/ubuntu/paymentservice python3 -m venv venv source venv/bin/activate pip install --upgrade pip pip install -r requirements.txt python manage.py migrate --noinput - # ── 5. Systemd Service Deployment ────────────────────────────── - sudo cp /tmp/searchengine.service /etc/systemd/system/searchengine.service + # ── 4. Systemd Service Deployment ────────────────────────────── + sudo cp /tmp/paymentservice.service /etc/systemd/system/paymentservice.service sudo systemctl daemon-reload - sudo systemctl enable searchengine.service - sudo systemctl restart searchengine.service || { - echo "searchengine.service failed to restart! Checking journalctl:" - sudo journalctl -u searchengine.service -n 50 --no-pager + sudo systemctl enable paymentservice.service + sudo systemctl restart paymentservice.service || { + echo "paymentservice.service failed to restart! Checking journalctl:" + sudo journalctl -u paymentservice.service -n 50 --no-pager exit 1 } - # ── 6. Nginx Config ─────────────────────────────────────────── - if [ -f "/etc/letsencrypt/live/betasearch-api.tradhox.com/fullchain.pem" ]; then - sed -i 's|/etc/letsencrypt/live/betapartner-api.tradhox.com|/etc/letsencrypt/live/betasearch-api.tradhox.com|g' /tmp/nginx-proxy.conf + # ── 5. Ensure SSL Certificate Exists for betapayments.tradhox.com ─ + if [ ! -d "/etc/letsencrypt/live/betapayments.tradhox.com" ]; then + echo "Requesting SSL certificate for betapayments.tradhox.com..." + sudo systemctl stop nginx || true + sudo certbot certonly --standalone -d betapayments.tradhox.com --non-interactive --agree-tos --register-unsafely-without-email || true + sudo systemctl start nginx || true fi - sudo cp /tmp/nginx-proxy.conf /etc/nginx/sites-available/searchengine - sudo ln -sf /etc/nginx/sites-available/searchengine /etc/nginx/sites-enabled/searchengine + + # Fallback if certificate was not created + if [ ! -f "/etc/letsencrypt/live/betapayments.tradhox.com/fullchain.pem" ]; then + echo "Warning: betapayments.tradhox.com cert not present, falling back to existing partner cert temporarily..." + sed -i 's|/etc/letsencrypt/live/betapayments.tradhox.com|/etc/letsencrypt/live/betapartner-api.tradhox.com|g' /tmp/nginx-proxy.conf + fi + + # ── 6. Nginx Config ─────────────────────────────────────────── + sudo cp /tmp/nginx-proxy.conf /etc/nginx/sites-available/paymentservice + sudo ln -sf /etc/nginx/sites-available/paymentservice /etc/nginx/sites-enabled/paymentservice sudo nginx -t && sudo systemctl reload nginx # ── 7. CloudWatch Agent Config ──────────────────────────────── @@ -152,5 +142,5 @@ jobs: sudo cp /tmp/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json sudo systemctl restart amazon-cloudwatch-agent || true - echo "searchengine microservice successfully deployed!" + echo "paymentservice microservice successfully deployed for betapayments.tradhox.com!" EOF diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f2c7ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Django +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal +media/ +staticfiles/ + +# Environment variables +.env +.env.local + +# IDE & Editor +.idea/ +.vscode/ +*.swp +*.swo + +# OS +.DS_Store +Thumbs.db diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/config/asgi.py b/config/asgi.py new file mode 100644 index 0000000..33a47a1 --- /dev/null +++ b/config/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for config project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/6.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') + +application = get_asgi_application() diff --git a/config/settings.py b/config/settings.py new file mode 100644 index 0000000..83e8397 --- /dev/null +++ b/config/settings.py @@ -0,0 +1,150 @@ +""" +Django settings for config project. + +Generated by 'django-admin startproject' using Django 6.1. + +For more information on this file, see +https://docs.djangoproject.com/en/6.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/6.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/6.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-!&om-o4k1p3t76*ordcwf9tuqo6j69jtt2r-u6jp$^@6h0$!kh' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +import os + +ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',') if os.environ.get('ALLOWED_HOSTS') else ['*'] + +CORS_ALLOW_ALL_ORIGINS = True +CSRF_TRUSTED_ORIGINS = [ + 'https://betapayments.tradhox.com', + 'http://localhost', + 'http://127.0.0.1', +] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'rest_framework', + 'corsheaders', + 'payments', +] + +MIDDLEWARE = [ + 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'config.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'config.wsgi.application' + + +# Database Configuration (PostgreSQL) +# https://docs.djangoproject.com/en/6.1/ref/settings/#databases + +db_host = os.environ.get('DB_HOST', '16.113.106.152') +db_port = os.environ.get('DB_PORT', '5432') +db_user = os.environ.get('DB_USER', 'vignesh') +db_pass = os.environ.get('DB_PASSWORD', 'vignesh_secret_pass_2026') +db_name = os.environ.get('DB_NAME', 'payments_beta') + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': db_name, + 'USER': db_user, + 'PASSWORD': db_pass, + 'HOST': db_host, + 'PORT': str(db_port), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/6.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/6.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/6.1/howto/static-files/ + +STATIC_URL = 'static/' + + +# Email +# https://docs.djangoproject.com/en/6.1/topics/email/#topic-email-configuration + +MAILERS = { + 'default': { + 'BACKEND': 'django.core.mail.backends.console.EmailBackend', + }, +} diff --git a/config/urls.py b/config/urls.py new file mode 100644 index 0000000..cfa3d26 --- /dev/null +++ b/config/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for config project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/6.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/payments/', include('payments.urls')), +] diff --git a/config/wsgi.py b/config/wsgi.py new file mode 100644 index 0000000..1b4cc01 --- /dev/null +++ b/config/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for config project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/6.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..8e7ac79 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/payments/__init__.py b/payments/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/payments/admin.py b/payments/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/payments/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/payments/apps.py b/payments/apps.py new file mode 100644 index 0000000..58d36ac --- /dev/null +++ b/payments/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class PaymentsConfig(AppConfig): + name = 'payments' diff --git a/payments/migrations/__init__.py b/payments/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/payments/models.py b/payments/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/payments/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/payments/tests.py b/payments/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/payments/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/payments/urls.py b/payments/urls.py new file mode 100644 index 0000000..0453cd3 --- /dev/null +++ b/payments/urls.py @@ -0,0 +1,5 @@ +from django.urls import path + +urlpatterns = [ + # Payment service endpoints +] diff --git a/payments/views.py b/payments/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/payments/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/paymentservice.service b/paymentservice.service new file mode 100644 index 0000000..2d91e25 --- /dev/null +++ b/paymentservice.service @@ -0,0 +1,16 @@ +[Unit] +Description=Tradhox Payment Service Microservice Daemon +After=network.target + +[Service] +User=ubuntu +Group=ubuntu +WorkingDirectory=/home/ubuntu/paymentservice +ExecStart=/home/ubuntu/paymentservice/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8088 config.wsgi:application +Restart=always +RestartSec=5 +StandardOutput=append:/home/ubuntu/paymentservice/logs/gunicorn-access.log +StandardError=append:/home/ubuntu/paymentservice/logs/gunicorn-error.log + +[Install] +WantedBy=multi-user.target diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..27c3fb2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +Django>=5.1,<7.0 +djangorestframework>=3.15.0 +django-cors-headers>=4.3.0 +psycopg2-binary>=2.9.9 +python-dotenv>=1.0.0 +gunicorn>=22.0.0 +requests>=2.31.0