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
fix(sites): route every managed app type on give-subdomain, not just docker
Juan Denis3d31f4e8ea00
diff --git a/backend/app/services/site_domain_service.py b/backend/app/services/site_domain_service.pyindex 2bf5a9e7..3f257a02 100644--- a/backend/app/services/site_domain_service.py+++ b/backend/app/services/site_domain_service.py@@ -139,6 +139,77 @@ class SiteDomainService:except Exception as e:return {'created': False, 'reason': 'error', 'error': str(e)}+ @classmethod+ def _vhost_create_kwargs(cls, app, domains, ssl_cert, ssl_key, force_type=None):+ """Build ``NginxService.create_site(**kwargs)`` for ``app``, or+ ``(None, reason)`` when the app type can't be served by host nginx or is+ missing what it needs (a port for proxied apps, a root for served ones).++ ``force_type`` overrides the ``app_type`` → template choice — a managed+ WordPress site always proxies to its container port even though the row+ says ``wordpress`` (whose stock template is php-fpm, not a proxy).+ """+ t = (force_type or app.app_type or '').lower()+ base = dict(name=app.name, domains=domains, ssl_cert=ssl_cert, ssl_key=ssl_key)+ # Reverse-proxy to a local container/app port.+ if t in ('docker', 'wordpress'):+ if not app.port:+ return None, f'{t} app has no published port to route to.'+ return dict(base, app_type='docker', port=app.port), None+ if t in ('flask', 'django', 'python'):+ if not app.port:+ return None, f'{t} app has no published port to route to.'+ return dict(base, app_type=t, root_path=app.root_path or '', port=app.port), None+ # Serve from a filesystem root.+ if t == 'php':+ if not app.root_path:+ return None, 'php app has no root path to serve.'+ return dict(base, app_type='php', root_path=app.root_path,+ php_version=(getattr(app, 'php_version', None) or '8.2')), None+ if t == 'static':+ if not app.root_path:+ return None, 'static app has no root path to serve.'+ return dict(base, app_type='static', root_path=app.root_path), None+ return None, f"app type '{t}' cannot be published via host nginx."++ @classmethod+ def write_app_vhost(cls, app, force_type=None):+ """(Re)write and enable the host-nginx vhost publishing ``app`` at every+ one of its Domain rows (``server_name`` = all domains).++ Handles all host-nginx app types: docker/wordpress and+ python/flask/django reverse-proxy to the app's port; php/static serve a+ filesystem root. Serves the base-domain wildcard cert when HTTPS is+ enabled and every domain is a covered subdomain (custom domains bring+ their own cert). Best-effort — never raises; returns ``{'nginx',+ 'warning'}`` (``nginx`` is ``None`` when nothing was written).+ """+ from app.models.domain import Domain+ from app.services.nginx_service import NginxService++ domains = [d.name for d in Domain.query.filter_by(application_id=app.id).all()]+ if not domains:+ return {'nginx': None, 'warning': None}++ ssl_cert = ssl_key = None+ if cls.https_enabled() and all(cls.covers(d) for d in domains):+ ssl_cert, ssl_key = cls.wildcard_cert_paths()++ kwargs, reason = cls._vhost_create_kwargs(app, domains, ssl_cert, ssl_key, force_type)+ if reason:+ return {'nginx': None, 'warning': reason}++ try:+ res = NginxService.create_site(**kwargs)+ except Exception as e:+ return {'nginx': None, 'warning': str(e)}+ if not res.get('success'):+ return {'nginx': res, 'warning': f"nginx vhost not created: {res.get('error')}"}+ en = NginxService.enable_site(app.name)+ if not en.get('success'):+ return {'nginx': res, 'warning': f"vhost written but not enabled: {en.get('error')}"}+ return {'nginx': res, 'warning': None}+@classmethoddef give_subdomain(cls, app, label=None):"""One-click 'give this app a subdomain': publish ``app`` at@@ -151,7 +222,6 @@ class SiteDomainService:"""from app import dbfrom app.models.domain import Domain- from app.services.nginx_service import NginxServicebase = cls.base_domain()if not base:@@ -175,21 +245,13 @@ class SiteDomainService:db.session.rollback()return {'success': False, 'error': f'Could not record domain: {e}'}- warning = None- nginx = None- if app.app_type == 'docker' and app.port:- all_domains = [d.name for d in Domain.query.filter_by(application_id=app.id).all()]- ssl_cert, ssl_key = (None, None)- if cls.https_enabled() and all(cls.covers(d) for d in all_domains):- ssl_cert, ssl_key = cls.wildcard_cert_paths()- nginx = NginxService.create_site(- name=app.name, app_type='docker', domains=all_domains,- root_path=app.root_path or '', port=app.port,- ssl_cert=ssl_cert, ssl_key=ssl_key)- if nginx.get('success'):- NginxService.enable_site(app.name)- else:- warning = nginx.get('error')+ # Publish the site: (re)write + enable its host-nginx vhost. Works for+ # every host-nginx app type (docker/wordpress/python proxy to the app+ # port; php/static serve a root), not just docker — a non-routable or+ # misconfigured app degrades to a warning rather than failing the publish.+ v = cls.write_app_vhost(app)+ nginx = v.get('nginx')+ warning = v.get('warning')dns = cls.ensure_site_dns(host)if dns and not dns.get('skipped') and not dns.get('created') and dns.get('message'):diff --git a/backend/app/services/wordpress_service.py b/backend/app/services/wordpress_service.pyindex d1ae2ef2..467f5ca0 100644--- a/backend/app/services/wordpress_service.py+++ b/backend/app/services/wordpress_service.py@@ -2146,37 +2146,19 @@ RewriteRule ^wp-content/uploads/.*\\.php$ - [F]@classmethoddef _write_app_vhost(cls, app) -> Dict:- """Write + enable the nginx reverse-proxy vhost for a docker app from its- current Domain rows (server_name = every domain). Best-effort and never- raises; returns ``{'nginx', 'warning'}``."""- from app.models.domain import Domain- from app.services.nginx_service import NginxService+ """Write + enable the nginx reverse-proxy vhost for a WordPress site from+ its current Domain rows (server_name = every domain). Best-effort and+ never raises; returns ``{'nginx', 'warning'}``.++ Delegates to the shared ``SiteDomainService.write_app_vhost`` writer,+ forcing the docker proxy template: a managed WP site is always served by+ proxying to its container, never via the stock php-fpm ``wordpress``+ template. A portless site is a silent no-op (nothing to proxy to)."""from app.services.site_domain_service import SiteDomainServiceif not app.port:return {'nginx': None, 'warning': None}- domains = [d.name for d in Domain.query.filter_by(application_id=app.id).all()]- if not domains:- return {'nginx': None, 'warning': None}- # Serve the wildcard cert when HTTPS is set up and every vhost domain is a- # managed subdomain it covers (custom domains carry their own cert).- ssl_cert = ssl_key = None- if SiteDomainService.https_enabled() and all(SiteDomainService.covers(d) for d in domains):- ssl_cert, ssl_key = SiteDomainService.wildcard_cert_paths()- try:- res = NginxService.create_site(- name=app.name, app_type='docker', domains=domains,- root_path=app.root_path or '', port=app.port,- ssl_cert=ssl_cert, ssl_key=ssl_key,- )- if res.get('success'):- en = NginxService.enable_site(app.name)- if not en.get('success'):- res['warning'] = f"vhost written but not enabled: {en.get('error')}"- return {'nginx': res, 'warning': None}- return {'nginx': res, 'warning': f"nginx vhost not created: {res.get('error')}"}- except Exception as e:- return {'nginx': None, 'warning': f'nginx vhost error: {e}'}+ return SiteDomainService.write_app_vhost(app, force_type='docker')@classmethoddef _canonical_site_url(cls, app) -> str:diff --git a/backend/tests/test_dns_give_subdomain.py b/backend/tests/test_dns_give_subdomain.pyindex 6f89de8b..05ac153e 100644--- a/backend/tests/test_dns_give_subdomain.py+++ b/backend/tests/test_dns_give_subdomain.py@@ -84,6 +84,90 @@ def test_give_subdomain_wildcard_mode_skips_per_site_record(app, monkeypatch):assert Domain.query.filter_by(name='wild-one.apps.example.com').first() is not None+def _capture_create_site(monkeypatch):+ """Patch NginxService so give_subdomain writes no real vhost but we can see+ exactly which template/params it chose. Returns the captured kwargs dict."""+ from app.services.nginx_service import NginxService+ captured = {}+ monkeypatch.setattr(NginxService, 'create_site',+ staticmethod(lambda **k: (captured.update(k), {'success': True})[1]))+ monkeypatch.setattr(NginxService, 'enable_site', staticmethod(lambda name: {'success': True}))+ return captured+++def test_give_subdomain_routes_php_app_from_root(app, monkeypatch):+ """A managed PHP app (no port, has a root) must get a real php-fpm vhost —+ not the old docker-only skip that left it at localhost only."""+ from app import db+ from app.services.site_domain_service import SiteDomainService++ _set('sites_base_domain', 'apps.example.com')+ a = _mk_app(name='Legacy PHP', port=None)+ a.app_type = 'php'+ a.root_path = '/srv/legacy-php'+ db.session.commit()++ captured = _capture_create_site(monkeypatch)+ res = SiteDomainService.give_subdomain(a)+ assert res['success'] and res.get('warning') is None+ assert captured['app_type'] == 'php'+ assert captured['root_path'] == '/srv/legacy-php'+ assert 'legacy-php.apps.example.com' in captured['domains']+++def test_give_subdomain_routes_static_app_from_root(app, monkeypatch):+ from app import db+ from app.services.site_domain_service import SiteDomainService++ _set('sites_base_domain', 'apps.example.com')+ a = _mk_app(name='Brochure', port=None)+ a.app_type = 'static'+ a.root_path = '/srv/brochure'+ db.session.commit()++ captured = _capture_create_site(monkeypatch)+ res = SiteDomainService.give_subdomain(a)+ assert res['success'] and res.get('warning') is None+ assert captured['app_type'] == 'static'+ assert captured['root_path'] == '/srv/brochure'+++def test_give_subdomain_routes_python_app_by_port(app, monkeypatch):+ from app import db+ from app.services.site_domain_service import SiteDomainService++ _set('sites_base_domain', 'apps.example.com')+ a = _mk_app(name='Flask API', port=9100)+ a.app_type = 'python'+ db.session.commit()++ captured = _capture_create_site(monkeypatch)+ res = SiteDomainService.give_subdomain(a)+ assert res['success'] and res.get('warning') is None+ assert captured['app_type'] == 'python'+ assert captured['port'] == 9100+++def test_give_subdomain_warns_when_app_type_unroutable(app, monkeypatch):+ """A php app with no root can't be served — the publish records the Domain+ but surfaces a warning instead of silently pretending it routed."""+ from app import db+ from app.models.domain import Domain+ from app.services.site_domain_service import SiteDomainService++ _set('sites_base_domain', 'apps.example.com')+ a = _mk_app(name='Broken PHP', port=None)+ a.app_type = 'php'+ a.root_path = None+ db.session.commit()++ _capture_create_site(monkeypatch)+ res = SiteDomainService.give_subdomain(a)+ assert res['success'] and res['nginx'] is None+ assert 'root' in (res.get('warning') or '')+ assert Domain.query.filter_by(name='broken-php.apps.example.com').first() is not None++def test_give_subdomain_rejects_label_taken_by_other_app(app, monkeypatch):from app import dbfrom app.models.domain import Domain