-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (91 loc) · 2.39 KB
/
index.js
File metadata and controls
102 lines (91 loc) · 2.39 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Driver code
exports.handler = async event => {
var userID = event.rawPath.replace("/", "");
var user = await getItem(userID);
var viewsCount = 1;
if (user != null) {
if (user.Item) {
viewsCount = parseInt(user.Item.count.N);
updateItem(userID, ++viewsCount);
} else {
putItem(userID);
}
}
return buildResponse(viewsCount);
};
const AWS = require("aws-sdk");
const config = require("./config");
AWS.config.update(config.aws_config);
const dynamoDb = new AWS.DynamoDB();
//Return image response with count
async function putItem(userID) {
try {
var params = {
TableName: "github-counter",
Item: {
userid: { S: userID },
count: { N: "1" }
}
};
return await dynamoDb.putItem(params).promise();
} catch (err) {
console.log(err);
return null;
}
}
//put item
async function getItem(userID) {
try {
var params = {
TableName: "github-counter",
Key: {
userid: { S: userID }
}
};
return await dynamoDb.getItem(params).promise();
} catch (err) {
console.log(err);
return null;
}
}
//update item
async function updateItem(userID, count) {
try {
var params = {
ExpressionAttributeNames: {
"#C": "count"
},
ExpressionAttributeValues: {
":c": {
N: `${count.toString()}`
}
},
Key: {
userid: {
S: userID
}
},
ReturnValues: "ALL_NEW",
TableName: "github-counter",
UpdateExpression: "SET #C = :c"
};
return await dynamoDb.updateItem(params).promise().Item;
} catch (err) {
console.log(err);
}
}
async function buildResponse(viewsCount) {
var response = {
statusCode: 200,
body: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="180" height="25" role="img">
<rect x="0" y="0" rx="0" ry="0" width="180" height="25" style="fill:Teal;stroke:black;stroke-width:2;opacity:1"/>
<rect x="0" y="0" rx="0" ry="0" width="105" height="25" style="fill:black;stroke:black;stroke-width:0;opacity:1"/>
<text fill="#ffffff" font-size="15" font-family="Verdana" x="3" y="17">Profile views: ${viewsCount}</text>
</svg>`,
headers: {
"Content-Type": "image/svg+xml",
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate"
}
};
return response;
}