Skip to content

Commit 521e8fe

Browse files
authored
Merge pull request #253 from FalkorDB/create-logs-and-secure-missing-variables
Fix #252 add logs and secure missing variables
2 parents f4391bf + 4b007b1 commit 521e8fe

File tree

8 files changed

+125
-52
lines changed

8 files changed

+125
-52
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
cache: 'no-store'
@@ -25,6 +33,7 @@ export async function POST(request: NextRequest, { params }: { params: { graph:
2533

2634
return NextResponse.json({ result: json }, { status: 200 })
2735
} catch (err) {
28-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
36+
console.error(err)
37+
return NextResponse.json((err as Error).message, { status: 400 })
2938
}
3039
}
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
cache: 'no-store'
2330
})
@@ -30,6 +37,7 @@ export async function POST(request: NextRequest, { params }: { params: { graph:
3037

3138
return NextResponse.json({ result: json }, { status: 200 })
3239
} catch (err) {
33-
return NextResponse.json({ massage: (err as Error).message }, { status: 200 })
40+
console.error(err)
41+
return NextResponse.json((err as Error).message, { status: 400 })
3442
}
3543
}

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
cache: 'no-store'
@@ -21,7 +27,8 @@ export async function GET(request: NextRequest, { params }: { params: { graph: s
2127

2228
return NextResponse.json({ result: json }, { status: 200 })
2329
} catch (err) {
24-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
30+
console.error(err)
31+
return NextResponse.json((err as Error).message, { status: 400 })
2532
}
2633
}
2734

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
cache: 'no-store'
@@ -21,6 +27,7 @@ export async function GET(request: NextRequest, { params }: { params: { graph: s
2127

2228
return NextResponse.json({ result: json }, { status: 200 })
2329
} catch (err) {
24-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
30+
console.error(err)
31+
return NextResponse.json((err as Error).message, { status: 400 })
2532
}
2633
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
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
cache: 'no-store'
1725
})
@@ -20,6 +28,7 @@ export async function POST(request: NextRequest, { params }: { params: { graph:
2028

2129
return NextResponse.json({ result: json }, { status: 200 })
2230
} catch (err) {
23-
return NextResponse.json({ massage: (err as Error).message }, { status: 400 })
31+
console.error(err)
32+
return NextResponse.json((err as Error).message, { status: 400 })
2433
}
2534
}

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
cache: 'no-store'
@@ -51,8 +60,7 @@ export async function POST(request: NextRequest, { params }: { params: { graph:
5160

5261
return NextResponse.json({ result: json }, { status: 200 })
5362
} catch (err) {
54-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
63+
console.error(err)
64+
return NextResponse.json((err as Error).message, { status: 400 })
5565
}
56-
57-
5866
}

app/api/repo/route.ts

Lines changed: 26 additions & 14 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
cache: 'no-store'
1114
})
@@ -18,31 +21,40 @@ export async function GET() {
1821

1922
return NextResponse.json({ result: repositories }, { status: 200 })
2023
} catch (err) {
21-
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
24+
console.error(err)
25+
return NextResponse.json((err as Error).message, { status: 400 })
2226
}
2327
}
2428

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

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

29-
// try {
30-
// const result = await fetch(`${process.env.BEAKEND_URL}/process_repo`, {
41+
// const result = await fetch(`${url}/process_repo`, {
3142
// method: 'POST',
32-
// body: JSON.stringify({ repo_url: url, ignore: ["./.github", "./sbin", "./.git", "./deps", "./bin", "./build"] }),
43+
// body: JSON.stringify({ repo_url, ignore: ["./.github", "./sbin", "./.git", "./deps", "./bin", "./build"] }),
3344
// headers: {
34-
// "Authorization": process.env.SECRET_TOKEN!,
45+
// "Authorization": token,
3546
// 'Content-Type': 'application/json'
36-
// },
47+
// },
3748
// cache: 'no-store'
38-
// })
49+
// });
3950

4051
// if (!result.ok) {
41-
// throw new Error(await result.text())
52+
// throw new Error(await result.text());
4253
// }
4354

44-
// return NextResponse.json({ message: "success" }, { status: 200 })
55+
// return NextResponse.json({ message: "success" }, { status: 200 });
4556
// } catch (err) {
46-
// return NextResponse.json({ message: (err as Error).message }, { status: 400 })
57+
// console.error(err)
58+
// return NextResponse.json((err as Error).message, { status: 400 });
4759
// }
4860
// }

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)