Skip to content
counting things, as microservice
JavaScript
Branch: master
Clone or download
Travis CI
1
Latest commit eb4321e Aug 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src build: force to use src folder convention Aug 14, 2019
.editorconfig build: first commit Jul 28, 2019
.gitattributes build: first commit Jul 28, 2019
.gitignore
.npmrc build: first commit Jul 28, 2019
.travis.yml build: remove coverage Aug 1, 2019
CHANGELOG.md chore(release): 1.0.25 Aug 15, 2019
LICENSE build: first commit Jul 28, 2019
README.md build: tweaks Aug 12, 2019
demo.gif docs: update demo Jul 28, 2019
now.json build: force to use src folder convention Aug 14, 2019
package.json chore(release): 1.0.25 Aug 15, 2019

README.md

count

Last version Build Status Dependency status Dev Dependencies Status

A simple microservice for counting things.

It's inspired by antirez.com and rauchg.com.

Deploy to now

Usage

You can count whatever you want.

Imagine you are visiting a blog post and you want to count views.

Just pass the blog post relative path for counting pageviews:

kikobeats.com/culture-shipping → count.now.sh/pageviews/culture-shipping?incr → HTTP 201 Created

The service will return you the current counter, and the first and last timestamp associated:

{
  "updatedAt": 1564327678670,
  "createdAt": 1564327676653,
  "count": 25
}

For counting thing, you need to perform a GET to count.now.sh/:collection/:id, being supported the following query string:

  • increment|incr: When it is present, it specifies how much increment the value.

It doesn't matter if your id contains final slash; it will be sanitized.

For printing the value in your website, a representative code for doing that could be:

<script>
  fetch('https://count.now.sh' + window.location.pathname + '?increment&collection=pageviews)
    .then(res => res.json())
    .then(({ pageviews }) => {
      document.querySelector('.pageviews-container').classList.remove('display-none')
      document.querySelector('.pageviews-count').textContent = pageviews
    })
</script>

Environment Variables

Database

The service is backed at Firebase. You need to create an account (they are free!) and copy some relevant values you can find in your Firebase accounts tab.

FIRESTORE_PROJECT_ID

Type: string

The project id to use.

FIRESTORE_PRIVATE_KEY

Type: string

Your private key associated with your Firebase account.

It should be encoded in base64.

FIRESTORE_CLIENT_EMAIL

Type: string

Your email associated with your Firebase account.

Protection

The following environment variables are optional and they are used protecting abusive use of the service from external traffic.

API_KEY

Type: string

When you provide it, all request to need to be authenticated in order to increment counters.

The API key can be provided:

  • as x-api-key request header.
  • as key or apiKey as query parameter.

You can use randomkeygen.com for generating an unique API key.

Rate Limit

The following variables are optional and providing them enable a rate limit window associated per request IP address.

RATE_LIMIT_WINDOW

Type: number

It defines a rate limit window, where the value specified is the number of seconds before RATE_LIMIT_MAX points are reset.

The value should be expressed in milliseconds.

RATE_LIMIT_MAX

Type: number

It defines a rate limit window, where the vlaue is the maximum number of points can be consumed over RATE_LIMIT_WINDOW.

License

count-microservice © Kiko Beats, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.

kikobeats.com · GitHub Kiko Beats · Twitter @Kikobeats

You can’t perform that action at this time.