-
Notifications
You must be signed in to change notification settings - Fork 556
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
[Question] Static method not work welll with relative path. #1121
Comments
files for test D:.
│ go.mod
│ go.sum
│ main.go
│
└─static
hello.txt It works well when i test func main() {
h := server.Default(server.WithExitWaitTime(time.Second))
h.Static("/", "./static")
h.Spin()
} But i'm trying to change the url to 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 Line 801 in 0fe1182
Line 838 in 0fe1182
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:
More: https://github.com/cloudwego/hertz-examples/tree/main/file/staticFile |
Thanks, it actually works with PathRewrite. https://github.com/cloudwego/hertz-examples/blob/36af1b1f7ef584a674db94620b22e62edfb95a96/file/staticFile/main.go#L31 hertz/pkg/route/routergroup.go Line 204 in 0fe1182
|
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 Reproduce
static/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.
The text was updated successfully, but these errors were encountered: