-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ced9e90
commit 0e093af
Showing
7 changed files
with
563 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM public.ecr.aws/docker/library/node:18 AS build | ||
WORKDIR /srv | ||
ADD package.json . | ||
RUN npm install | ||
|
||
FROM public.ecr.aws/docker/library/node:18-slim | ||
COPY --from=build /srv . | ||
ADD . . | ||
EXPOSE 80 | ||
CMD ["node", "index.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
var aws = require('aws-sdk'); | ||
const express = require('express'); | ||
const app = express(); | ||
const table = process.env.HITS_NAME; | ||
const PORT = 80; // The port to listen on | ||
|
||
const dynamodb = new aws.DynamoDB.DocumentClient(); | ||
|
||
if (!table) { | ||
console.error('Warning: The HITS_NAME environment variable needs to be set with the name of a DynamoDB table'); | ||
} | ||
|
||
const os = require('os'); | ||
const hostname = os.hostname(); | ||
|
||
app.get('*', function (req, res) { | ||
if (!table) { | ||
console.error('Error: The HITS_NAME environment variable needs to be set with the name of a DynamoDB table'); | ||
return res.send('Error: The HITS_NAME environment variable needs to be set with the name of a DynamoDB table'); | ||
} | ||
|
||
dynamodb.update({ | ||
TableName: table, | ||
Key: { | ||
counter: 'global', | ||
}, | ||
UpdateExpression: 'SET hitCount = if_not_exists(hitCount, :zero) + :value', | ||
ExpressionAttributeValues: { | ||
':zero': 0, | ||
':value': 1, | ||
}, | ||
ReturnValues: 'ALL_NEW' | ||
}, function (err, results) { | ||
if (err) { | ||
return res.send(err); | ||
} else { | ||
var hitCount = results.Attributes.hitCount; | ||
res.send(`There have been ${hitCount} hits. (${hostname})`); | ||
} | ||
}); | ||
}); | ||
|
||
app.listen(PORT, () => console.log(`Listening on port ${PORT}!`)); | ||
|
||
// This causes the process to respond to "docker stop" faster | ||
process.on('SIGTERM', function () { | ||
console.log('Received SIGTERM, shutting down'); | ||
app.close(); | ||
}); |
Oops, something went wrong.