You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cookbook/src/learning/curriculum.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,9 @@ This curriculum is designed to take you from a RustAPI beginner to an advanced u
13
13
-**Expected Output:** A running server that responds to `GET /` with "Hello World".
14
14
-**Pitfalls:** Not enabling `tokio` features if setting up manually.
15
15
16
+
#### 🛠️ Mini Project: "The Echo Server"
17
+
Create a new endpoint `POST /echo` that accepts any text body and returns it back to the client. This verifies your setup handles basic I/O correctly.
18
+
16
19
#### 🧠 Knowledge Check
17
20
1. What command scaffolds a new RustAPI project?
18
21
2. Which feature flag is required for the async runtime?
@@ -25,6 +28,9 @@ This curriculum is designed to take you from a RustAPI beginner to an advanced u
25
28
-**Expected Output:** Endpoints that return static JSON data.
26
29
-**Pitfalls:** Forgetting to register routes in `main.rs` if not using auto-discovery.
27
30
31
+
#### 🛠️ Mini Project: "The Calculator"
32
+
Create an endpoint `GET /add?a=5&b=10` that returns `{"result": 15}`. This practices query parameter extraction and JSON responses.
33
+
28
34
#### 🧠 Knowledge Check
29
35
1. Which macro is used to define a GET handler?
30
36
2. How do you return a JSON response from a handler?
@@ -37,6 +43,9 @@ This curriculum is designed to take you from a RustAPI beginner to an advanced u
37
43
-**Expected Output:**`GET /users/{id}` returns the ID. `POST /users` echoes the JSON body.
38
44
-**Pitfalls:** Consuming the body twice (e.g., using `Json` and `Body` in the same handler).
39
45
46
+
#### 🛠️ Mini Project: "The User Registry"
47
+
Create a `POST /register` endpoint that accepts a JSON body `{"username": "...", "age": ...}` and returns a welcome message using the username. Use the `Json` extractor.
48
+
40
49
#### 🧠 Knowledge Check
41
50
1. Which extractor is used for URL parameters like `/users/:id`?
42
51
2. Which extractor parses the request body as JSON?
Copy file name to clipboardExpand all lines: docs/cookbook/src/recipes/file_uploads.md
+28-21Lines changed: 28 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# File Uploads
2
2
3
-
Handling file uploads efficiently is crucial for modern applications. RustAPI provides a `Multipart` extractor that allows you to stream uploads, enabling you to handle large files (e.g., 1GB+) without consuming proportional RAM.
3
+
Handling file uploads is a common requirement. RustAPI provides a `Multipart` extractor to parse `multipart/form-data` requests.
4
4
5
5
## Dependencies
6
6
@@ -13,15 +13,13 @@ tokio = { version = "1", features = ["fs", "io-util"] }
13
13
uuid = { version = "1", features = ["v4"] }
14
14
```
15
15
16
-
## Streaming Upload Example
16
+
## Buffered Upload Example
17
17
18
-
Here is a complete, runnable example of a file upload server that streams files to a `./uploads` directory.
18
+
RustAPI's `Multipart` extractor currently buffers the entire request body into memory before parsing. This means it is suitable for small to medium file uploads (e.g., images, documents) but care must be taken with very large files to avoid running out of RAM.
By default, some frameworks load the entire file into RAM. RustAPI's `Multipart` allows you to process the stream incrementally using `field.chunk()`.
99
-
-**Buffering**: `field.bytes().await` (Load all into RAM - simple but dangerous for large files)
100
-
-**Streaming**: `field.chunk().await` (Load small chunks - scalable)
103
+
### 1. Buffering
104
+
RustAPI loads the entire `multipart/form-data` body into memory.
105
+
-**Pros**: Simple API, easy to work with.
106
+
-**Cons**: High memory usage for concurrent large uploads.
107
+
-**Mitigation**: Set a reasonable `DefaultBodyLimit` (e.g., 10MB - 100MB) to prevent DoS attacks.
101
108
102
109
### 2. Body Limits
103
-
The default request body limit is often small (e.g., 1MB) to prevent DoS attacks. You must explicitly increase this limit for file upload routes using `RustApi::new().body_limit(size)`. This applies globally to the application instance. If you need different limits for different routes, consider creating separate router instances or using a custom layer.
110
+
The default request body limit is small (2MB) to prevent attacks. You **must** explicitly increase this limit for file upload routes using `.layer(DefaultBodyLimit::max(size_in_bytes))`.
104
111
105
112
### 3. Security
106
113
-**Path Traversal**: Malicious users can send filenames like `../../system32/cmd.exe`. Always rename files or sanitize filenames strictly.
0 commit comments