Skip to content

Repository files navigation

⭐ Mayu

Moe-Counter Compatible Website Hit Counter

Mayu is a drop-in replacement for Moe-Counter, designed to be lightweight and easy to use.

Mayu is written in Gleam and uses SQLite or PostgreSQL as its database.



Usage

Themes are selectable using the theme query parameter of any get operation.

E.g., mayu.due.moe/get/@demo?theme=urushi

This repository tracks four original themes: garukura, lain, urushi, and yuruyuri. The full Moe-Counter theme collection (asoul, gelbooru, moebooru, rule34, and dozens more) is pulled in by ./scripts/sync-themes.sh, which the Docker image runs at build time. Run it after cloning if you want the full set locally. The script fetches the upstream revision pinned in scripts/sync-themes.sh and copies missing files without replacing or deleting existing local files. To use a local upstream checkout, pass its path as the script's first argument.

Mayu will pad the counter number with zeroes until it reaches a length of 6 characters. You can modify this behaviour by changing the padding query parameter of any get operation, up to a maximum of 12.

Local

$ git clone git@github.com:Fuwn/mayu.git
$ cd mayu
$ gleam run
$ # or
$ nix run

Docker

docker run --volume 'mayu:/mayu/data/' -p '80:3000' --rm fuwn/mayu:latest

This Docker command uses a named volume, mayu, which allows Mayu's database to persist between container restarts. The volume is unnecessary with PostgreSQL, since the data lives in the database server.

Mayu listens on port 3000 unless PORT is set, and binds every interface over both IPv4 and IPv6.

Database

Mayu will use SQLite by default and will place the database file, count.db, within the data/ directory of the project's root directory.

Set MAYU_DATABASE_URL to a postgres:// or postgresql:// URL to use PostgreSQL instead, e.g. postgres://mayu:secret@localhost:5432/mayu. Add ?sslmode=require if your provider requires TLS. Mayu creates its table on start-up, so an empty database is all it needs, and the data/ directory goes unused.

Mayu has the same default database layout as Moe-Counter, so if you've already used Moe-Counter previously, Mayu will work off of any previously accumulated counter data, so long as you transfer the database file over.

Mayu additionally adds two database columns: created_at and updated_at, which will not affect standard operations in any way, but will allow for additional data to be available should you perform a record operation.

Configuration

Mayu is configured through environment variables, all of which are optional.

Variable Description
PORT Port the server listens on. Defaults to 3000.
MAYU_DATABASE_URL A postgres:// or postgresql:// URL, e.g. postgres://mayu:secret@localhost:5432/mayu. When unset, Mayu uses the SQLite file at data/count.db.
MAYU_THEMES Comma-separated allowlist of themes to load, e.g. asoul,garukura. When unset, every theme is loaded. Restricting it lowers memory use and caps the maximum response size.
MAYU_VERSION Version string shown on the index page. Set automatically by the Docker image.
MAYU_PRUNE_MIN_COUNT Counters with fewer than this many hits are eligible for pruning.
MAYU_PRUNE_AFTER_DAYS Counters not incremented within this many days are eligible for pruning.
MAYU_PRUNE_EVERY_HOURS How often, in hours, the prune sweep runs.

Counter pruning is disabled unless all three MAYU_PRUNE_* variables are set to positive integers. When enabled, each sweep deletes counters that are both idle and low: not incremented within MAYU_PRUNE_AFTER_DAYS and below MAYU_PRUNE_MIN_COUNT hits. Actively used counters and high-count counters are always kept, so abandoned throwaway counters are reclaimed without affecting real ones.

Routes

  • /heart-beat: alive
  • /get/@name: An image/svg+xml counter, using asoul when loaded or a loaded fallback theme, modifiable using the theme query parameter
  • /record/@name: Increments the counter and returns a JSON object containing the database's name, num, created_at, and updated_at fields for counter name

Checks

Run gleam test for the local regression checks. PostgreSQL checks run with MAYU_TEST_DATABASE_URL=postgres://postgres@localhost:5432/mayu_test gleam run -m postgres_test; they use a temporary schema inside a transaction that is rolled back afterwards.

python3 test/http_smoke.py http://localhost:3000 checks a running test instance, including concurrent increments. It creates a counter, so use a disposable database. CI runs it against the built container and also checks the Nix build on Linux and macOS.

Resource Attributions

Licence

This project is licensed under the GNU General Public License v3.0.

About

⭐ Optimised Moe-Counter Compatible Website Hit Counter Written in Gleam

Topics

Resources

Stars

24 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages