# Directory Structure

## Introduction

A Marmeon app is a set of modules. Each feature lives in one folder under `modules/`, with its routes, controllers, pages,
migrations and tests side by side. Around the modules sit a few folders that hold the app together. This is a new app with the
starter kit, as `pnpm create marmeon my-app` writes it:

```text
my-app/
├── bootstrap/        the app's definition, its browser and server entries, generated types
├── config/           the app's own settings
├── lang/             texts the whole app shares
├── layouts/          the app's own components, such as its confirm dialog
├── modules/
│   ├── auth/         sign-in, registration, password reset, API tokens
│   ├── home/         the home page
│   └── system/       the tables of the queue, notifications, live updates
├── storage/          what the app writes while it runs
├── index.html        the document every page renders into
├── .env              this machine's settings, never committed
├── .env.example
├── .env.test
├── AGENTS.md         the guide for coding agents; CLAUDE.md imports it
├── package.json
├── tsconfig.json
├── vite.config.ts
├── vitest.config.ts
└── .oxlintrc.json
```

A blank app from `pnpm create marmeon my-app --minimal` has the same skeleton without `lang/`, `layouts/`,
`modules/auth/` and `modules/home/`. This page walks through each part, then through your first module.

## The root directory

### `bootstrap/`

`bootstrap/` connects the parts. You edit the first file often, the others rarely:

| File | What it does |
| --- | --- |
| `app.ts` | Describes the app: its modules, its configuration and its global middleware. Every command and every test loads it. |
| `client.tsx` | The browser's entry. It reads the page from the document and hydrates it. |
| `ssr.tsx` | The server's entry for rendering pages. |
| `pages.ts` | Finds the page components under `modules/*/views/`, one code-split chunk per page. |
| `head.ts` | The document title, shared by both entries. |
| `i18n.ts` | Loads the language files for the page's locale, in the browser and on the server. |
| `routes.ts` | Generated: every named route and every view, for the type checker. Commit it. |
| `database.d.ts` | Generated: the types of the database tables. Commit it. |

`bootstrap/app.ts` only describes the app. It boots nothing itself. The `marmeon` command boots it for a command or the server,
and each test boots a fresh instance:

```ts title="bootstrap/app.ts"
import { join } from 'node:path';
import { defineApplication } from '@marmeon/core';
import { contentSecurityPolicy, securityHeaders } from '@marmeon/http';
import auth from '#modules/auth';
import home from '#modules/home';
import system from '#modules/system';
import { AppConfig } from '../config/app.ts';

export default defineApplication({
  root: join(import.meta.dirname, '..'),
  config: [AppConfig],
  modules: [auth, home, system],
  middleware: {
    global: [securityHeaders(), contentSecurityPolicy()],
  },
});
```

The [modules](modules.md#the-application) page lists every option of `defineApplication()`.

### `config/`

`config/app.ts` holds the app's own settings: `APP_NAME` and `APP_URL`. Settings that belong to a feature live in its module
instead, and each installed package brings its own. The [configuration](configuration.md#defining-configuration) page explains
config definitions.

### `lang/`

The texts the whole app shares, such as the questions of `layouts/Confirm.tsx`, one file per language: `lang/en.ts`,
`lang/de.ts`. A module keeps its own texts in its `lang/` folder. The [localization](localization.md) page covers both.

### `layouts/`

Components the whole app shares, unlike a module's `layouts/`, which wraps that module's pages. The starter kit has
`Confirm.tsx`, the dialog the app asks its questions with, such as the one before unsaved changes are left;
`bootstrap/client.tsx` passes it to `createMarmeonApp`. The [dialogs](dialogs.md#asking-the-user) page shows it. Starter kit
only.

### `middleware/`

Middleware that belongs to no feature. A new app does not have the
folder yet: `securityHeaders()` from `@marmeon/http` already adds `nosniff`, a referrer policy and `X-Frame-Options` to every
response. The [middleware](middleware.md) page shows how to write your own.

### `modules/`

The features of the app. Each module is a folder with an `index.ts` that exports its definition:

| Module | What it holds |
| --- | --- |
| `auth/` | Sign-in, registration, password reset, the e-mail check, the security page and API tokens. Written by `marmeon make:auth`, and yours to change. The [starter kit](starter-kit.md) page describes it. |
| `home/` | The home page at `/`. Starter kit only. |
| `system/` | The migrations of the tables the framework's drivers need, such as the queue's `jobs` table, the `notifications` table and the `live_hints` table. |

### `storage/`

What the app writes while it runs. Git keeps the folders and ignores their contents. On a server, `storage/` is a volume of its
own:

| Path | What it holds |
| --- | --- |
| `storage/database.sqlite` | The SQLite database. |
| `storage/app/private/`, `storage/app/public/` | The local disks of [file storage](storage.md). |
| `storage/framework/sessions/` | Sessions of the `file` driver. |
| `storage/framework/cache/` | The cache of the `file` driver. |
| `storage/framework/devtools/` | What the [devtools](devtools.md) recorded. Development only. |
| `storage/framework/compile-cache/` | Node's compile cache, which makes `marmeon start` faster. |
| `storage/framework/testing/` | The SQLite databases of [refreshed test apps](database-testing.md), one per test worker, and what a test of a [generated migration](database-testing.md#testing-a-generated-migration) writes: its module and a database of its own. |

### The files in the root

| File | What it does |
| --- | --- |
| `index.html` | The document template. The server renders each page into it. |
| `.env`, `.env.example`, `.env.test` | The settings of this machine, a template for them, and the settings of the tests. See [configuration](configuration.md#the-environment-files). |
| `package.json` | The dependencies and scripts. Its `imports` field maps `#modules/<name>` to `./modules/<name>/index.ts`. |
| `tsconfig.json` | Strict TypeScript that Node runs without a compile step. `erasableSyntaxOnly` refuses what Node cannot strip, such as enums and parameter properties. |
| `vite.config.ts` | Vite with the `marmeon()` plugin and the React plugin. |
| `vitest.config.ts` | The tests: `modules/**/*.test.ts`. |
| `.oxlintrc.json` | The lint preset. It keeps views from importing server code by value. |
| `AGENTS.md`, `CLAUDE.md` | The guide a coding agent reads at the start of a session. `marmeon agents:sync` keeps a block of `AGENTS.md` current; the rest is yours. `CLAUDE.md` imports `AGENTS.md`. See [coding agents](agents.md). |
| `marmeon-stubs.json` | Where the files that `create-marmeon`, `marmeon make:auth` and `marmeon publish` copied came from: the package, its version and a hash per file. `publish` records the migrations of a package in it, so a later release that changes one is noticed. Written by the framework, committed with the app. See [the auth module](starter-kit.md#what-the-auth-module-holds). |

### Generated folders

Three folders appear once you run the app or build it. Git ignores them:

| Folder | Written by | What it holds |
| --- | --- | --- |
| `node_modules/` | the package manager | The dependencies. |
| `.marmeon/` | `marmeon` itself | The list of installed packages and their providers, the routes the browser may learn, the schema the migrations describe, and after a build the precomputed injection lists. |
| `dist/` | `marmeon build` | The browser's files in `dist/client/` and the server rendering in `dist/server/`. |

`marmeon` does not use `.marmeon/` when it is a link that leads out of the app (a cloned repository can bring one): it neither writes
nor reads there, and says so with `.marmeon leads out of the app … Nothing was written.` or `… was not read.` `marmeon discover`, `marmeon build`,
`marmeon schema:project`, `marmeon route:types`, the Vite plugin and a start stop, even when the file behind the link is current. Where the
schema the migrations describe is asked for directly (the migrator, for example), it is a cache: it is computed in memory, with a warning. The [last error](errors.md#the-development-error-page)
is not written while `.marmeon` is a link of any kind. Remove the link, or point it at a directory inside the app.

## Inside a module

A module grows the folders it needs. The auth module of the starter kit uses most of them:

```text
modules/auth/
├── index.ts                the definition, and what other modules may import
├── routes.ts               the routes of the web group
├── api-routes.ts           the routes of the api group, under /api
├── AuthServiceProvider.ts  bindings and start-up code
├── controllers/            the classes behind the routes
├── views/                  the pages
├── layouts/                the layouts around the pages
├── lang/                   the module's texts
├── middleware/
├── mail/                   mails
├── jobs/                   queued jobs
├── listeners/              event listeners
├── commands/               console commands
├── migrations/
├── factories/
├── seeders/
├── schedule.ts             when the scheduled work runs
└── auth.test.ts            tests, next to the code
```

Only `index.ts` is required, and two folder names matter: the pages are found under `views/`, and the module's texts under
`lang/`. The `system` module's `views/errors/` and `views/layers/` hold the framework's pages your app replaces (error pages, the layer backdrop). Routes, providers, jobs,
listeners, commands, migrations and the schedule count once the module's definition lists them. Controllers and other classes
need no list: the container builds them when a route or a constructor asks for them.

`index.ts` is also the module's public face. Other modules import from `#modules/notes`, never from a file inside it, so
whatever `index.ts` exports is what the rest of the app may use. In this example the module also has a repository, which the
walk-through below does not create:

```ts title="modules/notes/index.ts"
import { defineModule } from '@marmeon/core';
import routes from './routes.ts';

// What other modules may use:
export { NoteRepository } from './NoteRepository.ts';

export default defineModule({
  name: 'notes',
  routes,
});
```

The [modules](modules.md) page shows every key a module definition can have, and how to enforce the boundaries with the lint
preset.

### Components several modules share

A module's `index.ts` is loaded as server code, so it cannot export a `.tsx` component: Node refuses the file ("Unknown file extension"). Views read a module's entry with `import type` only (the lint preset says so). Components that several modules use get an import of their own, next to `#modules/*`:

```json title="package.json"
{
  "imports": {
    "#modules/*": "./modules/*/index.ts",
    "#ui": "./modules/ui/components/index.ts"
  }
}
```

Views import from `#ui` directly. The target is a file you write: `modules/ui/components/index.ts` re-exports the shared components (`export { Button } from './Button.tsx';`), and the `ui` module's own `index.ts` stays free of components.

### Folders by feature

Apart from `index.ts`, `views/` and `lang/`, the framework finds nothing by the folder names above. A module can just as well
group its files by feature:

```text
modules/billing/
├── index.ts
├── routes.ts                 loads the route files of the features
├── invoices/
│   ├── routes.ts
│   ├── ShowInvoiceController.ts
│   ├── InvoiceRepository.ts
│   └── SendInvoiceJob.ts
├── subscriptions/
├── views/
│   ├── invoices/Show.tsx
│   └── subscriptions/Index.tsx
├── lang/
└── migrations/
```

The folder names still say one thing: `controllers/`, `jobs/`, `listeners/` and the other folders of their kind hold
[server code](vite.md#server-code-in-the-browser), and the build refuses a page that imports a file from them as a value.
A feature folder is not one of them. Name the feature folders as server code, so a page that imports
`ShowInvoiceController.ts` as a value stops the build instead of shipping the controller to the browser:

```json title="package.json"
{
  "marmeon": {
    "serverModules": ["modules/billing/invoices/**", "modules/billing/subscriptions/**"]
  }
}
```

The build, the tests and the lint preset read this one list (the preset once `marmeon make:lint-config --force` has written it anew). See [Server folders of your own](vite.md#server-folders-of-your-own).

A few things stay where they are, because the framework finds them by their path:

- Pages are the `.tsx` files under the module's own `views/`, at any depth. A page in `invoices/` is not found. Group the pages
  by feature inside `views/` instead: the page name follows the path, so `modules/billing/views/invoices/Show.tsx` is
  `billing/invoices/Show`.
- Texts are the files directly in `lang/`.
- Migrations are the files directly in the one folder the definition names. Folders inside it may hold helpers a migration
  imports.
- A module lists one route file per group, and that file loads the features' route files:

```ts title="modules/billing/routes.ts"
import { defineRoutes } from '@marmeon/http';
import invoices from './invoices/routes.ts';
import subscriptions from './subscriptions/routes.ts';

export default defineRoutes((Route) => {
  invoices.register(Route);
  subscriptions.register(Route);
});
```

Jobs, listeners and commands are listed in `index.ts` wherever they live. The boundary rule, which
`marmeon make:lint-config --boundaries` adds to the lint preset, covers files up to seven folders below a module, more if a
module is deeper when the preset is written.

A feature that brings its own tables, routes and pages often makes a module of its own: then it has its own migrations and its
own `views/`, and other modules reach it only through its `index.ts` and the tables it [exposes](queries.md#module-boundaries).

### Where tests live

The app's `vitest.config.ts` decides which files are tests. A new app collects `modules/**/*.test.ts`, so a test next to the
code and one in a module's `tests/` folder both run. For a `tests/` folder in the app's root, add it to the list:

```ts title="vitest.config.ts"
import { marmeon } from '@marmeon/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  plugins: [marmeon({ test: true })],
  test: { include: ['modules/**/*.test.ts', 'tests/**/*.test.ts'] },
});
```

With the boundary rule, the lint preset lets a test import from inside any module, wherever the test lives.

## Your first module

A new feature starts as a module. This walk-through adds a page at `/notes` for signed-in users.

### Create the module

`make:module` writes `index.ts` and an empty `routes.ts` (`--no-routes` leaves `routes.ts` out, for a module without pages or endpoints), and `make:controller` writes a controller and its page:

```sh
pnpm marmeon make:module notes
pnpm marmeon make:controller ListNotes --module=notes --view
```

With npm, run the commands as `npx marmeon …`. `pnpm marmeon list` shows every command the app has.

### Register it

The app loads a module only when `bootstrap/app.ts` lists it. `make:module` adds the module last in `modules` (routes match in module order), which gives:

```ts title="bootstrap/app.ts"
import { join } from 'node:path';
import { defineApplication } from '@marmeon/core';
import { contentSecurityPolicy, securityHeaders } from '@marmeon/http';
import auth from '#modules/auth';
import home from '#modules/home';
import system from '#modules/system';
import { AppConfig } from '../config/app.ts';
import notes from '#modules/notes';

export default defineApplication({
  root: join(import.meta.dirname, '..'),
  config: [AppConfig],
  modules: [auth, home, system, notes],
  middleware: {
    global: [securityHeaders(), contentSecurityPolicy()],
  },
});
```

### Add the route, the controller and the page

The route points to the controller and gives it a name. `authenticate()` sends guests to the sign-in page, and `verified()`
sends an account whose address is not confirmed yet to the auth module's notice:

```ts title="modules/notes/routes.ts"
import { authenticate, verified } from '@marmeon/auth';
import { defineRoutes, everyone } from '@marmeon/http';
import { ListNotesController } from './controllers/ListNotesController.ts';

export default defineRoutes((Route) => {
  Route.middleware(authenticate()).middleware(verified()).authorize(everyone).get('/notes', ListNotesController).name('notes.index');
});
```

`authenticate()` alone would let in an account registered with somebody else's address. The auth module's own pages check the
address themselves, and a route of yours needs `verified()` for that. The [starter kit](starter-kit.md#your-own-pages) page
explains why.

The controller answers with the page `notes/ListNotes`, which is `modules/notes/views/ListNotes.tsx`, and hands it its props:

```ts title="modules/notes/controllers/ListNotesController.ts"
import type { Authenticated } from '@marmeon/auth';
import { Controller, type HttpContext } from '@marmeon/http';

export class ListNotesController extends Controller {
  handle(ctx: HttpContext<Authenticated>) {
    return this.view('notes/ListNotes', { author: ctx.user.name });
  }
}
```

The page takes its props from the controller's type, so a prop the controller does not send is a compile error:

```tsx title="modules/notes/views/ListNotes.tsx"
import type { PageProps } from '@marmeon/http';
import { Head, Link } from '@marmeon/react';
import type { ListNotesController } from '../controllers/ListNotesController.ts';

export default function ListNotes({ author }: PageProps<ListNotesController>) {
  return (
    <main>
      <Head title="Notes" />
      <h1>Notes of {author}</h1>
      <Link route="dashboard">Back to the dashboard</Link>
    </main>
  );
}
```

The view imports the controller with `import type`. A value import would bundle server code into the browser, and the lint
preset and the build both refuse it.

### See it

`pnpm dev` keeps `bootstrap/routes.ts` up to date, so `notes.index` is a known route name at once. Open
http://localhost:3000/notes and sign in with a confirmed account, and the page is there. The
[installation](installation.md#your-first-account) page shows how to register the first account. `pnpm marmeon route:list` shows
the route with every middleware it passes.

### A table for the notes

`make:migration` writes a migration into the module:

```sh
pnpm marmeon make:migration create_notes_table --module=notes
```

The first migration of a module also needs its folder in the module's definition: the command adds
`migrations: new URL('./migrations/', import.meta.url)` and prints the line only when the file has another shape or you pass
`--no-register`. `pnpm marmeon migrate` then creates the table and, in development,
writes its types into `bootstrap/database.d.ts`. The [migrations](migrations.md) and [repositories](queries.md) pages go on
from there. [Your First App](first-app.md) takes this module on to a complete notes app with a policy, forms and tests.
