# Large applications Cylinder is a small framework. That says nothing about the size of what you can build with it. The model will be familiar from PHP, where a URL has long mapped to a file, and where very large applications have been built. Cylinder keeps that model and changes what tended to go wrong with it: a handler is a plain function, and everything it depends on is passed in by name instead of being reached for as a global. This page explains why that model holds up as a codebase grows, and shows how to lay out a large project. ## Why it scales A web application becomes hard to work on when understanding one page means understanding the whole system. Cylinder's design keeps each page a separate, small problem, and that does not change with the number of pages. **The code for any URL is at a path you can work out.** The handler for `/account/orders` is the file `account/orders.page.get.py`. Nobody has to trace decorators, blueprints, or URL configuration through several files to find it. In a project with ten pages that saves a moment. In one with two thousand it is the difference between opening a file and conducting a search. **A change touches one small file.** A handler is one file with one `main()`, and its signature lists everything it uses. There is no application context to keep in mind and no middleware order to remember. Reviewing a change to a page means reading that page. **Nothing central grows.** Adding a page is adding a file. There is no route table, registration module, or application factory that every feature has to edit, so there is no file that everyone's changes collide in. A large site is a sum of small independent parts. **Handlers cannot become entangled.** A handler file's name contains dots, so it cannot be imported by another handler. Shared code has to live in an ordinary module, which is where it belongs, and handlers never form import cycles with each other. **The feedback loop stays short.** A handler is loaded when it is requested. Edit one, request it again, and you see the result, with a traceback that points at one file if it fails. That loop takes the same few seconds in a large project as in a small one. **The cost of a request does not grow with the site.** To match a request, Cylinder looks a handful of names up in the listing of each directory on the way to the URL, and stops at the first directory that does not exist. It keeps each listing in memory until the directory changes, so the work depends on how deep the tree is along that URL, not on how many pages exist or on how long a URL the client sends. A site with ten thousand handlers does the same work to route a request as a site with ten. ## A layout for a large project In a large application, most of the code is not in handlers. It is in ordinary Python packages that the handlers call: ```text my_project |-- cylinder_main.py creates the app and its shared objects |-- /shop a normal package: the application's own logic |-- __init__.py |-- checks.py authentication and permission checks |-- orders.py queries and business rules |-- models.py |-- /templates |-- /tests |-- /my_webapps |-- store.404.py |-- store.500.py |-- store.page.get.py |-- store.hook.late.py headers for the whole site |-- /store |-- account.hook.early.py everything under /account requires a login |-- /account |-- orders.page.get.py |-- /orders |-- {order_id}.page.get.py |-- settings.page.get.py |-- settings.page.post.py |-- admin.hook.early.py |-- /admin |-- ... |-- API.404.py |-- API.hook.late.py |-- /API |-- ... ``` The `shop` package sits beside `cylinder_main.py`, so handlers can import it by name. ### Keep handlers thin A handler's job is to read the request, call the application's logic, and build the response: ```python # store/account/orders/{order_id}.page.get.py from shop import orders def main(order_id, response, abort, db, render_template, g): order = orders.find(db, g.user, order_id) if order is None: abort(404) response.content_type = "text/html; charset=utf-8" return render_template("order.html", order=order) ``` The name in braces is a [placeholder](routing.md#placeholders): the file answers for any one order, and `order_id` is the segment of the URL it stood for. The rules about what an order is and who may see it live in `shop/orders.py`, where they can be unit tested without a request. The handler stays short enough to read at a glance, however much the rules grow. ### Put shared checks in a module, and call them from hooks [Every hook between the site root and a URL](hooks.md#every-hook-on-the-path-runs) runs for a request, so a section's hook adds to the ones above it. In a large site, several sections will have their own hooks. Keep the logic in a module and let each hook be a few lines that call it: ```python # shop/checks.py def require_login(request, abort, g): g.user = user_for(request) if g.user is None: abort(302, "/login") ``` ```python # store/account.hook.early.py from shop import checks def main(request, response, abort, g): checks.require_login(request, abort, g) return response ``` A hook file then reads as a statement of policy for its directory, and the directory listing shows which sections have one. ### Treat `app_map()` as the application's list of dependencies Everything a handler can ask for, beyond what Cylinder provides, comes from the dictionary that `app_map()` returns. Keep that dictionary small and stable: a database session factory, a template renderer, configuration, a per-request namespace. It is the one place that defines what the whole application can reach, which makes it easy to review and easy to replace in tests. ### Give each section its own error handling Error handlers are scoped by path, so the public site, the admin area, and the API can each present errors their own way without knowing about each other. See [Different error pages for different sections](errors.md#different-error-pages-for-different-sections). ### Split into several webapps when the parts are separate One process can serve several webapps, chosen per request in `app_map()`. A public site and an internal tool that share a database can live in separate directories and still share the `shop` package. See [Several sites from one process](routing.md#several-sites-from-one-process). ## Working in a large codebase A few habits make a big tree easy to move around in. - **To find the code for a URL, build the path.** In the `store` webapp, `/account/orders/1234` is handled by `store/account/orders/1234.page.get.py`. Nobody names a file for one order, so look in that directory for the name in braces instead: `{order_id}.page.get.py`. If neither is there, the nearest `.tree.` file up the tree answers, and if there is none the URL is a `404`. - **To see what else runs for a URL, list the directories above it.** Files with `.hook.` in their names are hooks, and files named for a status code are error handlers. Every hook that is named for a part of the URL applies, and the closest error handler. - **To see what a handler depends on, read its signature.** - **To find every page that uses something, search for the parameter name.** Handlers ask for dependencies by name, so `db` or `render_template` is a reliable search term. ## Development at scale Handler files are picked up without a restart. Ordinary modules, such as the `shop` package above, are loaded once and need a restart to change, and in a large application that is where most edits happen. For development, run the app with Werkzeug's reloading server, which restarts the process when a module changes. [Restarting automatically during development](operations.md#restarting-automatically-during-development) shows how to set it up. ## Testing at scale - **Unit test the package.** With handlers kept thin, most logic is in ordinary functions that need no request. - **Test each section through the [test client](testing.md).** It exercises the real routing, hooks, and error handlers, so a test can assert that every page under `/account` redirects without a login. - **Test the hooks you rely on.** A hook applies by its name and its place, so a test that a section still rejects an anonymous request guards against a hook that was renamed or moved off the path. ## Limits to know about What Cylinder does not do is on one page, whatever the size of the project: [Limits](limits.md).