-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetEnvironmentReport.js
More file actions
80 lines (72 loc) · 2.58 KB
/
getEnvironmentReport.js
File metadata and controls
80 lines (72 loc) · 2.58 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
71
72
73
74
75
76
77
78
79
80
const { getEnvironmentById } = require("./db.js");
// 완전히 개방된 CORS 헤더 추가
const addCorsHeaders = (response) => {
response.headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Credentials": true,
"Access-Control-Max-Age": "86400",
"Content-Type": "application/json"
};
return response;
};
exports.handler = async (event) => {
// OPTIONS 요청 처리 (프리플라이트 요청)
const httpMethod = event.requestContext?.http?.method || event.httpMethod || 'GET';
if (httpMethod === 'OPTIONS') {
return addCorsHeaders({
statusCode: 200,
body: ""
});
}
try {
let environmentId;
// Query 파라미터에서 ID 가져오기
if (event.queryStringParameters && event.queryStringParameters.id) {
environmentId = event.queryStringParameters.id;
}
// Path 파라미터에서 ID 가져오기
else if (event.pathParameters && event.pathParameters.id) {
environmentId = event.pathParameters.id;
}
// Body에서 ID 가져오기 (POST 요청인 경우)
else if (event.body) {
const requestBody = JSON.parse(event.body);
environmentId = requestBody.id;
}
else {
return {
statusCode: 400,
body: JSON.stringify({ error: "environment_id가 필요합니다." })
};
}
// 숫자로 변환
environmentId = parseInt(environmentId);
if (isNaN(environmentId) || environmentId <= 0) {
return {
statusCode: 400,
body: JSON.stringify({ error: "유효한 environment_id가 아닙니다." })
};
}
// DB에서 데이터 조회
const environmentData = await getEnvironmentById(environmentId);
if (!environmentData) {
return {
statusCode: 404,
body: JSON.stringify({ error: "해당 ID의 데이터를 찾을 수 없습니다." })
};
}
// 성공적으로 데이터 찾음
return addCorsHeaders({
statusCode: 200,
body: JSON.stringify(environmentData)
});
} catch (error) {
console.error("🚨 환경 데이터 조회 오류:", error);
return {
statusCode: 500,
body: JSON.stringify({ error: "데이터 조회 중 오류 발생" })
};
}
};