-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Does go zero support api wildcard? #3716
Comments
Regarding your issue, you might try using a route definition like |
Yes, I tried to do a file server in go zero project, where I hoped to visit the files in a sub-directory. |
I have a example of file server that supporting sub-directory. package main
import (
"github.com/zeromicro/go-zero/rest/router"
"net/http"
"strings"
"github.com/zeromicro/go-zero/rest"
)
func dirHandler(prefix, fileDir string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
handler := http.StripPrefix(prefix, http.FileServer(http.Dir(fileDir)))
handler.ServeHTTP(w, req)
}
}
func registerHandlers(engine *rest.Server, prefix, dirPath string) {
// Set up the dir level
dirLevel := []string{":1", ":2", ":3", ":4", ":5", ":6", ":7", ":8"}
for i := 1; i < len(dirLevel); i++ {
path := prefix + strings.Join(dirLevel[:i], "/")
engine.AddRoute(
rest.Route{
Method: http.MethodGet,
Path: path,
Handler: dirHandler(prefix, dirPath),
})
}
}
func main() {
// custom 404
rt := router.NewRouter()
rt.SetNotFoundHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("404 not found."))
}))
server := rest.MustNewServer(rest.RestConf{Port: 8080}, rest.WithRouter(rt))
//Register file handler
registerHandlers(server, "/static/", "./assets/")
server.Start()
} reference: |
thank you, it helps me a lot |
this is just a tricky way, we really need to support wildcard route |
I created a file server in go zero in hope of visiting the sub-directory. However, it didn't work. I found the doc where I couldn't find any wildcard information. It seems that go zero only support one level route-path matching.
The text was updated successfully, but these errors were encountered: