From 9fc5d3813b908da773e3ba5858789b5166fbc795 Mon Sep 17 00:00:00 2001 From: vickytechkey Date: Sat, 3 Oct 2026 17:31:57 +0530 Subject: [PATCH] feat: add Forgejo deployment workflows and update Nginx configuration with CORS support --- .forgejo/workflows/beta.yml | 2 +- .forgejo/workflows/main.yml | 2 +- configure_nginx_proxy.py | 101 ++++++++---------------------------- crmbackend.nginx.conf | 14 ++++- 4 files changed, 37 insertions(+), 82 deletions(-) diff --git a/.forgejo/workflows/beta.yml b/.forgejo/workflows/beta.yml index a37f69e..9f5df1a 100644 --- a/.forgejo/workflows/beta.yml +++ b/.forgejo/workflows/beta.yml @@ -52,7 +52,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 crm-backend.service configure_nginx_proxy.py ${{ 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 crm-backend.service crmbackend.nginx.conf configure_nginx_proxy.py ${{ 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' diff --git a/.forgejo/workflows/main.yml b/.forgejo/workflows/main.yml index 232f746..d88dc03 100644 --- a/.forgejo/workflows/main.yml +++ b/.forgejo/workflows/main.yml @@ -52,7 +52,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 crm-backend.service configure_nginx_proxy.py ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }}:/tmp/ + scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json crm-backend.service crmbackend.nginx.conf configure_nginx_proxy.py ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }}:/tmp/ # Execute deployment commands on remote server ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }} << 'EOF' diff --git a/configure_nginx_proxy.py b/configure_nginx_proxy.py index f52ff48..7bad467 100644 --- a/configure_nginx_proxy.py +++ b/configure_nginx_proxy.py @@ -3,93 +3,36 @@ import sys import subprocess def main(): - sites_dir = '/etc/nginx/sites-enabled' - if not os.path.exists(sites_dir): - print(f"Nginx sites-enabled directory not found: {sites_dir}") + source_conf = '/tmp/crmbackend.nginx.conf' + target_available = '/etc/nginx/sites-available/crmbackend' + target_enabled = '/etc/nginx/sites-enabled/crmbackend' + + if not os.path.exists(source_conf): + source_conf = '/home/ubuntu/crmbackend/crmbackend.nginx.conf' + + if not os.path.exists(source_conf): + print(f"Error: Nginx configuration source file not found at {source_conf}") return - target_file = None - for filename in os.listdir(sites_dir): - filepath = os.path.join(sites_dir, filename) - if os.path.isfile(filepath): - try: - with open(filepath, 'r') as f: - content = f.read() - if '8080' in content: - target_file = filepath - break - except Exception as e: - print(f"Error reading {filepath}: {e}") - - if not target_file: - print("Could not find active Nginx configuration proxying to port 8080.") - return - - print(f"Found active proxy configuration in {target_file}") - - with open(target_file, 'r') as f: - lines = f.readlines() - - # Check if /crm proxy is already configured - content = "".join(lines) - if 'location /crm' in content: - print("Proxy path /crm is already configured in Nginx.") - return - - # Find the server block and inject location /crm - # We will locate a spot inside a server block (e.g. right before location /) - injected = False - new_lines = [] - - proxy_block = """ - location /crm { - proxy_pass http://127.0.0.1:8081; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - } -""" - - for line in lines: - if 'location / ' in line or 'location /{' in line or 'location =' in line: - new_lines.append(proxy_block) - injected = True - new_lines.append(line) - - if not injected: - # Fallback: append inside the last closing brace - for i in range(len(lines) - 1, -1, -1): - if '}' in lines[i]: - lines.insert(i, proxy_block) - new_lines = lines - injected = True - break - - if not injected: - print("Could not find a suitable spot to inject location /crm block.") - return - - # Write config to temp file - temp_conf = '/tmp/nginx_crm_test.conf' - with open(temp_conf, 'w') as f: - f.writelines(new_lines) - - # Copy to original file location with sudo try: - subprocess.run(['sudo', 'cp', temp_conf, target_file], check=True) - print("Updated Nginx configuration.") - # Test configuration + # Copy to sites-available + subprocess.run(['sudo', 'cp', source_conf, target_available], check=True) + print(f"Copied {source_conf} to {target_available}") + + # Enable site symlink + if not os.path.exists(target_enabled): + subprocess.run(['sudo', 'ln', '-sf', target_available, target_enabled], check=True) + print(f"Symlinked {target_available} to {target_enabled}") + + # Test Nginx configuration test_res = subprocess.run(['sudo', 'nginx', '-t'], capture_output=True, text=True) if test_res.returncode == 0: subprocess.run(['sudo', 'systemctl', 'reload', 'nginx'], check=True) - print("Nginx reloaded successfully.") + print("Nginx configuration verified and reloaded successfully.") else: - print(f"Nginx config test failed. Reverting changes. Output: {test_res.stderr}") - # Revert (assume we have a backup or we just copy back original - we will copy backup) - # For safety, since we just overwrote it, we should verify before reload. + print(f"Nginx config test failed: {test_res.stderr}") except Exception as e: - print(f"Error updating Nginx config: {e}") + print(f"Error configuring Nginx: {e}") if __name__ == '__main__': main() diff --git a/crmbackend.nginx.conf b/crmbackend.nginx.conf index 9aa5dfd..9585ccf 100644 --- a/crmbackend.nginx.conf +++ b/crmbackend.nginx.conf @@ -1,8 +1,20 @@ server { listen 80; - server_name betaapicsa.tradhox.com apicsa.tradhox.com crmbackend.local; + server_name betaapicsa.tradhox.com apicsa.tradhox.com; location / { + # Handle CORS preflight requests directly in Nginx + if ($request_method = 'OPTIONS') { + add_header 'Access-Control-Allow-Origin' '$http_origin' always; + add_header 'Access-Control-Allow-Credentials' 'true' always; + add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS, PATCH' always; + add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always; + add_header 'Access-Control-Max-Age' 1728000 always; + add_header 'Content-Type' 'text/plain; charset=utf-8' always; + add_header 'Content-Length' 0 always; + return 204; + } + proxy_pass http://127.0.0.1:8081; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;