Core API Worker
Relevant source files
The following files were used as context for generating this wiki page:
The Core API Worker (internally identified as lp-factory-api) serves as the primary backend control plane for the FusionOps ecosystem. Deployed as a Cloudflare Worker, it manages campaign CRUD operations, landing page configurations, and template management. It acts as the central hub connecting the Astro-based dashboard UI to the underlying data persistence layers and external operational tools.
Sources: CLAUDE.md, DEPLOYMENT_SUMMARY.md, PRODUCTION_STATUS.md
Architecture and Infrastructure
The worker is built on the Cloudflare Workers platform using the Wrangler CLI for deployment and configuration. It is designed for high performance with a target startup time of 25ms and utilizes a modular handler-based architecture to process incoming requests.
Primary Bindings
The API Worker is integrated with several Cloudflare services via environment bindings:
- D1 Database: A Cloudflare SQLite instance named
fusionops-main-new-v2containing 24 tables for core application data. - R2 Bucket: Named
lp-factory-thumbs, used for storing template thumbnails. - Browser Rendering API: Enabled for automated screenshot and preview generation tasks.
Sources: apps/api-worker/wrangler.toml:17-38, DEPLOYMENT_SUMMARY.md, PRODUCTION_STATUS.md
Data Flow Overview
The following diagram illustrates the high-level data flow between the API Worker and other system components.
The diagram shows the Core API Worker as the central orchestrator between the UI, storage layers, and external service providers.
Sources: CLAUDE.md, INFRASTRUCTURE_SETUP.md
API Endpoints and Routing
The worker exposes a series of RESTful endpoints under the /api prefix. Requests are routed to specific logic handlers based on the URL path.
Core Managed Resources
| Endpoint | Method | Description |
|---|---|---|
/api/health | GET | Validates worker status and connectivity. |
/api/campaigns | GET/POST | Lists existing campaigns or creates a new tracking campaign. |
/api/campaigns/:id | GET/PUT | Retrieves or updates specific campaign metadata. |
/api/templates | GET | Lists available landing page templates from the registry. |
/api/templates/:id | GET | Retrieves a specific template package. |
/api/settings | GET/POST | Manages global application settings, including Neon URL sync. |
Sources: CLAUDE.md, src/services/api.js, apps/api-worker/src/worker.js:130-150
Request Lifecycle
The API Worker follows a standard lifecycle for processing requests, including authentication checks and CORS handling.
The lifecycle ensures all requests are validated against the API_SECRET and handled via secure database queries.
Sources: apps/api-worker/src/worker.js:20-50, SECURITY_AUDIT_DEEP.md
Database Schema and Persistence
The worker primarily interacts with the fusionops-main D1 database. This database contains 24 tables categorized into core, operations, financial, and deployment tracking groups.
Key Table Groups
| Group | Tables |
|---|---|
| Core | settings, sites, templates, site_versions |
| Operations | ops_accounts, ops_domains, ops_profiles, ops_logs |
| Deployments | deploys, ops_deployments, vps_deploys |
| Financial | monthly_pnl, opex, reconcile_records |
Sources: PRODUCTION_STATUS.md, INFRASTRUCTURE_SETUP.md:120-145
Dual-Database Synchronization
While D1 is the primary store for worker operations, the system supports synchronization with Neon Postgres for dashboard data. The API Worker handles writing connection details like the neonUrl to the settings table after successful connections are established in the UI.
Sources: INFRASTRUCTURE_SETUP.md:85-110
Security and Configuration
The worker's operational behavior is defined in wrangler.toml and secured through several mechanisms.
Configuration Parameters
| Parameter | Value / Source | Description |
|---|---|---|
name | lp-factory-api | Deployment name on Cloudflare. |
compatibility_date | 2024-11-12 | Compatibility flag for Worker runtime features. |
API_SECRET | Wrangler Secret | Bearer token required for /api/analysis and sensitive routes. |
VITE_API_BASE | Env Variable | Publicly exposed URL for client-side API calls. |
Sources: apps/api-worker/wrangler.toml:1-15, CLAUDE.md, SECURITY_AUDIT_DEEP.md
CORS Policy
The worker implements a standard CORS policy to allow interaction with the Astro frontend. Current headers permit GET, POST, PUT, PATCH, DELETE, OPTIONS methods and a variety of headers including Authorization, x-api-key, and x-csrf-token.
Sources: apps/api-worker/src/worker.js:10-25, SECURITY_AUDIT_DEEP.md
Summary
The Core API Worker is the backbone of the FusionOps platform, providing the necessary orchestration logic for campaign management and landing page deployment. By leveraging Cloudflare's serverless infrastructure and D1's SQLite capabilities, it maintains a low-latency environment for operational tasks while ensuring data integrity across both internal and external databases. Its role is critical in bridging the gap between the user dashboard and the various tracking and deployment providers utilized by the project.
Sources: CLAUDE.md, DEPLOYMENT_SUMMARY.md