Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstjn committed Aug 23, 2017
0 parents commit 02fcd43
Show file tree
Hide file tree
Showing 17 changed files with 3,604 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
dist
75 changes: 75 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting our team at **[email protected]**. As an alternative
feel free to reach out to any of us personally. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MIT License

Copyright (c) 2017 Sebastian Müller <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Serverless Analytics ⚡️

[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)
[![license](https://img.shields.io/github/license/sbstjn/serverless-analytics.svg)](LICENSE.md)

Example project for a personal [serverless](https://serverless.com) *Google Analytics* clone to track website visitors.

After deploying the service you will have a HTTP endpoint using API Gateway that accepts requests and puts them into a Kinesis Stream. A Lambda function processes the stream and writes a basic metric about how many visitors you have per absolute URL to DynamoDB.

## Components

#### Tracking Service

- Amazon Kinesis to stream visitor events
- Amazon API Gateway as HTTP proxy for Kinesis
- Amazon DynamoDB for data storage
- AWS Lambda to process visitor events

#### Examples

- Static website to track visitors
- API & Dashboard to show metrics

## Configuration

All settings can be changes in the `serverless.yml` configuration file. You can easily change the DynamoDB Table, Kinesis Stream and API Gateway Resource names:

```yaml
service: sls-analytics

custom:
names:
bucket:
website: ${self:service}-website-example
dashboard: ${self:service}-website-dashboard
resource: track
dynamodb: ${self:service}-data
kinesis: ${self:service}-stream
```
The S3 Bucket configuration is only needed for the included example website. If you don't need the example sites, have a look at the `scripts/deploy.sh` file and disable the deployment and remove the CloudFormation resources from the `serverless.yml` file.

*Amazon requires unique names for S3 buckets and other resources. Please rename at least the service before you try to deploy the example!*

## Deployment

Running `yarn deploy` will trigger a default [serverless](https://serverless.com) deployment. After the output of the CloudFormation Stack is available, the included static websites will be generated *(Using the hostname from the stack output)* and uploaded to the configured S3 buckets. As the final step, the deploy process will display the URL of the example website and data dashboard:

```bash
# Install dependencies
$ > yarn install
# Deploy
$ > yarn deploy
[…]
Dashboard: http://sls-analytics-website-dashboard.s3-website-us-east-1.amazonaws.com/
Website: http://sls-analytics-website-example.s3-website-us-east-1.amazonaws.com/
```

The **website** includes a simple HTML file, some stylings, and a few JavaScript lines that send a request to your API on every page load. Visit your URL, hit a few times the refresh button and take a look at the DynamoDB table or the **dashboard** URL.

## Tracking

Basically tracking is nothing more than a HTTP request to the API Gateway with a set of payload information *(currently just `url` and `name`)*. Normally you would have a non-JS fallback, like an image e.g., but a simple `fetch` call does the job for now:

```js
fetch(
'https://n6q0egpreh.execute-api.us-east-1.amazonaws.com/v1/track',
{
method: "POST",
body: JSON.stringify( { url: location.href, name: document.title } ),
headers: new Headers(
{
"Content-Type": "application/json"
}
)
}
)
```

## Data Access

You can get a list of all tracked data by invoking the `list` function, or just have a look into your DynamoDB or use the included dashboard.

```bash
$ > sls invoke -f list
[
{
"name": "http://sls-analytics-website-example.s3-website-us-east-1.amazonaws.com/",
"value": 10
},
{
"name": "http://sls-analytics-website-example.s3-website-us-east-1.amazonaws.com/?foo=bar",
"value": 2
}
]
```

## Infrastructure

![Infrastructure](infra.png)

## License

Feel free to use the code, it's released using the [MIT license](LICENSE.md).

## Contribution

You are welcome to contribute to this project! 😘

To make sure you have a pleasant experience, please read the [code of conduct](CODE_OF_CONDUCT.md). It outlines core values and beliefs and will make working together a happier experience.
Binary file added infra.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"license": "MIT",
"scripts": {
"build": "./scripts/build.sh",
"deploy": "./scripts/deploy.sh"
},
"devDependencies": {
"ejs-cli": "^2.0.0",
"node-sass": "^4.5.3",
"s3sync-cli": "^2.0.0",
"serverless": "^1.17.0",
"serverless-dynamodb-autoscaling": "^0.6.1",
"serverless-stack-output": "0.2"
}
}
25 changes: 25 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

case "$1" in
# Compile EJS to HTML for index page
website)
rm -rf dist/website
mkdir -p dist/website

$(npm bin)/ejs-cli "*.ejs" --base-dir sites/website --out dist/website --options .serverless/output.json
$(npm bin)/node-sass --output-style compressed sites/website/style.scss > dist/website/style.css
;;
# Compile SASS to CSS
dashboard)
rm -rf dist/dashboard
mkdir -p dist/dashboard

$(npm bin)/ejs-cli "*.ejs" --base-dir sites/dashboard --out dist/dashboard --options .serverless/output.json
$(npm bin)/node-sass --output-style compressed sites/dashboard/style.scss > dist/dashboard/style.css
;;
# Run `html` and `styles` sub tasks per default
*)
$0 website
$0 dashboard
;;
esac
50 changes: 50 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

case "$1" in
# Deploy service with serverless to AWS
service)
$(npm bin)/serverless deploy
;;
# Deploy content from `dist` folder to S3 bucket
website|dashboard)
# Check for configuration
if [ ! -f .serverless/output.json ]; then
echo "Error: No CloudFormation stack output found!"
echo ""
echo "Please run ./scripts/deploy.sh service"
exit 1
fi

# Run build tasks
echo "Compile static resources …"
echo ""

./scripts/build.sh

BucketNameWebsite=`cat .serverless/output.json | jq -r '.BucketNameWebsite'`
BucketNameDashboard=`cat .serverless/output.json | jq -r '.BucketNameDashboard'`
Region=`cat .serverless/output.json | jq -r '.Region'`

WebsiteURL="http://$BucketNameWebsite.s3-website-$Region.amazonaws.com/"
DashboardURL="http://$BucketNameDashboard.s3-website-$Region.amazonaws.com/"

echo ""
echo "Sync \`dist\` folder with S3 …"
echo ""

# Sync with S3 bucket
$(npm bin)/s3sync -b $BucketNameWebsite -p dist/website

# Sync with S3 bucket
$(npm bin)/s3sync -b $BucketNameDashboard -p dist/dashboard

echo ""
echo "Dashboard: $DashboardURL"
echo "Website: $WebsiteURL"
;;
# Run `service` and `website` sub tasks per default
*)
$0 service
$0 website
;;
esac
Loading

0 comments on commit 02fcd43

Please sign in to comment.