Skip to content

Commit 2e24de9

Browse files
committed
add logs and secure missing variables
1 parent cacb58d commit 2e24de9

File tree

8 files changed

+124
-51
lines changed

8 files changed

+124
-51
lines changed

app/api/chat/[graph]/route.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { NextRequest, NextResponse } from "next/server"
2+
import { getEnvVariables } from "../../utils"
23

34

45
export async function POST(request: NextRequest, { params }: { params: { graph: string } }) {
56

6-
const graphName = params.graph
7+
const repo = params.graph
78
const msg = request.nextUrl.searchParams.get('msg')
89

910
try {
10-
const result = await fetch(`${process.env.BACKEND_URL}/chat`, {
11+
12+
if (!msg) {
13+
throw new Error("Message parameter is required")
14+
}
15+
16+
const { url, token } = getEnvVariables()
17+
18+
const result = await fetch(`${url}/chat`, {
1119
method: 'POST',
12-
body: JSON.stringify({ repo: graphName, msg }),
20+
body: JSON.stringify({ repo, msg }),
1321
headers: {
14-
"Authorization": process.env.SECRET_TOKEN!,
22+
"Authorization": token,
1523
"Content-Type": 'application/json'
1624
}
1725
})
@@ -24,6 +32,7 @@ export async function POST(request: NextRequest, { params }: { params: { graph:
2432

2533
return NextResponse.json({ result: json }, { status: 200 })
2634
} catch (err) {
27-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
35+
console.error(err)
36+
return NextResponse.json((err as Error).message, { status: 400 })
2837
}
2938
}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1+
import { getEnvVariables } from "@/app/api/utils";
12
import { NextRequest, NextResponse } from "next/server";
23

34
export async function POST(request: NextRequest, { params }: { params: { graph: string, node: string } }) {
45

5-
const nodeId = params.node;
6-
const graphId = params.graph;
7-
const targetId = request.nextUrl.searchParams.get('targetId')
6+
const repo = params.graph;
7+
const src = Number(params.node);
8+
const dest = Number(request.nextUrl.searchParams.get('targetId'))
89

910
try {
1011

11-
const result = await fetch(`${process.env.BACKEND_URL}/find_paths`, {
12+
if (!dest) {
13+
throw new Error("targetId is required");
14+
}
15+
16+
const { url, token } = getEnvVariables()
17+
18+
const result = await fetch(`${url}/find_paths`, {
1219
method: 'POST',
1320
headers: {
14-
"Authorization": process.env.SECRET_TOKEN!,
21+
"Authorization": token,
1522
'Content-Type': 'application/json'
1623
},
1724
body: JSON.stringify({
18-
repo: graphId,
19-
src: Number(nodeId),
20-
dest: Number(targetId!)
25+
repo,
26+
src,
27+
dest
2128
})
2229
})
2330

@@ -29,6 +36,7 @@ export async function POST(request: NextRequest, { params }: { params: { graph:
2936

3037
return NextResponse.json({ result: json }, { status: 200 })
3138
} catch (err) {
32-
return NextResponse.json({ massage: (err as Error).message }, { status: 200 })
39+
console.error(err)
40+
return NextResponse.json((err as Error).message, { status: 400 })
3341
}
3442
}

app/api/repo/[graph]/commit/route.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
import { getEnvVariables } from "@/app/api/utils";
12
import { NextRequest, NextResponse } from "next/server";
23

34
export async function GET(request: NextRequest, { params }: { params: { graph: string } }) {
45

6+
const repo = params.graph
7+
58
try {
6-
const result = await fetch(`${process.env.BEAKEND_URL}/list_commits`, {
9+
10+
const { url, token } = getEnvVariables()
11+
12+
const result = await fetch(`${url}/list_commits`, {
713
method: 'POST',
8-
body: JSON.stringify({ repo: params.graph }),
14+
body: JSON.stringify({ repo }),
915
headers: {
10-
"Authorization": process.env.SECRET_TOKEN!,
16+
"Authorization": token,
1117
"Content-Type": 'application/json'
1218
}
1319
})
@@ -20,7 +26,8 @@ export async function GET(request: NextRequest, { params }: { params: { graph: s
2026

2127
return NextResponse.json({ result: json }, { status: 200 })
2228
} catch (err) {
23-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
29+
console.error(err)
30+
return NextResponse.json((err as Error).message, { status: 400 })
2431
}
2532
}
2633

app/api/repo/[graph]/info/route.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { NextRequest, NextResponse } from "next/server";
2+
import { getEnvVariables } from "@/app/api/utils";
23

34
export async function GET(request: NextRequest, { params }: { params: { graph: string } }) {
45

6+
const repo = params.graph
7+
58
try {
6-
const result = await fetch(`${process.env.BACKEND_URL}/repo_info`, {
9+
10+
const { url, token } = getEnvVariables();
11+
12+
const result = await fetch(`${url}/repo_info`, {
713
method: 'POST',
8-
body: JSON.stringify({ repo: params.graph }),
14+
body: JSON.stringify({ repo }),
915
headers: {
10-
"Authorization": process.env.SECRET_TOKEN!,
16+
"Authorization": token,
1117
"Content-Type": 'application/json'
1218
}
1319
})
@@ -20,6 +26,7 @@ export async function GET(request: NextRequest, { params }: { params: { graph: s
2026

2127
return NextResponse.json({ result: json }, { status: 200 })
2228
} catch (err) {
23-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
29+
console.error(err)
30+
return NextResponse.json((err as Error).message, { status: 400 })
2431
}
2532
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
import { NextRequest, NextResponse } from "next/server";
2+
import { getEnvVariables } from "@/app/api/utils";
23

34
export async function POST(request: NextRequest, { params }: { params: { graph: string } }) {
45

5-
const nodeIds = (await request.json()).nodeIds.map((id: string) => Number(id));
6-
const graphId = params.graph;
7-
6+
const repo = params.graph;
7+
const node_ids = (await request.json()).nodeIds.map((id: string) => Number(id));
8+
89
try {
9-
const result = await fetch(`${process.env.BACKEND_URL}/get_neighbors`, {
10+
11+
const { url, token } = getEnvVariables();
12+
13+
if (node_ids.length === 0) {
14+
throw new Error("nodeIds is required");
15+
}
16+
17+
const result = await fetch(`${url}/get_neighbors`, {
1018
method: 'POST',
11-
body: JSON.stringify({ node_ids: nodeIds, repo: graphId }),
19+
body: JSON.stringify({ node_ids, repo }),
1220
headers: {
1321
"Content-Type": 'application/json',
14-
"Authorization": process.env.SECRET_TOKEN!,
22+
"Authorization": token,
1523
}
1624
})
1725

1826
const json = await result.json()
1927

2028
return NextResponse.json({ result: json }, { status: 200 })
2129
} catch (err) {
22-
return NextResponse.json({ massage: (err as Error).message }, { status: 400 })
30+
console.error(err)
31+
return NextResponse.json((err as Error).message, { status: 400 })
2332
}
2433
}

app/api/repo/[graph]/route.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { NextRequest, NextResponse } from "next/server"
2+
import { getEnvVariables } from "../../utils"
23

34
export async function GET(request: NextRequest, { params }: { params: { graph: string } }) {
45

56
const graphName = params.graph
67

78
try {
8-
const result = await fetch(`${process.env.BACKEND_URL}/graph_entities?repo=${graphName}`, {
9+
10+
const { url, token } = getEnvVariables()
11+
12+
const result = await fetch(`${url}/graph_entities?repo=${graphName}`, {
913
method: 'GET',
1014
headers: {
11-
"Authorization": process.env.SECRET_TOKEN!,
15+
"Authorization": token,
1216
}
1317
})
1418

@@ -20,24 +24,29 @@ export async function GET(request: NextRequest, { params }: { params: { graph: s
2024

2125
return NextResponse.json({ result: json }, { status: 200 })
2226
} catch (err) {
23-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
27+
console.error(err)
28+
return NextResponse.json((err as Error).message, { status: 400 })
2429
}
2530
}
2631

2732
export async function POST(request: NextRequest, { params }: { params: { graph: string } }) {
2833

34+
const repo = params.graph
2935
const prefix = request.nextUrl.searchParams.get('prefix')!
3036

3137
try {
38+
3239
if (!prefix) {
3340
throw new Error("Prefix is required")
3441
}
3542

36-
const result = await fetch(`${process.env.BACKEND_URL}/auto_complete`, {
43+
const { url, token } = getEnvVariables()
44+
45+
const result = await fetch(`${url}/auto_complete`, {
3746
method: 'POST',
38-
body: JSON.stringify({ repo: params.graph, prefix }),
47+
body: JSON.stringify({ repo, prefix }),
3948
headers: {
40-
"Authorization": process.env.SECRET_TOKEN!,
49+
"Authorization": token,
4150
"Content-Type": 'application/json'
4251
}
4352
})
@@ -50,8 +59,7 @@ export async function POST(request: NextRequest, { params }: { params: { graph:
5059

5160
return NextResponse.json({ result: json }, { status: 200 })
5261
} catch (err) {
53-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
62+
console.error(err)
63+
return NextResponse.json((err as Error).message, { status: 400 })
5464
}
55-
56-
5765
}

app/api/repo/route.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { NextRequest, NextResponse } from "next/server";
1+
import { NextResponse } from "next/server";
2+
import { getEnvVariables } from "../utils";
23

34
export async function GET() {
45
try {
5-
const result = await fetch(`${process.env.BACKEND_URL}/list_repos`, {
6+
7+
const { url, token } = getEnvVariables()
8+
const result = await fetch(`${url}/list_repos`, {
69
method: 'GET',
710
headers: {
8-
"Authorization": process.env.SECRET_TOKEN!,
11+
"Authorization": token,
912
}
1013
})
1114

@@ -17,30 +20,39 @@ export async function GET() {
1720

1821
return NextResponse.json({ result: repositories }, { status: 200 })
1922
} catch (err) {
20-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
23+
console.error(err)
24+
return NextResponse.json((err as Error).message, { status: 400 })
2125
}
2226
}
2327

2428
// export async function POST(request: NextRequest) {
29+
30+
// const repo_url = request.nextUrl.searchParams.get('url');
31+
32+
// try {
2533

26-
// const url = request.nextUrl.searchParams.get('url');
34+
// if (!repo_url) {
35+
// throw new Error("URL parameter is missing");
36+
// }
37+
38+
// const { url, token } = getEnvVariables();
2739

28-
// try {
29-
// const result = await fetch(`${process.env.BEAKEND_URL}/process_repo`, {
40+
// const result = await fetch(`${url}/process_repo`, {
3041
// method: 'POST',
31-
// body: JSON.stringify({ repo_url: url, ignore: ["./.github", "./sbin", "./.git", "./deps", "./bin", "./build"] }),
42+
// body: JSON.stringify({ repo_url, ignore: ["./.github", "./sbin", "./.git", "./deps", "./bin", "./build"] }),
3243
// headers: {
33-
// "Authorization": process.env.SECRET_TOKEN!,
44+
// "Authorization": token,
3445
// 'Content-Type': 'application/json'
3546
// }
36-
// })
47+
// });
3748

3849
// if (!result.ok) {
39-
// throw new Error(await result.text())
50+
// throw new Error(await result.text());
4051
// }
4152

42-
// return NextResponse.json({ message: "success" }, { status: 200 })
53+
// return NextResponse.json({ message: "success" }, { status: 200 });
4354
// } catch (err) {
44-
// return NextResponse.json({ message: (err as Error).message }, { status: 400 })
55+
// console.error(err)
56+
// return NextResponse.json((err as Error).message, { status: 400 });
4557
// }
4658
// }

app/api/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function getEnvVariables() {
2+
const url = process.env.BACKEND_URL
3+
const token = process.env.SECRET_TOKEN
4+
5+
if (!url) {
6+
throw new Error("Environment variable BACKEND_URL must be set");
7+
}
8+
if (!token) {
9+
throw new Error("Environment variable SECRET_TOKEN must be set");
10+
}
11+
12+
return { url, token }
13+
}

0 commit comments

Comments
 (0)