Skip to content

Commit 7fd1f9a

Browse files
committed
doc: document backend key locations
1 parent 34fdf3a commit 7fd1f9a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

doc/contributors/structure.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Every directory under `/tools` is [an npm "workspaces" module](https://docs.npmj
2929

3030
Some of these modules are core pieces of Puter:
3131
- **Puter's backend** is [`/src/backend`](/src/backend)
32+
- See [key locations in backend documentation](/src/backend/doc/contributors/structure.md)
3233
- **Puter's GUI** is [`/src/gui`](/src/gui)
3334

3435
Some of these modules are apps:
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Puter Backend - Directory Structure
2+
3+
## MFU - Most Frequently Used
4+
5+
These locations under `/src/backend/src` are the most important
6+
to know about. Whether you're contributing a feature or fixing a bug,
7+
you might only need to look at code in these locations.
8+
9+
### `modules` directory
10+
11+
The `modules` directory contains Puter backend kernel modules only.
12+
Everything in here has a `<name of>Module.js` file and one or more
13+
`<name of>Service.js` files.
14+
15+
> **Note:** A "backend kernel module" is simply a class understood by
16+
[`src/backend/src/Kernel.js`](../../src/Kernel.js)
17+
that registers a number of "Service" classes.
18+
You can look at [Puter's init file](../../../../tools/run-selfhosted.js)
19+
to see how modules are added to Puter.
20+
21+
The `README.md` file inside any module directory is generated with
22+
the `module-docgen` script in the Puter repo's `/tools` directory.
23+
The actual documentation for the module exists in jsdoc comments
24+
in the source files.
25+
26+
Each module might contain these directories:
27+
- `doc/` - additional module documentation, like sample requests
28+
- `lib/` - utility code that isn't a Module or Service class.
29+
This utility code may be exposed by a service in the module
30+
to Puter's runtime import mechanism for extension support.
31+
32+
### `services` directory
33+
34+
This directory existed before the `modules` directory. Most of
35+
the services here go on a module called **CoreModule**
36+
(CoreModule.js is directly in `/src/backend/src`), but this
37+
directory can be thought of as "services that are not yet
38+
organized in a distinct module".
39+
40+
### `routers` directory
41+
42+
While routes are typically registered by Services, the implementation
43+
of a route might be placed under `src/backend/src/routers` to keep the
44+
service's code tidy or for legacy reasons.
45+
46+
These are some services that reference files under `src/backend/src/routers`:
47+
- [PermissionAPIService](../../src/services/PermissionAPIService.js) -
48+
This service registers routes that allow a user to configure permissions they
49+
grant to apps and groups. This is a relatively recent case of using files under the
50+
`routers` directory to clean up the service.
51+
- [UserProtectedEndpointsService](../../src/services/web/UserProtectedEndpointsService.js) -
52+
This service follows a slightly different approach where files under
53+
`routers/user-protected` contain an "endpoint specification" instead of an express
54+
handler function. This might be good inspiration for future routes.
55+
- [PuterAPIService](../../src/services/PuterAPIService.js) -
56+
This service is a catch-all for routes that existed before separation of concerns
57+
into backend kernel modules.
58+
59+
### `filesystem` directory
60+
61+
The filesystem is likely the most complex portion of Puter's source code. This code
62+
is in its own directory as a matter of circumstance more than intention. Ideally the
63+
filesystem's concerns will be split across a few modules as we prepare to add
64+
support for mounting different file systems and improved cache behavior.
65+
For example, Puter's native filesystem implementation should be mostly moved to
66+
`src/backend/src/modules/puterfs` as we continue this development.
67+
68+
Since this directory is in flux, don't trust this documentation completely.
69+
If you're contributing to filesystem,
70+
[tag @KernelDeimos on the community Discord](https://discord.gg/PQcx7Teh8u)
71+
if you have questions.
72+
73+
These are the key locations in the `filesystem` directory:
74+
- `FSNodeContext.js` - When you have a reference to a file or directory in backend code,
75+
it is an instance of the FSNodeContext class.
76+
- `ll_operations` - Runnables that implement the behavior of a filesystem operation.
77+
These used to include the behavior of Puter's filesystem, but they now delegate
78+
the actual behavior to the implementation in the `.provider` member of a
79+
FSNodeContext (filesystem node / a file or directory) so that we can eventually
80+
support "mountpoints" (multiple filesystem implementations).
81+
- `hl_operations` - Runnables that implement the behavior of higher-level versions
82+
of filesystem operations. For example, the high-level mkdir operation might create
83+
multiple directories in chain; the high-level write might change the name of the
84+
file to avoid conflicts if you specify the `dedupe_name` flag.

0 commit comments

Comments
 (0)