Open TinyBeer opened 2 months ago
files for test
D:.
│ go.mod
│ go.sum
│ main.go
│
└─static
hello.txt
It works well when i test http://127.0.0.1:8888/hello.txt
while use this code.
func main() {
h := server.Default(server.WithExitWaitTime(time.Second))
h.Static("/", "./static")
h.Spin()
}
But i'm trying to change the url to http://127.0.0.1:8888/static/hello.txt
.
func main() {
h := server.Default(server.WithExitWaitTime(time.Second))
h.Static("/static", "./static")
h.Spin()
}
error report
2024/05/28 23:07:28.923005 fs.go:856: [Error] HERTZ: Cannot open file="./static/static/hello.txt", error=open ./static/static/hello.txt: The system cannot find the path specified.
Maybe the problem comes from here, where I think we should use ctx.Param("filepath")
instead of ctx.Path()
https://github.com/cloudwego/hertz/blob/0fe11823b76a77cd68fa909993b400f0b0a2df35/pkg/app/fs.go#L801 https://github.com/cloudwego/hertz/blob/0fe11823b76a77cd68fa909993b400f0b0a2df35/pkg/app/fs.go#L838
func (h *fsHandler) handleRequest(c context.Context, ctx *RequestContext) {
var path []byte
if h.pathRewrite != nil {
path = h.pathRewrite(ctx)
} else {
path = ctx.Path() // <----
}
path = stripTrailingSlashes(path)
...
if !ok {
pathStr := string(path)
filePath := h.root + pathStr // <----
var err error
ff, err = h.openFSFile(filePath, mustCompress)
...
handler registered here
// StaticFS works just like `Static()` but a custom `FS` can be used instead.
func (group *RouterGroup) StaticFS(relativePath string, fs *app.FS) IRoutes {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static folder")
}
handler := fs.NewRequestHandler()
urlPattern := path.Join(relativePath, "/*filepath")
// Register GET and HEAD handlers
group.GET(urlPattern, handler)
group.HEAD(urlPattern, handler)
return group.returnObj()
}
Use PathRewrite to rewrite the URI, try this:
h.StaticFS("/assets", &app.FS{Root: "./", GenerateIndexPages: true, PathRewrite: app.NewPathSlashesStripper(1)})
More: https://github.com/cloudwego/hertz-examples/tree/main/file/staticFile
Thanks, it actually works with PathRewrite.
But it makes me more confused about the parameter relativepath
of the method Static.
Describe the bug I'm trying to change relative path of a Static method like
h.Static("/static/", "./static")
. When it comes to visithttp://127.0.0.1:8888/static/hello.txt
. It comes back with message[Cannot open requested path]
. An error report comes out. ··· 2024/05/23 22:47:40.016435 transport.go:65: [Info] HERTZ: HTTP server listening on address=[::]:8888 2024/05/23 22:47:48.879962 fs.go:856: [Error] HERTZ: Cannot open file="./static/static/hello.txt", error=open ./static/static/hello.txt: The system cannot find the path specified. ···It seems like Static join
relative path
androot path
together. To Reproducestatic/hello.txt
/static/
http://127.0.0.1:8888/static/hello.txt
Expected behavior get content of hello.txt Screenshots
Hertz version:
github.com/cloudwego/hertz v0.9.0
Environment:
Additional context
The entire family of Static family has this problem.