Skip to content
/ counter Public

A counter API for counting visitors, clicks or anything you want.

License

Notifications You must be signed in to change notification settings

Kynson/counter

Repository files navigation

Counter

Code style: Prettier License: MIT

A counter API for counting visitors, clicks or anything you want.

Usage

Important

It is recommanded to deploy your own instance of counter by cloing this project. The Worker deployed on https://counter.kynsonszetau.com is not intended for public use and counters created maybe deleted without prior notice.

Simply send a GET request to any path. The counter will be incremented, and the latest value after the increment will be returned in JSON. Each path is a separate counter, which makes counting different things a piece of cake.

If a counter is no longer needed, send a DELETE request to the corresponding path to delete it. Note that deleting a counter requires authentication with bearer token.

Deploying your Own Counter

Important

Cloudflare Wokers Paid plan is required to use Durable Objects.

  1. git clone this project
  2. Update wrangler.toml to match your needs (e.g. routes). Remember to uncomment the [[migrations]] section before the first deployment.
  3. Generate a random base64-encoded token
  4. Hash the token with SHA-512 and encode the hash with base64
  5. Upload the hash with wrangler secret put DELETE_TOKEN_HASH
  6. Deploy the worker with npm run deploy

How it works

This API is powered by Cloudflare Workers and Durable Objects. A globally unique counter object will be created when a GET request hits a path for the first time and initialize the counter. Any subsequence GET request will increment the counter for that path. Durbale Objects ensures there is only one running instance for each counter, each increment can be considered atomic.

Acknowledgement

This project is based on Cloudflare's Atomic Counter example.

About

A counter API for counting visitors, clicks or anything you want.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published