# Hosting on Mythic Beasts

The platform is a Node.js server, not a set of pages, so it cannot be dropped into a
folder of the existing website. Mythic Beasts' shared hosting hands a **whole hostname**
to a running application through a Unix socket
([their guide](https://www.mythic-beasts.com/support/hosting/python); it is written for
Python but works for any server). So the platform gets its own hostname:

    https://platform.fenlandpropertyholdings.co.uk

and the public website links to it. The public site is not touched otherwise.

Throughout, `VHOST` means `platform.fenlandpropertyholdings.co.uk`. If you choose a
different name, change it in `deploy/fenland-platform.service` too.

## 1. Create the hostname (Mythic Beasts control panel)

1. Add a website for `platform.fenlandpropertyholdings.co.uk` on the same hosting
   account as the main site, with HTTPS (Let's Encrypt) switched on.
2. Add the DNS record for `platform` if the panel does not do it for you.
3. Make sure shell (SSH) access is enabled on the account.

## 2. Check Node on the server

    ssh YOURUSER@margay.mythic-beasts.com
    node -v

The platform needs **Node 22 or later**. If the command is missing or older, install a
private copy with [nvm](https://github.com/nvm-sh/nvm) (`nvm install 22`), then put the
full path from `which node` into `ExecStart=` in the service file in place of
`/usr/bin/env node`. If neither works, ask Mythic Beasts support; a small VPS is the
fallback.

## 3. Copy the platform up

From this folder on your PC. The app lives in your home directory, **outside** `www`,
so nothing in it is ever served directly.

    scp -r lib public scripts deploy server.js package.json package-lock.json YOURUSER@margay.mythic-beasts.com:fenland-platform/

To take your existing records with you, also copy `data` (leave it out to start empty):

    scp -r data YOURUSER@margay.mythic-beasts.com:fenland-platform/

Do not copy `node_modules` or `.env`.

## 4. Set it up on the server

    cd ~/fenland-platform
    npm ci --omit=dev
    chmod 700 . data

Create the sign-in. You are prompted for the password (12 characters minimum); it is
stored hashed in `data/users.json`:

    npm run user -- add will

Add the Claude key (optional; needed for legal pack review and report commentary):

    cp .env.example .env && chmod 600 .env && nano .env

## 5. Run it as a service

    mkdir -p ~/.config/systemd/user
    cp deploy/fenland-platform.service ~/.config/systemd/user/
    systemctl --user daemon-reload
    systemctl --user enable --now fenland-platform
    systemctl --user status fenland-platform

Then tell the web server to pass the hostname to it:

    touch ~/www/platform.fenlandpropertyholdings.co.uk/.proxy-to-socket

Mythic Beasts reload their web server configuration every five minutes, so allow that
long before https://platform.fenlandpropertyholdings.co.uk shows the sign-in screen.

## 6. Link it from the public website

Add an ordinary link wherever you want it (footer, navigation):

    <a href="https://platform.fenlandpropertyholdings.co.uk" rel="nofollow">Sign in</a>

## Day to day

| Task | Command (on the server, in `~/fenland-platform`) |
|---|---|
| Add a user / change a password | `npm run user -- add <username>` |
| Remove a user | `npm run user -- remove <username>` |
| List users | `npm run user -- list` |
| See logs | `journalctl --user -u fenland-platform -n 100` |
| Restart after uploading new code | `systemctl --user restart fenland-platform` |
| Back up | copy `~/fenland-platform/data` somewhere safe |

## How access is protected

- Every page, script, document and API call needs a signed-in session; only the
  sign-in screen and its styling are public. The server refuses to start on a socket
  if no user exists.
- Passwords are hashed with scrypt. Five wrong attempts lock that username and
  address for 15 minutes.
- Sessions last 12 hours, in an HttpOnly, Secure, SameSite cookie. Changing a
  password signs that user out everywhere.
- Requests that change data are refused if they come from another site, and pages are
  marked not to be indexed by search engines or framed.
- All users see and can change everything; there are no roles.
