Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support embed file system to serve files in rest #4253

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions rest/internal/fileserver/filehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"strings"
)

func Middleware(path, dir string) func(http.HandlerFunc) http.HandlerFunc {
fileServer := http.FileServer(http.Dir(dir))
// Middleware returns a middleware that serves files from the given file system.
func Middleware(path string, fs http.FileSystem) func(http.HandlerFunc) http.HandlerFunc {
fileServer := http.FileServer(fs)
pathWithTrailSlash := ensureTrailingSlash(path)
pathWithoutTrailSlash := ensureNoTrailingSlash(path)

Expand Down
2 changes: 1 addition & 1 deletion rest/internal/fileserver/filehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestMiddleware(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
middleware := Middleware(tt.path, tt.dir)
middleware := Middleware(tt.path, http.Dir(tt.dir))
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
Expand Down
8 changes: 4 additions & 4 deletions rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ func WithCustomCors(middlewareFn func(header http.Header), notAllowedFn func(htt
}

// WithFileServer returns a RunOption to serve files from given dir with given path.
func WithFileServer(path, dir string) RunOption {
func WithFileServer(path string, fs http.FileSystem) RunOption {
return func(server *Server) {
server.router = newFileServingRouter(server.router, path, dir)
server.router = newFileServingRouter(server.router, path, fs)
}
}

Expand Down Expand Up @@ -351,10 +351,10 @@ type fileServingRouter struct {
middleware Middleware
}

func newFileServingRouter(router httpx.Router, path, dir string) httpx.Router {
func newFileServingRouter(router httpx.Router, path string, fs http.FileSystem) httpx.Router {
return &fileServingRouter{
Router: router,
middleware: fileserver.Middleware(path, dir),
middleware: fileserver.Middleware(path, fs),
}
}

Expand Down
28 changes: 25 additions & 3 deletions rest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package rest

import (
"crypto/tls"
"embed"
"fmt"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -21,6 +23,11 @@ import (
"github.com/zeromicro/go-zero/rest/router"
)

const (
exampleContent = "example content"
sampleContent = "sample content"
)

func TestNewServer(t *testing.T) {
logtest.Discard(t)

Expand Down Expand Up @@ -199,7 +206,7 @@ func TestWithFileServerMiddleware(t *testing.T) {
dir: "./testdata",
requestPath: "/assets/example.txt",
expectedStatus: http.StatusOK,
expectedContent: "example content",
expectedContent: exampleContent,
},
{
name: "Pass through non-matching path",
Expand All @@ -214,13 +221,13 @@ func TestWithFileServerMiddleware(t *testing.T) {
dir: "testdata",
requestPath: "/static/sample.txt",
expectedStatus: http.StatusOK,
expectedContent: "sample content",
expectedContent: sampleContent,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := MustNewServer(RestConf{}, WithFileServer(tt.path, tt.dir))
server := MustNewServer(RestConf{}, WithFileServer(tt.path, http.Dir(tt.dir)))
req := httptest.NewRequest(http.MethodGet, tt.requestPath, nil)
rr := httptest.NewRecorder()

Expand Down Expand Up @@ -688,3 +695,18 @@ Port: 54321
})
}
}

//go:embed testdata
var content embed.FS

func TestServerEmbedFileSystem(t *testing.T) {
filesys, err := fs.Sub(content, "testdata")
assert.NoError(t, err)

server := MustNewServer(RestConf{}, WithFileServer("/assets", http.FS(filesys)))
req, err := http.NewRequest(http.MethodGet, "/assets/sample.txt", http.NoBody)
assert.Nil(t, err)
rr := httptest.NewRecorder()
server.ServeHTTP(rr, req)
assert.Equal(t, sampleContent, rr.Body.String())
}