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

Does go zero support api wildcard? #3716

Closed
DominguitoLamo opened this issue Nov 12, 2023 · 5 comments
Closed

Does go zero support api wildcard? #3716

DominguitoLamo opened this issue Nov 12, 2023 · 5 comments

Comments

@DominguitoLamo
Copy link

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.

@fynxiu
Copy link
Contributor

fynxiu commented Nov 12, 2023

Regarding your issue, you might try using a route definition like /static/:file, which I suggest as a potential solution.
If you could provide some specific code examples, it would help us understand your issue better and allow us to provide more detailed solutions or code suggestions.

@DominguitoLamo
Copy link
Author

Regarding your issue, you might try using a route definition like /static/:file, which I suggest as a potential solution. If you could provide some specific code examples, it would help us understand your issue better and allow us to provide more detailed solutions or code suggestions.

Yes, I tried to do a file server in go zero project, where I hoped to visit the files in a sub-directory.

@POABOB
Copy link
Contributor

POABOB commented Nov 21, 2023

Regarding your issue, you might try using a route definition like /static/:file, which I suggest as a potential solution. If you could provide some specific code examples, it would help us understand your issue better and allow us to provide more detailed solutions or code suggestions.

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.
The registerHandlers is the most important func that implentment 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()
}

My files:
image
Browser:
image

reference:

@DominguitoLamo
Copy link
Author

Regarding your issue, you might try using a route definition like /static/:file, which I suggest as a potential solution. If you could provide some specific code examples, it would help us understand your issue better and allow us to provide more detailed solutions or code suggestions.

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. The registerHandlers is the most important func that implentment 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()
}

My files: image Browser: image

reference:

thank you, it helps me a lot

@vuhoanglam
Copy link

this is just a tricky way, we really need to support wildcard route

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants