|
| 1 | +# Middleware |
| 2 | + |
| 3 | +Middleware in AIScript provides a powerful way to process HTTP requests and responses. Middleware functions are executed in a sequence, allowing you to modify requests, validate authentication, log activities, and more before a route handler is invoked. |
| 4 | + |
| 5 | +## CORS |
| 6 | + |
| 7 | +Controls Cross-Origin Resource Sharing settings. |
1 | 8 |
|
2 | 9 | ```toml |
| 10 | +# project.toml |
| 11 | + |
3 | 12 | [middleware.cors] |
4 | | -allowed_origins = ["http://localhost:3000"] |
| 13 | +allowed_origins = ["http://localhost:3000", "https://example.com"] |
5 | 14 | allowed_methods = ["GET", "POST", "PUT", "DELETE"] |
6 | 15 | allowed_headers = ["Content-Type", "Authorization"] |
7 | 16 | allow_credentials = true |
| 17 | +max_age = 86400 |
| 18 | +``` |
8 | 19 |
|
9 | | -[middleware.body_limit] |
10 | | -limit = "100KB" |
| 20 | +### Options |
| 21 | + |
| 22 | +| Option | Type | Description | Default | |
| 23 | +| ------------------- | ------- | --------------------------------------------------------- | --------------------------------------------- | |
| 24 | +| `allowed_origins` | Array | List of origins that are allowed to access the resource | `["*"]` | |
| 25 | +| `allowed_methods` | Array | HTTP methods allowed | `["GET", "POST", "PUT", "DELETE", "OPTIONS"]` | |
| 26 | +| `allowed_headers` | Array | HTTP headers allowed | `["Content-Type", "Authorization"]` | |
| 27 | +| `allow_credentials` | Boolean | Indicates if cookies can be included in requests | `false` | |
| 28 | +| `max_age` | Number | How long the results of a preflight request can be cached | `86400` (24 hours) | |
| 29 | + |
| 30 | +## Rate Limit |
| 31 | + |
| 32 | +Limits the number of requests a client can make within a specified time period. |
| 33 | + |
| 34 | +```toml |
| 35 | +# project.toml |
11 | 36 |
|
12 | 37 | [middleware.rate_limit] |
13 | | -enabled = true |
14 | | -max_requests = 100 |
15 | | -window = 60 |
| 38 | +limit = 100 |
| 39 | +window = 60 # in seconds |
| 40 | +message = "Too many requests, please try again later." |
| 41 | +``` |
| 42 | + |
| 43 | +### Options |
| 44 | + |
| 45 | +| Option | Type | Description | Default | |
| 46 | +| --------------- | ------ | ----------------------------------------------------------------------- | --------------------- | |
| 47 | +| `limit` | Number | Maximum number of requests allowed | `100` | |
| 48 | +| `window` | Number | Time window in seconds | `60` | |
| 49 | +| `message` | String | Message to return when rate limit is exceeded | `"Too many requests"` | |
| 50 | +| `key_extractor` | String | Function to extract the rate limit key (e.g., "ip", "header:X-API-Key") | `"ip"` | |
| 51 | + |
| 52 | +## Body Limit |
| 53 | + |
| 54 | +Limits the size of request bodies. |
| 55 | + |
| 56 | +```toml |
| 57 | +# project.toml |
| 58 | + |
| 59 | +[middleware.body_limit] |
| 60 | +limit = "1mb" |
| 61 | +``` |
| 62 | + |
| 63 | +### Options |
| 64 | + |
| 65 | +| Option | Type | Description | Default | |
| 66 | +| ------- | ------ | --------------------------------------------------- | ------- | |
| 67 | +| `limit` | String | Maximum size of request body (e.g., "1mb", "500kb") | `"1mb"` | |
| 68 | + |
| 69 | +## Timeout |
| 70 | + |
| 71 | +Sets a timeout for handling requests. |
| 72 | + |
| 73 | +```toml |
| 74 | +# project.toml |
16 | 75 |
|
17 | 76 | [middleware.timeout] |
18 | | -timeout = 5 |
| 77 | +duration = 5000 # in milliseconds |
| 78 | +message = "Request timeout" |
| 79 | +``` |
| 80 | + |
| 81 | +### Options |
| 82 | + |
| 83 | +| Option | Type | Description | Default | |
| 84 | +| ---------- | ------ | ------------------------------------- | ------------------- | |
| 85 | +| `duration` | Number | Timeout in milliseconds | `5000` | |
| 86 | +| `message` | String | Message to return when timeout occurs | `"Request timeout"` | |
| 87 | + |
| 88 | +## Compression |
| 89 | + |
| 90 | +Compresses response bodies. |
19 | 91 |
|
| 92 | +```toml |
| 93 | +# project.toml |
| 94 | + |
| 95 | +[middleware.compression] |
| 96 | +level = 6 # compression level (1-9) |
| 97 | +threshold = 1024 # minimum size to compress |
20 | 98 | ``` |
21 | 99 |
|
22 | | -## CORS |
| 100 | +### Options |
23 | 101 |
|
24 | | -## Body limit |
| 102 | +| Option | Type | Description | Default | |
| 103 | +| ----------- | ------ | --------------------------------- | -------------------------------------------------------------------- | |
| 104 | +| `level` | Number | Compression level (1-9) | `6` | |
| 105 | +| `threshold` | Number | Minimum size in bytes to compress | `1024` | |
| 106 | +| `types` | Array | Content types to compress | `["text/plain", "text/html", "application/json", "application/xml"]` | |
25 | 107 |
|
26 | | -## Rate Limit |
| 108 | +## Security Headers |
27 | 109 |
|
28 | | -## Timeout |
| 110 | +Adds security-related HTTP headers to responses. |
| 111 | + |
| 112 | +```toml |
| 113 | +# project.toml |
| 114 | + |
| 115 | +[middleware.security_headers] |
| 116 | +xss_protection = "1; mode=block" |
| 117 | +content_security_policy = "default-src 'self'" |
| 118 | +``` |
| 119 | + |
| 120 | +### Options |
| 121 | + |
| 122 | +| Option | Type | Description | Default | |
| 123 | +| ------------------------- | ------ | ------------------------------------ | ------------------------------ | |
| 124 | +| `xss_protection` | String | X-XSS-Protection header value | `"1; mode=block"` | |
| 125 | +| `content_type_options` | String | X-Content-Type-Options header value | `"nosniff"` | |
| 126 | +| `frame_options` | String | X-Frame-Options header value | `"SAMEORIGIN"` | |
| 127 | +| `content_security_policy` | String | Content-Security-Policy header value | `"default-src 'self'"` | |
| 128 | +| `referrer_policy` | String | Referrer-Policy header value | `"no-referrer-when-downgrade"` | |
0 commit comments