https://github.com/jmp/retro_counter
A simple retro-style visitor counter for websites written in Elixir.
https://github.com/jmp/retro_counter
bandit elixir plug server visitor-counter
Last synced: 7 months ago
JSON representation
A simple retro-style visitor counter for websites written in Elixir.
- Host: GitHub
- URL: https://github.com/jmp/retro_counter
- Owner: jmp
- Created: 2025-02-19T15:26:23.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-03-09T14:36:38.000Z (7 months ago)
- Last Synced: 2025-03-09T15:28:55.640Z (7 months ago)
- Topics: bandit, elixir, plug, server, visitor-counter
- Language: Elixir
- Homepage:
- Size: 42 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RetroCounter
This is a simple retro-style visitor counter for websites.
It started as stupid idea. Who has visitor counters on their websites these days?
But it seemed like a small enough project to learn Elixir, so I decided why not.The original idea was to store the current visitor count in a flat text file.
When a person visits the site, we would read the count from the file, increment
it by one, then write the new count back into the file.There is one obvious problem with this approach. The file may get corrupted with
simultaneous reads and writes.Then I realized Elixir has super powers. I can use a [GenServer][genserver] to hold the count
in memory all the time. It also means I have a single process at the time writing to the file.
As an extra bonus, you can defer writing by some delay (e.g. 30 seconds) to make sure we don't
write to the file on every counter increment.Maybe this approach also has some downsides, but it was very simple to do in Elixir,
and relatively easy to test as well. Especially given that I had zero experience with
ExUnit before.Anyway, this has been an fun learning experience and maybe it'll be interesting for
somebody else as well. So, here you go.## Run using Mix
First, install the dependencies:
mix deps.get
Then run:
mix run --no-halt
Access the counter at http://localhost:4000/count.svg.
## Configuration
You can configure the server with environment variables:
| Environment variable | Default value | Description |
| --------------------------- | ------------- | ------------------------------------------------ |
| `RETRO_COUNTER_PORT` | `4000` | Port for the HTTP server. |
| `RETRO_COUNTER_PATH` | `count.txt` | Path to where the current count will be stored. |
| `RETRO_COUNTER_WRITE_DELAY` | `30000` | Number of milliseconds to defer writing to file. |## Run tests
mix test
[genserver]: https://hexdocs.pm/elixir/GenServer.html