Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit ca40fcc

Browse files
committed
botway(core): update api routes
1 parent a7c73dc commit ca40fcc

File tree

49 files changed

+3337
-2201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3337
-2201
lines changed

assets/components/Logo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function LogoSection() {
1+
const LogoSection = () => {
22
return (
33
<section className="flex items-start w-full px-4 mx-auto md:px-0 md:items-center md:w-1/3">
44
<div
@@ -15,4 +15,4 @@ export default function LogoSection() {
1515
</div>
1616
</section>
1717
);
18-
}
18+
};

assets/routes/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import LogoSection from "../components/Logo.tsx";
1+
import { LogoSection } from "../components/Logo.tsx";
22
import { Head } from "$fresh/runtime.ts";
33

4-
function App() {
4+
const App = () => {
55
return (
66
<>
77
<Head>
@@ -29,6 +29,6 @@ function App() {
2929
</main>
3030
</>
3131
);
32-
}
32+
};
3333

3434
export default App;

core/app/api/ce/enable/route.ts

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { NextResponse } from "next/server";
22
import { fetcher } from "@/tools/fetch";
3-
import { jwtDecrypt } from "jose";
3+
import { EncryptJWT, jwtDecrypt } from "jose";
44
import { BW_SECRET_KEY } from "@/tools/tokens";
55
import createClient from "@/supabase/server";
6-
import { Octokit } from "octokit";
76
import { faker } from "@faker-js/faker";
87

98
export const revalidate = 0;
@@ -27,33 +26,32 @@ export async function POST(request: Request) {
2726
BW_SECRET_KEY,
2827
);
2928

30-
const { payload: railwayApiToken } = await jwtDecrypt(
31-
user?.user_metadata["railwayApiToken"],
29+
const { payload: zeaburApiToken } = await jwtDecrypt(
30+
user?.user_metadata["zeaburApiToken"],
3231
BW_SECRET_KEY,
3332
);
3433

3534
const { payload: password } = await jwtDecrypt(body.password, BW_SECRET_KEY);
3635

3736
const { data, error: ceError } = await supabase
3837
.from("projects")
39-
.select("railway_project_id, lang")
38+
.select("zeabur_project_id, zeabur_env_id, lang, repo")
4039
.eq("id", body.projectId)
4140
.single();
4241

4342
if (ceError) {
4443
return NextResponse.json({ error: ceError });
4544
}
4645

47-
const { payload: railwayProjectId } = await jwtDecrypt(
48-
data.railway_project_id,
46+
const { payload: zeaburProjectId } = await jwtDecrypt(
47+
data.zeabur_project_id,
4948
BW_SECRET_KEY,
5049
);
5150

52-
const octokit = new Octokit({
53-
auth: githubApiToken.data,
54-
});
55-
56-
const ghu = await (await octokit.request("GET /user", {})).data;
51+
const { payload: zeaburEnvId } = await jwtDecrypt(
52+
data.zeabur_env_id,
53+
BW_SECRET_KEY,
54+
);
5755

5856
let pkgs = "cmake ";
5957

@@ -120,58 +118,83 @@ export async function POST(request: Request) {
120118

121119
const query = `
122120
mutation {
123-
templateDeploy(input: {
124-
services: [
125-
{
126-
hasDomain: true
127-
isPrivate: true
128-
owner: "${ghu.login}"
129-
name: "bwce-${body.slug}-${faker.number.int({ max: 100 })}"
130-
serviceName: "CE"
131-
template: "https://github.com/botwayorg/ce"
132-
variables: {
133-
GIT_REPO: "https://github.com/${body.repo}"
134-
GITHUB_TOKEN: "${githubApiToken.data}"
135-
PASSWORD: "${password.data}"
136-
PKGS: "${pkgs}"
137-
}
138-
volumes: [
139-
{
140-
mountPath: "/root"
141-
projectId: "${railwayProjectId.data}"
142-
}
143-
]
144-
}
145-
]
146-
147-
projectId: "${railwayProjectId.data}"
148-
}) {
149-
projectId
121+
createServiceFromMarketplace(
122+
projectID: "${zeaburProjectId.data}"
123+
itemCode: "botway-ce"
124+
name: "BotwayCE"
125+
) {
126+
_id
127+
createdAt
150128
}
151129
}
152130
`;
153131

154-
const enable = await fetcher("https://backboard.railway.app/graphql/v2", {
132+
const enable = await fetcher("https://gateway.zeabur.com/graphql", {
155133
method: "POST",
156134
headers: {
157135
"Content-Type": "application/json",
158-
Authorization: `Bearer ${railwayApiToken.data}`,
136+
Authorization: `Bearer ${zeaburApiToken.data}`,
159137
},
160138
body: JSON.stringify({
161139
query,
162140
}),
163141
});
164142

165143
if (enable.errors) {
166-
console.log(enable.errors);
167-
168144
return NextResponse.json({ message: enable.errors[0].message });
145+
} else {
146+
const query = `
147+
mutation {
148+
updateEnvironmentVariable(
149+
serviceID: "${enable.data.createServiceFromMarketplace._id}"
150+
environmentID: "${zeaburEnvId.data}"
151+
data: {
152+
GIT_REPO: "https://github.com/${data.repo}"
153+
GH_TOKEN: "${githubApiToken.data}"
154+
PASSWORD: "${password.data}"
155+
PKGS: "${pkgs}"
156+
}
157+
)
158+
159+
addDomain(
160+
serviceID: "${enable.data.createServiceFromMarketplace._id}"
161+
environmentID: "${zeaburEnvId.data}"
162+
domain: "${body.slug + "-" + faker.word.sample()}"
163+
isGenerated: true
164+
) {
165+
_id
166+
domain
167+
}
168+
}
169+
`;
170+
171+
const update = await fetcher("https://gateway.zeabur.com/graphql", {
172+
method: "POST",
173+
headers: {
174+
"Content-Type": "application/json",
175+
Authorization: `Bearer ${zeaburApiToken.data}`,
176+
},
177+
body: JSON.stringify({
178+
query,
179+
}),
180+
});
181+
182+
if (update.errors) {
183+
return NextResponse.json({ message: update.errors[0].message });
184+
}
169185
}
170186

187+
const ce_service_id = await new EncryptJWT({
188+
data: enable.data.createServiceFromMarketplace._id,
189+
})
190+
.setProtectedHeader({ alg: "dir", enc: "A128CBC-HS256" })
191+
.encrypt(BW_SECRET_KEY);
192+
171193
const { error } = await supabase
172194
.from("projects")
173195
.update({
174196
enable_ce: true,
197+
ce_service_id,
175198
})
176199
.eq("id", body.projectId);
177200

core/app/api/ce/route.ts

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,84 +20,58 @@ export async function POST(request: Request) {
2020
return NextResponse.json({ error: userError });
2121
}
2222

23-
const { payload: railwayApiToken } = await jwtDecrypt(
24-
user?.user_metadata["railwayApiToken"],
23+
const { data, error } = await supabase
24+
.from("projects")
25+
.select("zeabur_env_id, ce_service_id")
26+
.eq("id", body.projectId)
27+
.single();
28+
29+
if (error) {
30+
return NextResponse.json({ error });
31+
}
32+
33+
const { payload: zeaburApiToken } = await jwtDecrypt(
34+
user?.user_metadata["zeaburApiToken"],
2535
BW_SECRET_KEY,
2636
);
2737

28-
const { payload: railwayProjectId } = await jwtDecrypt(
29-
body.railwayProjectId,
38+
const { payload: ceServiceId } = await jwtDecrypt(
39+
data.ce_service_id,
40+
BW_SECRET_KEY,
41+
);
42+
43+
const { payload: zeaburEnvId } = await jwtDecrypt(
44+
data.zeabur_env_id,
3045
BW_SECRET_KEY,
3146
);
3247

3348
const query = `
3449
query {
35-
project(id: "${railwayProjectId.data}") {
36-
services {
37-
edges {
38-
node {
39-
id
40-
serviceInstances {
41-
edges {
42-
node {
43-
domains {
44-
customDomains {
45-
domain
46-
}
47-
48-
serviceDomains {
49-
domain
50-
}
51-
}
52-
53-
source {
54-
repo
55-
template {
56-
serviceSource
57-
}
58-
}
59-
}
60-
}
61-
}
62-
}
63-
}
50+
service(_id: "${ceServiceId.data}") {
51+
domains(environmentID: "${zeaburEnvId.data}") {
52+
domain
6453
}
6554
}
6655
}
6756
`;
6857

69-
const check = await fetcher("https://backboard.railway.app/graphql/v2", {
58+
const check = await fetcher("https://gateway.zeabur.com/graphql", {
7059
method: "POST",
7160
headers: {
7261
"Content-Type": "application/json",
73-
Authorization: `Bearer ${railwayApiToken.data}`,
62+
Authorization: `Bearer ${zeaburApiToken.data}`,
7463
},
7564
body: JSON.stringify({
7665
query,
7766
}),
7867
});
7968

8069
if (check.errors) {
81-
console.log(check.errors);
82-
8370
return NextResponse.json({ message: check.errors[0].message });
8471
}
8572

86-
const domainNode = check.data.project.services.edges.find((srv: any) =>
87-
srv.node.serviceInstances.edges.find(
88-
(si: any) =>
89-
si.node.source.template?.serviceSource ===
90-
"https://github.com/botwayorg/ce",
91-
),
92-
).node.serviceInstances.edges[0].node.domains;
93-
94-
let domain;
95-
96-
if (domainNode.customDomains.length != 0) {
97-
domain = domainNode.customDomains[0].domain;
98-
} else {
99-
domain = domainNode.serviceDomains[0].domain;
100-
}
101-
102-
return NextResponse.json({ message: "Success", domain });
73+
return NextResponse.json({
74+
message: "Success",
75+
domain: check.data.service.domains[0].domain,
76+
});
10377
}

0 commit comments

Comments
 (0)