ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
Add Docker app nginx config automation and port detection
Juan Denis8d9f7f383f17
diff --git a/backend/app/api/domains.py b/backend/app/api/domains.pyindex a84a55c1..2145d22c 100644--- a/backend/app/api/domains.py+++ b/backend/app/api/domains.py@@ -102,9 +102,31 @@ def create_domain():db.session.add(domain)db.session.commit()+ # Auto-create nginx config for Docker apps+ nginx_result = None+ if app.app_type == 'docker' and app.port:+ # Get all domains for this app to include in nginx config+ all_domains = [d.name for d in Domain.query.filter_by(application_id=application_id).all()]++ # Create nginx site config+ nginx_result = NginxService.create_site(+ name=app.name,+ app_type='docker',+ domains=all_domains,+ root_path=app.root_path or '',+ port=app.port+ )++ # Enable the site if creation was successful+ if nginx_result.get('success'):+ enable_result = NginxService.enable_site(app.name)+ if not enable_result.get('success'):+ nginx_result['warning'] = f"Site created but not enabled: {enable_result.get('error')}"+return jsonify({'message': 'Domain created successfully',- 'domain': domain.to_dict()+ 'domain': domain.to_dict(),+ 'nginx': nginx_result}), 201@@ -156,10 +178,35 @@ def delete_domain(domain_id):if user.role != 'admin' and app.user_id != current_user_id:return jsonify({'error': 'Access denied'}), 403+ application_id = domain.application_iddb.session.delete(domain)db.session.commit()- return jsonify({'message': 'Domain deleted successfully'}), 200+ # Update nginx config for Docker apps+ nginx_result = None+ if app.app_type == 'docker' and app.port:+ remaining_domains = [d.name for d in Domain.query.filter_by(application_id=application_id).all()]++ if remaining_domains:+ # Update nginx config with remaining domains+ nginx_result = NginxService.create_site(+ name=app.name,+ app_type='docker',+ domains=remaining_domains,+ root_path=app.root_path or '',+ port=app.port+ )+ if nginx_result.get('success'):+ NginxService.reload()+ else:+ # No domains left, disable and delete the site+ NginxService.disable_site(app.name)+ nginx_result = NginxService.delete_site(app.name)++ return jsonify({+ 'message': 'Domain deleted successfully',+ 'nginx': nginx_result+ }), 200@domains_bp.route('/<int:domain_id>/ssl/enable', methods=['POST'])@@ -313,3 +360,54 @@ def get_ssl_status():'total_certificates': len(certificates),'certificates': certificates}), 200+++@domains_bp.route('/nginx/regenerate/<int:app_id>', methods=['POST'])+@jwt_required()+@admin_required+def regenerate_nginx_config(app_id):+ """Regenerate nginx config for a Docker app."""+ app = Application.query.get(app_id)++ if not app:+ return jsonify({'error': 'Application not found'}), 404++ if app.app_type != 'docker':+ return jsonify({'error': 'This endpoint is only for Docker apps'}), 400++ if not app.port:+ return jsonify({'error': 'Application does not have a port configured'}), 400++ # Get all domains for this app+ domains = [d.name for d in Domain.query.filter_by(application_id=app_id).all()]++ if not domains:+ return jsonify({'error': 'No domains configured for this application'}), 400++ # Create nginx site config+ result = NginxService.create_site(+ name=app.name,+ app_type='docker',+ domains=domains,+ root_path=app.root_path or '',+ port=app.port+ )++ if not result.get('success'):+ return jsonify({'error': result.get('error', 'Failed to create nginx config')}), 400++ # Enable the site+ enable_result = NginxService.enable_site(app.name)+ if not enable_result.get('success'):+ return jsonify({+ 'warning': 'Config created but not enabled',+ 'error': enable_result.get('error'),+ 'config': result+ }), 200++ return jsonify({+ 'message': f'Nginx config regenerated for {app.name}',+ 'domains': domains,+ 'port': app.port,+ 'config': result+ }), 200diff --git a/backend/app/services/nginx_service.py b/backend/app/services/nginx_service.pyindex d060ffa4..40fbf84a 100644--- a/backend/app/services/nginx_service.py+++ b/backend/app/services/nginx_service.py@@ -109,6 +109,32 @@ class NginxService:log_not_found off;}}}}+'''++ # Docker reverse proxy template (for containerized apps)+ DOCKER_SITE_TEMPLATE = '''server {{+ listen 80;+ listen [::]:80;+ server_name {domains};++ access_log /var/log/nginx/{name}.access.log;+ error_log /var/log/nginx/{name}.error.log;++ location / {{+ proxy_pass http://127.0.0.1:{port};+ proxy_http_version 1.1;+ proxy_set_header Upgrade $http_upgrade;+ proxy_set_header Connection 'upgrade';+ 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;+ proxy_cache_bypass $http_upgrade;+ proxy_read_timeout 86400;+ proxy_connect_timeout 60;+ proxy_send_timeout 60;+ }}+}}'''SSL_BLOCK = '''@@ -297,6 +323,14 @@ class NginxService:root_path=root_path,port=port)+ elif app_type == 'docker':+ if not port:+ return {'success': False, 'error': 'Port is required for Docker apps'}+ config = cls.DOCKER_SITE_TEMPLATE.format(+ name=name,+ domains=domains_str,+ port=port+ )elif app_type == 'static':config = cls.STATIC_SITE_TEMPLATE.format(name=name,diff --git a/backend/app/services/template_service.py b/backend/app/services/template_service.pyindex af22f1ee..96d7e755 100644--- a/backend/app/services/template_service.py+++ b/backend/app/services/template_service.py@@ -409,6 +409,16 @@ class TemplateService:)# Create application record+ # Look for port in variables - templates may use PORT or HTTP_PORT+ app_port = None+ for port_var in ['PORT', 'HTTP_PORT', 'WEB_PORT']:+ if port_var in variables:+ try:+ app_port = int(variables[port_var])+ break+ except (ValueError, TypeError):+ pass+app = Application(name=app_name,app_type='docker',@@ -416,7 +426,7 @@ class TemplateService:root_path=app_path,docker_image=template.get('name'),user_id=user_id or 1,- port=int(variables.get('PORT', 0)) if 'PORT' in variables else None+ port=app_port)db.session.add(app)db.session.commit()