# Before You Go to Production

- [Introduction](#introduction)
- [The checklist](#the-checklist)
- [Cap the page size](#cap-the-page-size)
- [Let tokens expire](#let-tokens-expire)
- [Share the rate limits between servers](#share-the-rate-limits-between-servers)
- [Trust only your own proxies](#trust-only-your-own-proxies)
- [Know the registration limits](#know-the-registration-limits)
- [Choose who may call the API from a browser](#choose-who-may-call-the-api-from-a-browser)
- [Turn debug mode off](#turn-debug-mode-off)
- [Trust every host your clients use](#trust-every-host-your-clients-use)
- [Limit how much `?related=` loads](#limit-how-much-related-loads)
- [Know what your database decides](#know-what-your-database-decides)

<a name="introduction"></a>
## Introduction

Restify's defaults copy Restify for PHP, so a PHP application's clients keep working after the move. Some of those defaults are open: they suit a laptop, not a public server. Go through this page once before the first deploy, and again before a PHP application switches to Rust.

<a name="the-checklist"></a>
## The checklist

| Setting | Default | Set it to |
| --- | --- | --- |
| `RESTIFY_MAX_PER_PAGE` | No limit, as in PHP | The largest page a client needs, such as `100` |
| `RESTIFY_TOKEN_TTL` | Tokens never expire, as in PHP | A number of minutes, such as `43200` for 30 days |
| `CACHE_STORE` | `memory`, one store per process | `database` or `redis` |
| Trusted proxies | None | Your load balancer's addresses |
| CORS | Any origin under `api/*`, no cookies | Your frontend's origins |
| `APP_DEBUG` | `true` in `.env.example` | `false` |
| Trusted hosts | The host of `APP_URL` | Every host your clients use |
| `RESTIFY_RELATED_MAX_DEPTH` and `RESTIFY_RELATED_MAX_PATHS` | `3` and `20` | Keep them, or lower them |

<a name="cap-the-page-size"></a>
## Cap the page size

A client chooses the page size with `perPage` or `page[size]`. Without a cap, `perPage=100000` loads 100,000 rows in one request. Restify for PHP has no cap by default either, and PHP clients may rely on that, so Restify for Rust keeps it.

Set the cap in `.env`:

```ini
RESTIFY_MAX_PER_PAGE=100
```

A larger `perPage` then gets 100 rows. The cap also limits how many related rows a list loads for each relationship. See [Pagination](filtering.md#pagination).

<a name="let-tokens-expire"></a>
## Let tokens expire

A token from `POST /api/login` works until the user logs out, as in PHP. A stolen token works forever. Make tokens expire after a number of minutes:

```ini
RESTIFY_TOKEN_TTL=43200
```

See [Sanctum tokens](sanctum.md).

<a name="share-the-rate-limits-between-servers"></a>
## Share the rate limits between servers

Login and registration are throttled as in PHP: 6 logins a minute for one email from one IP address, 30 logins a minute from one IP address, and 6 registrations a minute from one IP address. PHP doesn't let you change these numbers, so Restify doesn't either.

The counters live in the framework's default cache store. That store is `memory` unless `CACHE_STORE` says otherwise. The `memory` store belongs to one process and is emptied on each restart. With 4 servers, a client gets 4 times the limit, and a deploy resets it. Use a shared store:

```ini
CACHE_STORE=redis
```

See the framework's [rate limiting guide](https://github.com/laravel/rust-framework/blob/0.x/docs/rate-limiting.md#cache-configuration).

<a name="trust-only-your-own-proxies"></a>
## Trust only your own proxies

The rate limits count by IP address. Behind a load balancer, every request seems to come from the load balancer's address, so all clients share one limit. Tell the framework which proxies to trust, in `src/app.rs`:

```rust
.with_middleware(|middleware, _| {
    middleware.trust_proxies(["10.0.0.0/8"]);
})
```

Don't trust `"*"` unless your load balancer replaces any `X-Forwarded-For` header the client sent. Otherwise a client can pick its own IP address and skip the limits. See the framework's guide on [trusting proxies](https://github.com/laravel/rust-framework/blob/0.x/docs/middleware.md#trusting-proxies).

<a name="know-the-registration-limits"></a>
## Know the registration limits

`POST /api/register` checks what PHP's `RestifyRegisterRequest` checks:

| Key | Rules |
| --- | --- |
| `email` | Required, an email address, at most 255 characters, not already used |
| `password` | Required, confirmed, at least 6 characters |
| `name` | No rule, as in PHP |

Restify keeps these rules so PHP clients keep working. A request body has the framework's size limit, so a `name` can't be endless. If your users table needs more, such as a length limit on `name`, serve your own register route instead of `Restify::auth`. See [Authentication](authentication.md).

<a name="choose-who-may-call-the-api-from-a-browser"></a>
## Choose who may call the API from a browser

By default, the framework lets JavaScript on any website call routes under `api/*`, without cookies. Restify's routes are under `api/`, so any website can call them with a token it holds. If only your own frontend should, list its origins in `src/config/cors.rs`. See the framework's guide on [CORS](https://github.com/laravel/rust-framework/blob/0.x/docs/routing.md#cors).

The MCP server refuses every other site's page, unless `src/config/cors.rs` lists the origin for its path: see [Calling the MCP server from a browser](mcp.md#calling-the-mcp-server-from-a-browser).

<a name="turn-debug-mode-off"></a>
## Turn debug mode off

With `APP_DEBUG=true`, a server error shows the error, its causes, the route and the request. Set it to `false` in production:

```ini
APP_DEBUG=false
```

See the framework's [configuration guide](https://github.com/laravel/rust-framework/blob/0.x/docs/configuration.md).

<a name="trust-every-host-your-clients-use"></a>
## Trust every host your clients use

In production, the framework answers `400` to a request whose `Host` header isn't the host of `APP_URL` or one of its subdomains. List every other host your clients use. See [Trust every host your clients use](migrating-from-php.md#trust-every-host-your-clients-use).

<a name="limit-how-much-related-loads"></a>
## Limit how much `?related=` loads

Restify refuses a `?related=` or `include` chain deeper than `RESTIFY_RELATED_MAX_DEPTH` (3) and a request that names more than `RESTIFY_RELATED_MAX_PATHS` (20) relationships, with a `422`. Restify for PHP has no such limit. `0` turns a limit off. See [How deep, and how many](relationships.md#how-deep-and-how-many).

<a name="know-what-your-database-decides"></a>
## Know what your database decides

Restify runs on SQLite, PostgreSQL, MySQL and MariaDB. A few answers depend on the database, in Restify for PHP and in Restify for Rust alike, so they don't change when you move from PHP to Rust on the same database. Check them when you change databases:

| What | SQLite | PostgreSQL | MySQL and MariaDB |
| --- | --- | --- | --- |
| `?search=` and upper and lower case | Ignores the case of A to Z | Ignores case | Follows the column's collation: the usual ones ignore case and accents, a `_bin` one doesn't |
| Where `NULL` goes when sorting ascending | First | Last | First |
| How text sorts | By the letters' codes: capitals first | By the database's locale | By the collation: the usual ones ignore case |
| `?group_by=` a column that isn't the key | Works | `500` | `500` on MySQL (`ONLY_FULL_GROUP_BY`); works on MariaDB |

To make the search ignore case on every database, set `RESTIFY_SEARCH_CASE_SENSITIVE=false`. See [Search](filtering.md#search).

On MariaDB, start `DATABASE_URL` with `mariadb://`, not `mysql://`, so the framework writes MariaDB's SQL.

On SQLite, put `journal_mode=wal` in the production `DATABASE_URL` too, if you turned on the write-ahead log: it is a setting of each environment's `.env`, not of the code. After the first write, `sqlite3 <file> "PRAGMA journal_mode"` answers `wal`. See [Many token requests on SQLite](sanctum.md#many-token-requests-on-sqlite).

