mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
feat: handle using root as the path of file server (#4255)
This commit is contained in:
@@ -3,18 +3,19 @@ package fileserver
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Middleware returns a middleware that serves files from the given file system.
|
// Middleware returns a middleware that serves files from the given file system.
|
||||||
func Middleware(path string, fs http.FileSystem) func(http.HandlerFunc) http.HandlerFunc {
|
func Middleware(path string, fs http.FileSystem) func(http.HandlerFunc) http.HandlerFunc {
|
||||||
fileServer := http.FileServer(fs)
|
fileServer := http.FileServer(fs)
|
||||||
pathWithTrailSlash := ensureTrailingSlash(path)
|
|
||||||
pathWithoutTrailSlash := ensureNoTrailingSlash(path)
|
pathWithoutTrailSlash := ensureNoTrailingSlash(path)
|
||||||
|
canServe := createServeChecker(path, fs)
|
||||||
|
|
||||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, pathWithTrailSlash) {
|
if canServe(r) {
|
||||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, pathWithoutTrailSlash)
|
r.URL.Path = r.URL.Path[len(pathWithoutTrailSlash):]
|
||||||
fileServer.ServeHTTP(w, r)
|
fileServer.ServeHTTP(w, r)
|
||||||
} else {
|
} else {
|
||||||
next(w, r)
|
next(w, r)
|
||||||
@@ -23,6 +24,44 @@ func Middleware(path string, fs http.FileSystem) func(http.HandlerFunc) http.Han
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createFileChecker(fs http.FileSystem) func(string) bool {
|
||||||
|
var lock sync.RWMutex
|
||||||
|
fileChecker := make(map[string]bool)
|
||||||
|
|
||||||
|
return func(path string) bool {
|
||||||
|
lock.RLock()
|
||||||
|
exist, ok := fileChecker[path]
|
||||||
|
lock.RUnlock()
|
||||||
|
if ok {
|
||||||
|
return exist
|
||||||
|
}
|
||||||
|
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
file, err := fs.Open(path)
|
||||||
|
exist = err == nil
|
||||||
|
fileChecker[path] = exist
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = file.Close()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createServeChecker(path string, fs http.FileSystem) func(r *http.Request) bool {
|
||||||
|
pathWithTrailSlash := ensureTrailingSlash(path)
|
||||||
|
fileChecker := createFileChecker(fs)
|
||||||
|
|
||||||
|
return func(r *http.Request) bool {
|
||||||
|
return r.Method == http.MethodGet &&
|
||||||
|
strings.HasPrefix(r.URL.Path, pathWithTrailSlash) &&
|
||||||
|
fileChecker(r.URL.Path[len(pathWithTrailSlash):])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ensureTrailingSlash(path string) string {
|
func ensureTrailingSlash(path string) string {
|
||||||
if strings.HasSuffix(path, "/") {
|
if strings.HasSuffix(path, "/") {
|
||||||
return path
|
return path
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func TestMiddleware(t *testing.T) {
|
|||||||
path: "/static/",
|
path: "/static/",
|
||||||
dir: "./testdata",
|
dir: "./testdata",
|
||||||
requestPath: "/other/path",
|
requestPath: "/other/path",
|
||||||
expectedStatus: http.StatusNotFound,
|
expectedStatus: http.StatusAlreadyReported,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Directory with trailing slash",
|
name: "Directory with trailing slash",
|
||||||
@@ -40,25 +40,48 @@ func TestMiddleware(t *testing.T) {
|
|||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedContent: "2",
|
expectedContent: "2",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Not exist file",
|
||||||
|
path: "/assets",
|
||||||
|
dir: "testdata",
|
||||||
|
requestPath: "/assets/not-exist.txt",
|
||||||
|
expectedStatus: http.StatusAlreadyReported,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Not exist file in root",
|
||||||
|
path: "/",
|
||||||
|
dir: "testdata",
|
||||||
|
requestPath: "/not-exist.txt",
|
||||||
|
expectedStatus: http.StatusAlreadyReported,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "websocket request",
|
||||||
|
path: "/",
|
||||||
|
dir: "testdata",
|
||||||
|
requestPath: "/ws",
|
||||||
|
expectedStatus: http.StatusAlreadyReported,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
middleware := Middleware(tt.path, http.Dir(tt.dir))
|
middleware := Middleware(tt.path, http.Dir(tt.dir))
|
||||||
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusAlreadyReported)
|
||||||
})
|
})
|
||||||
|
|
||||||
handlerToTest := middleware(nextHandler)
|
handlerToTest := middleware(nextHandler)
|
||||||
|
|
||||||
req := httptest.NewRequest("GET", tt.requestPath, nil)
|
for i := 0; i < 2; i++ {
|
||||||
rr := httptest.NewRecorder()
|
req := httptest.NewRequest(http.MethodGet, tt.requestPath, nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
handlerToTest.ServeHTTP(rr, req)
|
handlerToTest.ServeHTTP(rr, req)
|
||||||
|
|
||||||
assert.Equal(t, tt.expectedStatus, rr.Code)
|
assert.Equal(t, tt.expectedStatus, rr.Code)
|
||||||
if len(tt.expectedContent) > 0 {
|
if len(tt.expectedContent) > 0 {
|
||||||
assert.Equal(t, tt.expectedContent, rr.Body.String())
|
assert.Equal(t, tt.expectedContent, rr.Body.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user