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
refactor(unify): §7 — WordPress reachable through the generic /apps surface
Juan Denis92c435d43640
diff --git a/backend/app/api/apps.py b/backend/app/api/apps.pyindex 09e82a36..1aa6ad50 100644--- a/backend/app/api/apps.py+++ b/backend/app/api/apps.py@@ -2023,3 +2023,113 @@ def delete_app_backup(app_id, run_id):except BackupPolicyError as e:return jsonify({'error': str(e)}), 400return jsonify({'success': True})+++# --------------------------------------------------------------------------- #+# WordPress reachable through the generic application surface (§7 unification).+# A WordPress site is a 1:1 extension of an Application (WordPressSite.+# application_id); the WordPressSite record — not app_type — is the source of+# truth (see wordpress.py:_is_wp_app). These additive routes expose the WP+# linkage and its DB snapshots under /apps/:id without changing app_type, so the+# docker-compose code paths that key on app_type=='docker' keep working.+# --------------------------------------------------------------------------- #++def _wp_site_for_app(app):+ from app.models.wordpress_site import WordPressSite+ return WordPressSite.query.filter_by(application_id=app.id).first()+++@apps_bp.route('/<int:app_id>/wordpress', methods=['GET'])+@jwt_required()+def app_wordpress_info(app_id):+ """Whether this app is a WordPress site, and the linked site id. Lets the+ generic Services surface discover the WordPress extension."""+ app, err = _load_app_for_backup(app_id)+ if err:+ return err+ site = _wp_site_for_app(app)+ if not site:+ return jsonify({'is_wordpress': False, 'site_id': None}), 200+ return jsonify({+ 'is_wordpress': True,+ 'site_id': site.id,+ 'environment_type': site.environment_type,+ 'is_production': site.is_production,+ 'wp_version': site.wp_version,+ }), 200+++@apps_bp.route('/<int:app_id>/db-snapshots', methods=['GET'])+@jwt_required()+def list_app_db_snapshots(app_id):+ """List the WordPress DB snapshots for an app (unified app-id surface)."""+ app, err = _load_app_for_backup(app_id)+ if err:+ return err+ from app.models.wordpress_site import DatabaseSnapshot+ site = _wp_site_for_app(app)+ if not site:+ return jsonify({'error': 'This application is not a WordPress site'}), 400+ snaps = (DatabaseSnapshot.query.filter_by(site_id=site.id)+ .order_by(DatabaseSnapshot.created_at.desc()).all())+ return jsonify({'snapshots': [s.to_dict() for s in snaps], 'total': len(snaps)}), 200+++@apps_bp.route('/<int:app_id>/db-snapshots', methods=['POST'])+@jwt_required()+def create_app_db_snapshot(app_id):+ """Create a WordPress DB snapshot for an app."""+ app, err = _load_app_for_backup(app_id, edit=True)+ if err:+ return err+ from app.models.wordpress_site import DatabaseSnapshot+ from app.services.db_sync_service import DatabaseSyncService+ from app.services.wordpress_env_service import WordPressEnvService+ site = _wp_site_for_app(app)+ if not site:+ return jsonify({'error': 'This application is not a WordPress site'}), 400+ data = request.get_json() or {}+ result = DatabaseSyncService.create_snapshot(+ db_name=site.db_name,+ name=data.get('name', f'{app.name}_{site.id}'),+ tag=data.get('tag'),+ commit_sha=site.last_deploy_commit,+ host=site.db_host, user=site.db_user,+ password=WordPressEnvService._get_db_password(site),+ exclude_tables=data.get('exclude_tables', []),+ )+ if not result.get('success'):+ return jsonify(result), 500+ snap_meta = result['snapshot']+ snapshot = DatabaseSnapshot(+ site_id=site.id, name=snap_meta['name'], tag=data.get('tag'),+ file_path=snap_meta['file_path'], size_bytes=snap_meta['size_bytes'],+ compressed=snap_meta['compressed'], commit_sha=site.last_deploy_commit,+ tables_included=json.dumps(snap_meta.get('tables', [])),+ row_count=snap_meta.get('row_count', 0), status='completed',+ )+ db.session.add(snapshot)+ db.session.commit()+ DatabaseSyncService.upload_snapshot_offsite(snapshot.file_path)+ return jsonify({'success': True, 'snapshot': snapshot.to_dict()}), 201+++@apps_bp.route('/<int:app_id>/db-snapshots/<int:snapshot_id>', methods=['DELETE'])+@jwt_required()+def delete_app_db_snapshot(app_id, snapshot_id):+ """Delete a WordPress DB snapshot for an app."""+ app, err = _load_app_for_backup(app_id, edit=True)+ if err:+ return err+ from app.models.wordpress_site import DatabaseSnapshot+ from app.services.db_sync_service import DatabaseSyncService+ site = _wp_site_for_app(app)+ if not site:+ return jsonify({'error': 'This application is not a WordPress site'}), 400+ snapshot = DatabaseSnapshot.query.filter_by(id=snapshot_id, site_id=site.id).first()+ if not snapshot:+ return jsonify({'error': 'Snapshot not found'}), 404+ DatabaseSyncService.delete_snapshot(snapshot.file_path)+ db.session.delete(snapshot)+ db.session.commit()+ return jsonify({'success': True, 'message': 'Snapshot deleted'}), 200diff --git a/backend/tests/test_app_wordpress_surface.py b/backend/tests/test_app_wordpress_surface.pynew file mode 100644index 00000000..b53e6a07--- /dev/null+++ b/backend/tests/test_app_wordpress_surface.py@@ -0,0 +1,87 @@+"""§7 unification — WordPress reachable through the generic /apps surface.++A WordPress site is a 1:1 extension of an Application. These additive routes+expose the linkage (/apps/:id/wordpress) and the WP DB snapshots+(/apps/:id/db-snapshots) under the app id WITHOUT changing app_type, so the+docker-compose paths that key on app_type=='docker' are unaffected.+"""+import pytest+++def _seed_app(app, *, wordpress=True):+ from app import db+ from app.models import User+ from app.models.application import Application+ from app.models.wordpress_site import WordPressSite+ with app.app_context():+ admin = User.query.filter_by(username='testadmin').first()+ a = Application(name='wp-app', app_type='docker', status='running',+ root_path='/srv/wp', user_id=admin.id if admin else 1)+ db.session.add(a)+ db.session.commit()+ site_id = None+ if wordpress:+ site = WordPressSite(application_id=a.id, db_name='wpdb', db_user='wpuser',+ db_host='localhost', is_production=True,+ environment_type='production', wp_version='6.5')+ db.session.add(site)+ db.session.commit()+ site_id = site.id+ return a.id, site_id+++def test_app_wordpress_info_true(app, client, auth_headers):+ app_id, site_id = _seed_app(app, wordpress=True)+ res = client.get(f'/api/v1/apps/{app_id}/wordpress', headers=auth_headers)+ assert res.status_code == 200+ body = res.get_json()+ assert body['is_wordpress'] is True+ assert body['site_id'] == site_id+ assert body['is_production'] is True+++def test_app_wordpress_info_false_for_plain_app(app, client, auth_headers):+ app_id, _ = _seed_app(app, wordpress=False)+ res = client.get(f'/api/v1/apps/{app_id}/wordpress', headers=auth_headers)+ assert res.status_code == 200+ assert res.get_json()['is_wordpress'] is False+++def test_db_snapshots_rejects_non_wordpress(app, client, auth_headers):+ app_id, _ = _seed_app(app, wordpress=False)+ res = client.get(f'/api/v1/apps/{app_id}/db-snapshots', headers=auth_headers)+ assert res.status_code == 400+++def test_db_snapshots_list_empty(app, client, auth_headers):+ app_id, _ = _seed_app(app, wordpress=True)+ res = client.get(f'/api/v1/apps/{app_id}/db-snapshots', headers=auth_headers)+ assert res.status_code == 200+ assert res.get_json()['snapshots'] == []+++def test_db_snapshot_create_and_delete(app, client, auth_headers, monkeypatch):+ from app.services.db_sync_service import DatabaseSyncService+ from app.services.wordpress_env_service import WordPressEnvService++ monkeypatch.setattr(WordPressEnvService, '_get_db_password', staticmethod(lambda site: 'pw'))+ monkeypatch.setattr(DatabaseSyncService, 'create_snapshot', staticmethod(+ lambda **kw: {'success': True, 'snapshot': {+ 'name': kw.get('name'), 'file_path': '/var/backups/snap.sql.gz',+ 'size_bytes': 1234, 'compressed': True, 'tables': ['wp_posts'], 'row_count': 10,+ }}))+ monkeypatch.setattr(DatabaseSyncService, 'upload_snapshot_offsite', staticmethod(lambda p: None))+ monkeypatch.setattr(DatabaseSyncService, 'delete_snapshot', staticmethod(lambda p: True))++ app_id, _ = _seed_app(app, wordpress=True)+ created = client.post(f'/api/v1/apps/{app_id}/db-snapshots',+ json={'name': 'nightly', 'tag': 'manual'}, headers=auth_headers)+ assert created.status_code == 201, created.get_json()+ snap_id = created.get_json()['snapshot']['id']++ listed = client.get(f'/api/v1/apps/{app_id}/db-snapshots', headers=auth_headers)+ assert listed.get_json()['total'] == 1++ deleted = client.delete(f'/api/v1/apps/{app_id}/db-snapshots/{snap_id}', headers=auth_headers)+ assert deleted.status_code == 200+ assert client.get(f'/api/v1/apps/{app_id}/db-snapshots', headers=auth_headers).get_json()['total'] == 0