Project layout
A single-file program is a complete program and stays at the project root. A
project is a directory with a midad.toml, and the folders in it mean
something.
What goes where
| path | holds |
|---|---|
midad.toml |
the manifest: project name and entry file |
main.md |
the entry file: imports, routes, the exec block |
lib/ |
your own modules |
routes/ |
handler functions, mounted by the entry file |
vendor/ |
copies of other people's libraries, made by midad vendor |
tests/ |
programs that check the project rather than serve it |
README.md |
what the project is |
The reason the checker cares is readability, not mechanics. main.md should say
what is served, routes/ should hold the code behind each path, and a stranger
should be able to tell your code from a vendored library at a glance. A project
whose modules are scattered next to the entry file is hard to read even when it
works.
The manifest
[project]
name = "ledger"
entry = "main.md"
midad run . and midad check . read it. Passing a file path still works and
needs no manifest.
The rules
| rule | code | kind |
|---|---|---|
a file that mounts a route lives in routes/ or is the entry |
E0402 |
error |
a file under routes/ mounts a route |
E0403 |
error |
one module: per file, unique across the project |
E0404 |
error |
an imported module outside lib/, routes/, vendor/, tests/ |
W007 |
warning |
| an import that reaches outside the project directory | W002 |
warning |
| a module name that does not match its file name | W004 |
warning |
A module outside the conventional folders still checks and runs; the warning
says where it belongs. A handler outside routes/ is refused instead, because
it is a route nobody can find.
---
midad: "0.1"
module: shared
---
# shared
```fn
function twice(n: number): number {
return n * 2;
}
```
---
midad: "0.1"
module: main
imports:
- ./shared.md
---
# main
```exec
print(str(shared.twice(2)));
```
warning: W007: imported module `shared.md` is outside the conventional layoutRoutes in a project
A handler file in routes/ holds functions, and the entry file mounts each one
by name with a route block of its own:
routes/health.md function check(req: map): map { ... }
main.md imports: ./routes/health.md
route:GET /health health.check
midad check . accepts that shape. E0403 fires when a file sits under
routes/ and neither mounts a route itself nor has a handler that a route
anywhere in the program names; the fix is to mount it in the entry file, or to
move it to lib/ if it is code the handlers call. A route block in an imported
file is E0316, because only the entry file serves.
tests/
tests/ is for programs that check the project instead of serving it: a file
with an exec block that prints what it expects and exits non-zero when it does
not. There is no test runner, no assertion library and no test report. midad
check plus running the file is the whole toolchain.
What midad new writes
$ midad new notes
created notes
next:
cd notes
midad check . # type-check and audit permissions
midad run . # serve http://127.0.0.1:8787
midad run main.md # run the exec block and print
It writes midad.toml, main.md, routes/health.md, lib/greeting.md and a
README.md. The entry file mounts two handlers and imports the module:
notes/
midad.toml
main.md
README.md
lib/greeting.md
routes/health.md
The project runs as written (midad run . serves it and prints
hello world), and midad check . answers ok on it.