-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
70 lines (64 loc) · 1.85 KB
/
Copy pathroute.ts
File metadata and controls
70 lines (64 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { NextRequest, NextResponse } from "next/server"
import {
copyRepoToExistingContainer,
startDevServerInContainer,
} from "@/lib/utils/container"
import { setupLocalRepository } from "@/lib/utils/utils-server"
export async function POST(req: NextRequest) {
try {
const { repoFullName, containerName } = await req.json()
if (!repoFullName || !containerName) {
return NextResponse.json(
{ error: "Missing repoFullName or containerName" },
{ status: 400 }
)
}
// Make sure local repo exists and get its path
let hostRepoPath: string
try {
hostRepoPath = await setupLocalRepository({ repoFullName })
} catch (err) {
return NextResponse.json(
{ error: `Failed to prepare local repo: ${err}` },
{ status: 400 }
)
}
// Copy the repo into the existing running container
const mountPath = "/workspace"
try {
await copyRepoToExistingContainer({
hostRepoPath,
containerName,
mountPath,
})
} catch (err) {
return NextResponse.json(
{ error: `Failed to copy repo to container: ${err}` },
{ status: 400 }
)
}
// Start a dev server in the background inside the container
try {
await startDevServerInContainer({ containerName, mountPath })
} catch (err) {
// Do not fail the whole request if dev server fails to start; surface as warning
return NextResponse.json(
{ error: `Copied repo but failed to start dev server: ${err}` },
{ status: 202 }
)
}
return NextResponse.json({ success: true })
} catch (err) {
return NextResponse.json(
{
error:
typeof err === "string"
? err
: err instanceof Error
? err.message
: "Unknown error",
},
{ status: 500 }
)
}
}