Skip to content

Latest commit

 

History

History

deploy

Folders and files

README.md

Deploying GamesLab

One Go binary on the Pi, listening on a Unix socket. nginx serves the static files and proxies everything else to it. No ports, no container, no runtime dependencies — the binary is static and pure Go, so scp and systemctl restart is the whole deployment.

Files here:

deploy.sh build and ship, from your machine
gameslab.service the systemd unit
nginx/gameslab.henhen1227.com the site — the whole of it
docker-compose.yml, Caddyfile the container route, if you ever want it

How it is wired

One domain, one origin, split by path rather than by hostname:

gameslab.henhen1227.com
  /assets/*   -> disk (content-hashed, cached a year)
  /pieces/*   -> disk
  /ws         -> socket   (WebSocket)
  /api/*      -> socket
  /           -> socket   (the Go server writes the <head>)
  /g/<game>   -> socket        "        "        "

The whole mechanism is try_files $uri @backend: on the disk, serve it; otherwise it is a page and the backend answers.

Two things follow, both of which you want. Shared links unfurl properly — /g/<game> is the URL this site exists to have pasted, and the game's name and its one line are written into the <head> before the bytes leave, because a scraper never runs the JavaScript that would otherwise put them there. og:image is made absolute from X-Forwarded-Proto and X-Forwarded-Host at the same time. And there is no CORS anywhere, because the browser never leaves the origin it loaded from.

Only the built frontend goes under the site root. nginx serves any file that is there, so the binary, the database and the specs live in gameslab-backend/, which no root points at.

First time

Directories. The site root and the backend are siblings, deliberately:

sudo mkdir -p /var/www/production/henhen1227/gameslab.henhen1227.com /var/www/production/henhen1227/gameslab-backend/data && sudo chown -R ubuntu:www-data /var/www/production/henhen1227/gameslab.henhen1227.com /var/www/production/henhen1227/gameslab-backend && sudo chmod -R 2775 /var/www/production/henhen1227/gameslab.henhen1227.com /var/www/production/henhen1227/gameslab-backend

Owned by the account that deploys, group-writable to the account that serves: the upload arrives over ssh as you, and the service writes its database as www-data. 2775 is the setgid bit, so files created in here inherit the www-data group instead of depending on whoever made them — the same arrangement the parent directory already has.

Scoped to the two new directories rather than -R on the parent, which holds your other sites.

Copy gameslab.service to /etc/systemd/system/, then:

sudo systemctl daemon-reload && sudo systemctl enable gameslab

Do not start it yet — there is no binary. Copy nginx/gameslab.henhen1227.com over the generated one in /etc/nginx/sites-available/production/, confirm the symlink in sites-enabled, then:

sudo nginx -t && sudo systemctl reload nginx

Let the deploy restart the service without a password prompt — the ssh session is not interactive, so a prompt just hangs:

printf 'ubuntu ALL=(root) NOPASSWD: %s restart gameslab\n' "$(command -v systemctl)" | sudo tee /etc/sudoers.d/gameslab && sudo chmod 440 /etc/sudoers.d/gameslab

Back on your machine, set SSH_HOST to your ssh-config alias and ship:

cp deploy/deploy.env.example deploy/deploy.env
./deploy/deploy.sh

That builds both halves, uploads them, starts the service and asks the socket whether it came up. Then TLS, last, once the site answers over port 80:

sudo certbot --nginx -d gameslab.henhen1227.com

certbot rewrites the vhost in place, adding the 443 block and the redirect. The config here is plain port 80 for exactly that reason.

Every time after

./deploy/deploy.sh

Or ./deploy/deploy.sh backend / frontend for one half. Checks run first; SKIP_CHECKS=1 skips them, which is for when you already ran them and not for when they're failing.

The binary goes up as gameslab.incoming and is renamed into place, because you can't write to a running executable on Linux and a rename is atomic. The database lives in data/ and is never touched by a deploy.

sudo systemctl restart has to work without a password prompt — the ssh session isn't interactive, so a prompt just hangs. One sudoers line does it:

www-data ALL=(root) NOPASSWD: /bin/systemctl restart gameslab

Or set SUDO= in deploy.env if you deploy as root.

Things that matter

GAMESLAB_DEV must stay off. It opens the Lab's "save as default", which rewrites a seeded game's file in specs/ — on a public site, a stranger editing your library. The unit doesn't set it. Confirm after any change:

curl -s https://gameslab.henhen1227.com/api/health

"dev": false is the one to look at. The rest tells you how many games are published, how many people are waiting for an opponent, and how many are live.

Back up data/. It's the whole of the site's state — the published library, finished games, votes. SQLite in WAL mode, so copy it with the database's own tooling rather than cp:

sqlite3 /var/www/production/henhen1227/gameslab-backend/data/gameslab.db ".backup '/tmp/gameslab-$(date +%F).db'"

Seeding runs on every start and is idempotent. Correct a spec in specs/, deploy, and the running library picks it up. A spec that doesn't validate is refused and logged rather than stored.

The WebSocket needs its timeouts. The vhosts set proxy_read_timeout 3600s. Without it nginx cuts the connection at 60 seconds and boards stop moving mid-game. The server pings every 54s, so the connection stays warm; the timeout is only the ceiling.

Watch it:

sudo journalctl -u gameslab -f

The container route

Still here and still works, if you ever want it on something that isn't the Pi:

docker build -t gameslab . && docker run -p 8080:8080 -v gameslab-data:/data gameslab

docker-compose.yml and Caddyfile bring up the same thing with automatic TLS. Nothing in the systemd path depends on them.