-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.ts
242 lines (197 loc) · 6.15 KB
/
get.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import 'source-map-support/register';
import { Context } from 'vm';
import * as AWS from 'aws-sdk';
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import S3, { GetObjectRequest, GetObjectOutput } from 'aws-sdk/clients/s3';
const TABLE_NAME = "tower-of-druaga-jawsugdays";
const BUCKET_NAME = "jawsugdays2021";
const FILE_NAME = `${TABLE_NAME}.json`;
interface IDetail {
Condition: string,
Effect: string,
Memo: string,
Name: string
};
interface IFloor {
Type: string,
Floor: number,
Detail: IDetail[]
}
export async function handler(event:APIGatewayProxyEvent, _context:Context):Promise<APIGatewayProxyResult>{
// console.log(`[event] ${JSON.stringify(event)}`);
const response:APIGatewayProxyResult = await main();
return response;
}
export async function main():Promise<APIGatewayProxyResult> {
let response:APIGatewayProxyResult = null;
try {
const dc:DocumentClient = createDocumentClientObject();
const s3:S3 = createS3Object();
// await getDynamoDBDescribe()
// await putDynamoDBData(dc)
await putS3ObjectContents(s3);
const promises:any[] = [scanDynamoDbItems(dc), getS3ObjectContents(s3)];
const results: any[] = await Promise.all(promises);
const items: DocumentClient.ItemList = results[0];
const contents:IFloor = results[1];
response = createResponse(200, "", items, contents);
} catch(error) {
response = createResponse(500, error.message);
}
return response
}
export async function scanDynamoDbItems(_dc:DocumentClient):Promise<DocumentClient.ItemList> {
const param: DocumentClient.ScanInput = {
TableName: TABLE_NAME
};
const data: DocumentClient.ScanOutput = await _dc.scan(param).promise();
// console.info(`[data] ${JSON.stringify(data)}`);
return data.Items;
}
export async function getS3ObjectContents(_s3:S3):Promise<IFloor> {
const param: GetObjectRequest = {
Bucket: BUCKET_NAME,
Key: FILE_NAME,
};
// console.log('getS3ObjectContents params: ' + JSON.stringify(param));
const data:GetObjectOutput = await _s3.getObject(param).promise();
const contents:string = data.Body.toString()
// console.log('getS3ObjectContents contents: ' + contents);
const result = JSON.parse(contents) as IFloor;
return result;
}
/**
* DynamoDB.DocumentClientインスタンスを生成する。
* @return {DocumentClient} DynamoDB.DocumentClientインスタンス
*/
export function createDocumentClientObject():DocumentClient {
let documentClient: DocumentClient = null;
if(process.env.IS_OFFLINE) {
console.info("[getDynamoDbItems] IS_OFFLINE is true.");
documentClient = new AWS.DynamoDB.DocumentClient({
region: 'localhost',
endpoint: 'http://localhost:8000',
});
} else {
console.info("[getDynamoDbItems] IS_OFFLINE is false.");
documentClient = new AWS.DynamoDB.DocumentClient();
}
return documentClient;
}
export function createS3Object():S3 {
let s3:S3 = null;
if(process.env.IS_OFFLINE) {
console.info("[createS3Object] IS_OFFLINE is true.");
s3 = new AWS.S3({
s3ForcePathStyle: true,
accessKeyId: 'S3RVER', // This specific key is required when working offline
secretAccessKey: 'S3RVER',
endpoint: new AWS.Endpoint('http://localhost:4569'),
});
} else {
console.info("[createS3Object] IS_OFFLINE is false.");
s3 = new AWS.S3();
}
return s3;
}
export function createResponse(_code:number, _message:string, _items?:DocumentClient.AttributeMap, _contents?: IFloor): APIGatewayProxyResult {
const response:APIGatewayProxyResult = {
statusCode: _code,
body: JSON.stringify({
message: _message,
items: _items,
contents: _contents
})
};
return response;
}
// DynamoDBテーブルの情報を取得する
export async function getDynamoDBDescribe():Promise<void> {
const dynamo = new AWS.DynamoDB({
endpoint: 'http://localhost:8000'
}
)
const param: DocumentClient.DescribeTableInput = {
TableName: TABLE_NAME
};
const data = await dynamo.describeTable(param).promise();
console.info(`[describe] ${JSON.stringify(data)}`);
return;
}
// DynamoDBにデータを作成する用の関数
export async function putDynamoDBData(_dc:DocumentClient):Promise<void> {
const item:DocumentClient.PutItemInputAttributeMap = {
Type: "treasure",
Floor: 56,
Detail: [
{
Condition: "呪文をアーマーで受ける",
Effect: "なし",
Memo: "宝箱は出現するが、中身が空っぽ",
Name: "なし"
}
]
};
const param: DocumentClient.Put = {
TableName: TABLE_NAME,
Item: item,
};
console.log('putDynamoDBData params: ' + JSON.stringify(param));
await _dc.put(param).promise();
return;
}
// S3バケットにファイルを作成する用の関数
export async function putS3ObjectContents(_s3:S3):Promise<void> {
const contents = [
{
"Type": "treasure",
"Floor": 58,
"Detail": [
{
"Condition": "左から10列目の、上から8行目,2行目,5行目の順に下向きに通過する。",
"Effect": "クリアに必要",
"Memo": "60階の順所と同じ",
"Name": "ブルークリスタルロッド"
}
]
},
{
"Type": "treasure",
"Floor": 59,
"Detail": [
{
"Condition": "",
"Effect": "",
"Memo": "",
"Name": "なし"
}
]
},
{
"Type": "treasure",
"Floor": 60,
"Detail": [
{
"Condition": "",
"Effect": "",
"Memo": "順番は58階と同じ",
"Name": "なし"
}
]
}
];
const param: S3.PutObjectRequest = {
Bucket: BUCKET_NAME,
Key: FILE_NAME,
Body: JSON.stringify(contents)
};
console.log('putS3ObjectContents params: ' + JSON.stringify(param));
const data:S3.PutObjectOutput = await _s3.putObject(param).promise();
console.log('puS3ObjectContents data: ' + JSON.stringify(data));
return;
}
export const exportFuncs = {
scanDynamoDbItems:scanDynamoDbItems,
getS3ObjectContents: getS3ObjectContents
};