Skip to content

Commit ba7e8d0

Browse files
Support file server (#1498)
* [enhancement] open branch for static file server * [enhancement] import lru v2 version * add basic filter framework for fileserver * [enhancement] support get file with ttl * [enhancement] support mmap * [enhancement] add ttl for mmap * update filehandler * [enhancement] add prometheus metrics for cache pool * [enhancement] support etag * [enhancement] support Last-Modified * [enhancement] support set root dir, support hidden files, add tests * [enhancement] add index file * [enhancement] support try files * [enhancement] support rewrite path logic * [enhancement] support precompressed * [enhancement] support compress for small file * [enhancement] support compress for large files * [enhancement] support cache path parser * [test] add test to serve files and fix bugs * [bugfix] registry missing filters * [doc] add doc for fileserver * [testing] fix windows path * [testing] only support extension filter * [testing] fix test * add log to test windows path * [testing] fix test on windows * log windows path * [testing] fix test on windows * log for windows path * [testing] clean log --------- Co-authored-by: chen <chen.su1994@outlook.com>
1 parent 10874f4 commit ba7e8d0

File tree

11 files changed

+1633
-0
lines changed

11 files changed

+1633
-0
lines changed

docs/07.Reference/7.02.Filters.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,94 @@ filters:
18271827
| internalError | Error occurred during processing the request |
18281828

18291829

1830+
## FileServer
1831+
1832+
Miniuim config example:
1833+
1834+
```yaml
1835+
name: fileserver-pipeline
1836+
kind: Pipeline
1837+
filters:
1838+
- name: fileserver-filter
1839+
kind: FileServer
1840+
root: "/var/www/example"
1841+
```
1842+
1843+
Complete config example:
1844+
1845+
```yaml
1846+
name: fileserver-pipeline
1847+
kind: Pipeline
1848+
filters:
1849+
- name: fileserver-filter
1850+
kind: FileServer
1851+
root: "/var/www/example"
1852+
index: ["index.html", "index.txt"] # default is index.html and index.txt
1853+
hidden: [".git", "/build", "*/*.conf"]
1854+
tryFiles: ["{path}", "{path}/", "/index.html", "=404 error message"]
1855+
rewrite: "^/users/(\d+)$ /users.php?id=$1"
1856+
precompressed: "gzip br"
1857+
compress: "gzip"
1858+
cache:
1859+
bufferPoolSize: 2 * 1024 * 1024 * 1024 # default is 2GB
1860+
bufferPoolMaxFileSize: 10 * 1024 * 1024 # default is 10MB
1861+
bufferPoolTTL: 600 # default is 600 seconds
1862+
cacheFileExtensionFilters:
1863+
- pattern: ["*.html", "*.css"]
1864+
etagMaxAge: 3600 # default is 3600
1865+
- pattern: ["/usr/*.js"]
1866+
```
1867+
1868+
1869+
### Configuration
1870+
1871+
1872+
| Name | Type | Description | Required |
1873+
| --- | --- | --- | --- |
1874+
| root | string | The root directory path from which to serve files. | Yes |
1875+
| index | []string | A list of filenames to look for, in order, when a directory is requested. Defaults to `["index.html", "index.txt"]`. | No |
1876+
| hidden | []string | A list of glob patterns for files or directories to hide. Requests for these files will be treated as if they do not exist (404 Not Found). | No |
1877+
| tryFiles | []string | A sequence of files to try serving in order. If the previous file is not found, the next one is tried. Useful for Single-Page Applications (SPAs) or front-controller patterns. Supports placeholders like `{path}` and can end with an error code like `=404`. | No |
1878+
| rewrite | string | A rewrite rule used to internally modify the request URI before file lookups. It uses regular expression matching and capture groups (e.g., `$1`). | No |
1879+
| precompressed | string | A space-separated list of precompressed formats (e.g., `gzip br`). For a request to `/file`, it will check for the existence of `/file.gz`, `/file.br`, etc. | No |
1880+
| compress | string | The compression method (e.g., `gzip`) to use for on-the-fly compression of responses that are not precompressed. | No |
1881+
| cache | [CacheSpec](#fileservercachespec) | A container for configuring caching-related settings. | No |
1882+
1883+
#### More about hidden rules
1884+
##### 1. Component Patterns (rules without a path separator)
1885+
These patterns (e.g., ".git", "*.log") are matched against each individual name component of the relative path.
1886+
```
1887+
- Rule: ".git"
1888+
- Hides: "/path/to/project/.git", "/path/to/.git/config"
1889+
- Does NOT hide: "/path/to/project/git"
1890+
1891+
- Rule: "node_modules"
1892+
- Hides: "/path/to/project/node_modules", "/path/to/node_modules/express"
1893+
```
1894+
1895+
##### 2. Path Patterns (rules with a path separator):
1896+
These patterns are matched against the full absolute path. This matching is done in two ways:
1897+
1898+
```
1899+
a) As a prefix: If the rule is a prefix of the path, followed by a path separator, it's a match.
1900+
- Rule: "/build"
1901+
- Hides: "/build/app.js", "/build/"
1902+
- Does NOT hide: "/builder/app.js"
1903+
1904+
b) As a glob pattern: The rule is treated as a glob pattern to be matched against the entire absolute path.
1905+
- Rule: "/*/*/*.conf"
1906+
- Hides: "/etc/nginx/nginx.conf"
1907+
- Does NOT hide: "/etc/nginx.conf"
1908+
```
1909+
1910+
### Results
1911+
1912+
| Value | Description |
1913+
| --- | --- |
1914+
| internalError | An internal error occurred within the FileServer system |
1915+
| serverError | A server-side error occurred |
1916+
| clientError | The client's request was blocked |
1917+
| notFound | A required resource could not be found |
18301918
18311919
## Common Types
18321920
@@ -2324,3 +2412,25 @@ template: |
23242412
|------|------|-------------|----------|
23252413
| header | [httpheader.AdaptSpec](#httpheaderadaptspec) | Rules to revise request header | No |
23262414
| body | string | If provided the body of the original request is replaced by the value of this option. | No |
2415+
2416+
2417+
2418+
### fileserver.CacheSpec
2419+
2420+
This configuration is nested under the `cache` field.
2421+
2422+
| Name | Type | Description | Required |
2423+
| --- | --- | --- | --- |
2424+
| bufferPoolSize | integer | The maximum size of the in-memory cache pool in bytes. Defaults to `2147483648` (2 GB). | No |
2425+
| bufferPoolMaxFileSize | integer | The maximum size in bytes that a single file can be to be cached in the buffer pool. Defaults to `10485760` (10 MB). | No |
2426+
| bufferPoolTTL | integer | The time-to-live (TTL) for items in the cache pool, in seconds. Defaults to `600`. | No |
2427+
| cacheFileExtensionFilters | [][FileExtensionFilter](#fileserverfileextensionfilter) | A list of rules to control `ETag` cache headers based on file patterns. | No |
2428+
2429+
### fileserver.FileExtensionFilter
2430+
2431+
This configuration is for each object within the `cacheFileExtensionFilters` array.
2432+
2433+
| Name | Type | Description | Required |
2434+
| --- | --- | --- | --- |
2435+
| pattern | []string | A list of glob patterns to match files against (e.g., `["*.html", "*.css"]`). | Yes |
2436+
| etagMaxAge | integer | The `max-age` value, in seconds, to set for the `ETag` response header for files matching this pattern. Defaults to `3600`. | No |

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/gorilla/mux v1.8.0
3232
github.com/hashicorp/consul/api v1.26.1
3333
github.com/hashicorp/golang-lru v1.0.2
34+
github.com/hashicorp/golang-lru/v2 v2.0.7
3435
github.com/invopop/jsonschema v0.12.0
3536
github.com/invopop/yaml v0.2.0
3637
github.com/jackc/pgx/v5 v5.7.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
524524
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
525525
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
526526
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
527+
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
528+
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
527529
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
528530
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
529531
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=

0 commit comments

Comments
 (0)