-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-post.ecs-task.ts
81 lines (75 loc) · 2.25 KB
/
get-post.ecs-task.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
/* eslint-disable import/no-extraneous-dependencies */
import { Logger } from '@aws-lambda-powertools/logger';
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb';
import { unmarshall } from '@aws-sdk/util-dynamodb';
import AWSXRay from 'aws-xray-sdk';
import express, { Request, Response } from 'express';
const logger = new Logger();
const client = AWSXRay.captureAWSv3Client(new DynamoDBClient({}));
const namespace = process.env.POWERTOOLS_METRICS_NAMESPACE ?? 'test';
const serviceName = process.env.POWERTOOLS_SERVICE_NAME ?? 'get-post';
const metrics = new Metrics({
namespace,
serviceName,
defaultDimensions: {
environment: process.env.ENV ?? 'dev',
},
});
const app = express();
AWSXRay.middleware.setSamplingRules({
version: 2,
default: {
fixed_target: 0,
rate: 0,
},
rules: [
{
host: '*',
fixed_target: 1,
http_method: 'GET',
rate: 1,
url_path: '/posts/*',
},
],
});
app.use(AWSXRay.express.openSegment(serviceName!));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/posts/:id', async (req: Request, res: Response) => {
AWSXRay.getSegment();
try {
const getItem = new GetItemCommand({
Key: {
pk: {
S: req.params.id,
},
},
TableName: process.env.TABLE_NAME,
});
const item = await client.send(getItem);
logger.info({ message: JSON.stringify(item.Item) });
const result = unmarshall(item.Item ?? {});
logger.info({
message: JSON.stringify({ item: item.Item, result: result }),
});
metrics.addMetric('getItemSuccess', MetricUnits.Count, 1);
res.status(200).json(result);
} catch (e) {
metrics.addMetric('getItemFailure', MetricUnits.Count, 1);
logger.error({
message: `error: ${e}`,
});
res.status(404).json({ message: 'Post not found' });
} finally {
metrics.publishStoredMetrics();
}
});
app.get('/', (_req: Request, res: Response) => {
res.status(200).json({ statusCode: 200 });
});
app.get('/health', (_req: Request, res: Response) => {
res.status(200).send(JSON.stringify({ message: 'OK' }));
});
app.use(AWSXRay.express.closeSegment());
export default app;