mayu
⭐ Mayu
Moe-Counter Compatible Website Hit Counter
Mayu is a drop-in replacement for Moe-Counter. It serves the same counter routes and reads Moe-Counter's existing SQLite database.
Mayu commits every hit to the database as it arrives, so a crashed or killed process loses no counts. An idle Mayu container uses 4 MiB of memory, because Mayu reads a theme into memory only when it first serves that theme. An idle container from Moe-Counter's official image uses 90 MiB. Mayu's Docker image is 31 MB compressed, and Moe-Counter's is 84 MB.
Mayu is written in Crystal and stores counters in SQLite or PostgreSQL.
Usage
The theme query parameter of a get request selects the theme, for example mayu.due.moe/get/@demo?theme=urushi.
This repository tracks four original themes: garukura, lain, urushi, and yuruyuri. ./scripts/sync-themes.sh adds the rest of the Moe-Counter theme collection, including asoul, gelbooru, moebooru, and rule34. The Docker image runs the script at build time; run it yourself after cloning to get the full set locally.
Mayu compiles the themes in ./themes into its binary, so rebuild after adding or changing a theme.
The script fetches the upstream revision pinned inside it and copies only files that are missing locally. It never replaces or deletes local files. To copy from a local upstream checkout instead, pass its path as the first argument.
Mayu pads the count with leading zeroes to 6 digits. The padding query parameter of a get request changes this, up to a maximum of 16.
Mayu implements only the theme and padding parameters. It ignores Moe-Counter's other render parameters: num, offset, align, scale, pixelated, darkmode, and prefix. It does not implement theme=random. Moe-Counter pads to 7 digits by default and uses moebooru as its default theme, where Mayu uses asoul. Moe-Counter also answers cross-origin requests, and Mayu does not.
Local
$ git clone git@github.com:Fuwn/mayu.git
$ cd mayu
$ shards install
$ shards build
$ ./bin/mayu
Building needs Crystal, pkg-config, and the SQLite and OpenSSL development headers. With Nix, nix develop provides them. The schema needs SQLite 3.37 or newer, because the table is strict and the upsert uses returning.
Docker
docker run --volume 'mayu:/mayu/data/' -p '80:3000' --rm fuwn/mayu:latest
The named volume, mayu, keeps the SQLite database across container restarts. PostgreSQL deployments do not need it, because 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
By default, Mayu stores counters in SQLite at data/count.db, relative to the working directory. In the Docker image, that is /mayu/data/count.db.
To use PostgreSQL instead, set MAYU_DATABASE_URL to a postgres:// or postgresql:// URL, for example postgres://mayu:secret@localhost:5432/mayu. For TLS, add ?sslmode=verify-full. Mayu then checks that the server's certificate names the host and comes from a certificate authority the system trusts. If your provider uses its own certificate authority, mount its certificate and set sslrootcert to that file, for example ?sslmode=verify-full&sslrootcert=/mayu/root.crt. A container whose HOME is unset and whose user ID has no passwd entry must set sslrootcert, for example to system. Otherwise the driver looks for ~/.postgresql/root.crt and fails.
sslmode=require encrypts the connection without checking the certificate. Mayu logs a warning at start-up when it connects to a remote host without verify-ca or verify-full. Mayu creates its table on start-up, so an empty database is enough.
Mayu uses Moe-Counter's table layout. To keep existing counts, copy Moe-Counter's database file to data/count.db before starting Mayu.
On start-up, Mayu adds created_at and updated_at columns if the table lacks them. The record route returns these timestamps; counters that existed before the columns were added report an empty created_at.
Configuration
Mayu reads its configuration from environment variables. All of them are optional.
| Variable | Description |
|---|---|
PORT |
Port the server listens on. Defaults to 3000. |
MAYU_DATABASE_URL |
A postgres:// or postgresql:// URL. When unset or empty, Mayu uses the SQLite file at data/count.db. |
MAYU_THEMES |
Comma-separated list of themes to serve, for example asoul,garukura. When unset or empty, Mayu serves every theme it was built with. The list limits which themes visitors can select. It does not save memory, because Mayu never reads an unserved theme into memory. Leaving out lain, urushi, and yuruyuri lowers the largest possible response. |
Pruning
Mayu never deletes counters. To prune idle, low-count counters, run one of these statements on a schedule, for example from cron. Both delete counters with fewer than 10 hits and no hits in the past 30 days. They skip counters with no updated_at, which have not been hit since Mayu added its timestamp columns.
-- SQLite (data/count.db)
delete from tb_count
where num < 10 and updated_at < datetime('now', '-30 days');
-- PostgreSQL
delete from tb_count
where num < 10
and updated_at < to_char(now() at time zone 'utc' - interval '30 days',
'YYYY-MM-DD HH24:MI:SS');
You can run the SQLite statement while Mayu is running. Deleting rows does not shrink the database file.
Routes
/serves the homepage, which builds embed snippets for a counter. Its theme list omits themes whose names end in-h, except the default theme./get/@nameincrements counternameand returns it as animage/svg+xmlimage./@name, Moe-Counter's canonical embed path, does the same. Both accept thethemeandpaddingquery parameters. Without a validtheme, Mayu usesasoulif it is loaded. Otherwise it uses the first loaded theme in alphabetical order whose name does not end in-h, or the first theme when every name does./record/@nameincrements counternameand returns itsname,num,created_at, andupdated_atas JSON./heart-beatreturnsalive.
Counter names must be 1 to 64 bytes long as written in the URL, so %20 counts as three bytes. The demo counter always shows 1234567890 and is never stored. Mayu lets clients cache its image for a year, unless the requested theme is not loaded or no theme is loaded. Any other path redirects to this repository.
Checks
crystal tool format --check src spec checks formatting, and crystal spec runs the SQLite and request tests.
MAYU_TEST_DATABASE_URL=postgres://postgres@localhost:5432/mayu_test crystal spec also runs the PostgreSQL tests, which are pending without that variable. They work in a temporary schema inside a transaction, then roll the transaction back so the database keeps its previous state.
test/start_tls_postgres.sh /tmp/mayu-tls starts a PostgreSQL container on port 5433 with a certificate that names only localhost. Set MAYU_TEST_TLS_DATABASE_URL to run the TLS test, which checks that sslmode=verify-full accepts localhost and refuses 127.0.0.1:
MAYU_TEST_TLS_DATABASE_URL='postgres://postgres@localhost:5433/mayu_test?sslrootcert=/tmp/mayu-tls/ca.crt' crystal spec
docker rm -f mayu-tls-postgres removes the container afterwards.
python3 test/http_smoke.py http://localhost:3000 checks a running instance, including concurrent increments. It creates a counter, so point it at a disposable database. CI runs it against the built container.
Resource Attributions
- A-SOUL_Official
- Moebooru
- Rule 34 (NSFW)
- Gelbooru (NSFW)
- Urushi
- Lain Iwakura
- GaruKura
Licence
This project is licensed under the GNU General Public License v3.0.
mayu
- 26
- 2
- 0
- 0
- 3
- 40 minutes ago
- May 14, 2024
GNU General Public License v3.0
Wed, 02 Dec 2026 10:13:02 GMT