Skip to content

Commit 5c75aef

Browse files
committed
feat: add proxy service for gin
1 parent 282a77f commit 5c75aef

File tree

5 files changed

+1872
-0
lines changed

5 files changed

+1872
-0
lines changed

pkg/gin/staticfs/README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
## StaticFS
2+
3+
StaticFS is a high-performance static file service designed for the Gin framework. It improves the access speed of static resources and reduces server load through various optimization methods. The main features include:
4+
5+
- Automatically processes index files (such as `index.html`)
6+
- File system caching to reduce disk I/O operations
7+
- HTTP Cache Control to reduce client requests
8+
- Flexible configuration options to adapt to different scenario requirements
9+
- Advanced directory listing and file browser (HTML and JSON API)
10+
- File sorting (by name, size, modification time) and pagination
11+
- Optional file download functionality
12+
- Built-in security filter to hide sensitive files and directories
13+
14+
### 1. Static File Service Usage
15+
16+
This functionality is used as a FileServer in Gin. It allows you to map a URL path to a static file directory on the server.
17+
18+
**Directory Structure:**
19+
20+
```
21+
.
22+
├── static/
23+
│ ├── index.html
24+
│ ├── css/
25+
│ │ └── style.css
26+
│ └── js/
27+
│ └── main.js
28+
└── main.go
29+
```
30+
31+
32+
#### Example of Usage
33+
34+
```go
35+
package main
36+
37+
import (
38+
"log"
39+
40+
"github.com/gin-gonic/gin"
41+
"github.com/go-dev-frame/sponge/pkg/gin/staticfs"
42+
)
43+
44+
func main() {
45+
r := gin.Default()
46+
47+
// Maps the URL prefix /user/ to the local /var/www/dist/index.html
48+
staticfs.StaticFS(r, "/user/", "/var/www/dist")
49+
50+
log.Println("Server is running on http://localhost:8080")
51+
log.Println("Access static files at http://localhost:8080/user/")
52+
53+
r.Run(":8080")
54+
}
55+
```
56+
57+
Now, you can access your static files at the following URLs:
58+
- `http://localhost:8080/user/` -> Serves the content of `static/index.html`
59+
- `http://localhost:8080/user/css/style.css` -> Serves the content of `static/css/style.css`
60+
61+
#### StaticFS Configuration Options
62+
63+
You can customize the behavior of `StaticFS` using the following option functions:
64+
65+
- `WithStaticFSIndexFile(indexFile string)`: Sets the name of the index file to be processed automatically.
66+
- `WithCacheExpiration(duration time.Duration)`: sets the cache expiration time.
67+
- `WithCacheMaxAge(duration time.Duration)`: sets the Cache-Control max-age header value.
68+
- `WithCacheSize(size int)`: sets the maximum number of entries in the file existence cache.
69+
- `WithMiddlewares(middlewares ...gin.HandlerFunc)`: sets middlewares for staticFS.
70+
71+
<br>
72+
73+
### 2. Directory Listing and File Browser
74+
75+
In addition to serving static files, StaticFS provides a powerful directory listing feature that can be used as a simple file browser. This feature registers its routes directly on the Gin engine and offers both an HTML interface and a JSON API.
76+
77+
#### Key Features
78+
79+
- **Web UI Interface**: Browse server directories through a web page.
80+
- **JSON API**: Provides an API for programmatic access to directory contents.
81+
- **File Sorting**: Supports sorting by file name, size, or modification time in ascending or descending order.
82+
- **Pagination**: Automatically paginates the file list when a directory contains many files.
83+
- **File Download**: Optionally enables a file download feature.
84+
- **Security Filtering**: By default, it filters sensitive files and directories (e.g., `.git`, `.env`, `/proc`). Custom filter rules are also supported.
85+
86+
#### Example of Usage
87+
88+
```go
89+
package main
90+
91+
import (
92+
"log"
93+
94+
"github.com/gin-gonic/gin"
95+
"github.com/go-dev-frame/sponge/pkg/gin/staticfs"
96+
)
97+
98+
func main() {
99+
r := gin.Default()
100+
101+
// Register the directory listing routes /dir/list and /dir/list/api
102+
staticfs.ListDir(r)
103+
104+
log.Println("Server is running on http://localhost:8080")
105+
log.Println("Access the file browser at: http://localhost:8080/dir/list?dir=/path/to/your/directory")
106+
107+
r.Run(":8080")
108+
}
109+
```
110+
111+
#### Accessing URLs
112+
113+
- **HTML Browser Interface**:
114+
`http://localhost:8080/dir/list?dir=/path/to/your/directory`
115+
- `dir`: Required parameter. Specifies the directory path to browse.
116+
- `sort`: Optional parameter. The basis for sorting (`name`, `size`, `time`).
117+
- `order`: Optional parameter. The sort order (`asc`, `desc`).
118+
- `page`: Optional parameter. The page number.
119+
120+
- **File Download Endpoint** (requires `WithListDirDownload()`):
121+
`http://localhost:8080/dir/file/download?path=/path/to/your/file.txt`
122+
- `path`: Required parameter. The full path of the file to download.
123+
124+
- **JSON API Endpoint**:
125+
`http://localhost:8080/dir/list/api?dir=/path/to/your/directory`
126+
- Returns JSON data of the files and subdirectories within the specified directory.
127+
128+
#### ListDir Configuration Options
129+
130+
You can customize the behavior of `ListDir` using the following option functions:
131+
132+
- `WithListDirPrefixPath(prefix string)`: Sets a common URL prefix for all related routes.
133+
- `WithListDirDownload()`: Enables the file download feature and its corresponding `/dir/file/download` route.
134+
- `WithListDirFilter(enable bool)`: Enables or disables the security filter. It is `true` (enabled) by default.
135+
- `WithListDirFilesFilter(filters ...string)`: Adds custom file name filters. Matched files will be hidden.
136+
- `WithListDirDirsFilter(filters ...string)`: Adds custom directory path filters. Matched directories will be hidden.

0 commit comments

Comments
 (0)