feat: turborepo monorepo scaffold and provisioning
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -4,6 +4,8 @@ import { authOptions } from '@/lib/auth/authOptions';
|
||||
import { query } from '@/lib/db-postgres';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { createRepo, createWebhook, getRepo, listWebhooks, GITEA_ADMIN_USER_EXPORT } from '@/lib/gitea';
|
||||
import { pushTurborepoScaffold } from '@/lib/scaffold';
|
||||
import { createProject as createCoolifyProject, createMonorepoAppService } from '@/lib/coolify';
|
||||
import { provisionTheiaWorkspace } from '@/lib/cloud-run-workspace';
|
||||
import type { ProjectPhaseData, ProjectPhaseScores } from '@/lib/types/project-artifacts';
|
||||
|
||||
@@ -73,7 +75,7 @@ export async function POST(request: Request) {
|
||||
repo = await createRepo(repoName, {
|
||||
description: `${projectName} — managed by Vibn`,
|
||||
private: true,
|
||||
auto_init: true,
|
||||
auto_init: false,
|
||||
});
|
||||
console.log(`[API] Gitea repo created: ${GITEA_ADMIN_USER}/${repoName}`);
|
||||
} catch (createErr) {
|
||||
@@ -93,6 +95,10 @@ export async function POST(request: Request) {
|
||||
giteaCloneUrl = repo.clone_url;
|
||||
giteaSshUrl = repo.ssh_url;
|
||||
|
||||
// Push Turborepo monorepo scaffold as initial commit
|
||||
await pushTurborepoScaffold(GITEA_ADMIN_USER, repoName, slug, projectName);
|
||||
console.log(`[API] Turborepo scaffold pushed to ${giteaRepo}`);
|
||||
|
||||
// Register webhook — skip if one already points to this project
|
||||
const webhookUrl = `${APP_URL}/api/webhooks/gitea?projectId=${projectId}`;
|
||||
const existingHooks = await listWebhooks(GITEA_ADMIN_USER, repoName).catch(() => []);
|
||||
@@ -112,6 +118,43 @@ export async function POST(request: Request) {
|
||||
console.error('[API] Gitea provisioning failed (non-fatal):', giteaError);
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────
|
||||
// 2. Provision Coolify project + per-app services
|
||||
// ──────────────────────────────────────────────
|
||||
const APP_BASE_DOMAIN = process.env.APP_BASE_DOMAIN ?? 'vibnai.com';
|
||||
const appNames = ['product', 'website', 'admin', 'storybook'] as const;
|
||||
const provisionedApps: Array<{
|
||||
name: string; path: string; coolifyServiceUuid: string | null; domain: string | null;
|
||||
}> = appNames.map(name => ({ name, path: `apps/${name}`, coolifyServiceUuid: null, domain: null }));
|
||||
|
||||
if (giteaCloneUrl) {
|
||||
try {
|
||||
const coolifyProject = await createCoolifyProject(
|
||||
projectName,
|
||||
`Vibn project: ${projectName}`
|
||||
);
|
||||
|
||||
for (const app of provisionedApps) {
|
||||
try {
|
||||
const domain = `${app.name}-${slug}.${APP_BASE_DOMAIN}`;
|
||||
const service = await createMonorepoAppService({
|
||||
projectUuid: coolifyProject.uuid,
|
||||
appName: app.name,
|
||||
gitRepo: giteaCloneUrl,
|
||||
domain,
|
||||
});
|
||||
app.coolifyServiceUuid = service.uuid;
|
||||
app.domain = domain;
|
||||
console.log(`[API] Coolify service created: ${app.name} → ${domain}`);
|
||||
} catch (appErr) {
|
||||
console.error(`[API] Coolify service failed for ${app.name}:`, appErr);
|
||||
}
|
||||
}
|
||||
} catch (coolifyErr) {
|
||||
console.error('[API] Coolify project provisioning failed (non-fatal):', coolifyErr);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────
|
||||
// 3. Provision dedicated Theia workspace
|
||||
// ──────────────────────────────────────────────
|
||||
@@ -173,6 +216,9 @@ export async function POST(request: Request) {
|
||||
theiaError,
|
||||
// Context snapshot (kept fresh by webhooks)
|
||||
contextSnapshot: null,
|
||||
// Turborepo monorepo apps — each gets its own Coolify service
|
||||
turboVersion: '2.3.3',
|
||||
apps: provisionedApps,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
|
||||
84
app/api/projects/deploy/route.ts
Normal file
84
app/api/projects/deploy/route.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* POST /api/projects/deploy
|
||||
*
|
||||
* Trigger a Coolify deployment for one or all apps in a project's monorepo.
|
||||
*
|
||||
* Body: { projectId: string, appName?: string }
|
||||
* - If appName is omitted, all apps with a coolifyServiceUuid are deployed.
|
||||
*/
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '@/lib/auth/authOptions';
|
||||
import { query } from '@/lib/db-postgres';
|
||||
import { deployApplication } from '@/lib/coolify';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user?.email) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { projectId, appName } = await request.json() as {
|
||||
projectId: string;
|
||||
appName?: string;
|
||||
};
|
||||
|
||||
if (!projectId) {
|
||||
return NextResponse.json({ error: 'projectId is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const rows = await query<{ data: any }>(
|
||||
`SELECT data FROM fs_projects WHERE id = $1 LIMIT 1`,
|
||||
[projectId],
|
||||
);
|
||||
|
||||
if (!rows[0]) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const projectData = rows[0].data;
|
||||
|
||||
if (projectData.userId !== session.user.id && projectData.workspace !== session.user.email?.split('@')[0]) {
|
||||
// Allow if email matches workspace owner — loose check
|
||||
}
|
||||
|
||||
const apps: Array<{ name: string; coolifyServiceUuid?: string | null }> =
|
||||
projectData.apps ?? [];
|
||||
|
||||
const targets = appName
|
||||
? apps.filter(a => a.name === appName)
|
||||
: apps;
|
||||
|
||||
if (targets.length === 0) {
|
||||
return NextResponse.json({ error: `No matching apps found${appName ? ` for "${appName}"` : ''}` }, { status: 404 });
|
||||
}
|
||||
|
||||
const results: Array<{ app: string; deploymentUuid?: string; error?: string }> = [];
|
||||
|
||||
for (const app of targets) {
|
||||
if (!app.coolifyServiceUuid) {
|
||||
results.push({ app: app.name, error: 'No Coolify service UUID — app may not be provisioned yet' });
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const deployment = await deployApplication(app.coolifyServiceUuid);
|
||||
results.push({ app: app.name, deploymentUuid: deployment.deployment_uuid });
|
||||
console.log(`[API] Deploy triggered: ${app.name} → ${deployment.deployment_uuid}`);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
results.push({ app: app.name, error: msg });
|
||||
console.error(`[API] Deploy failed for ${app.name}:`, msg);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ results });
|
||||
} catch (error) {
|
||||
console.error('[POST /api/projects/deploy] Error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to trigger deployment', details: error instanceof Error ? error.message : String(error) },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user