-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Adding custom routes to the mux. Cannot add root route. ("/") #1909
Comments
This is an example. Edit by https://grpc-ecosystem.github.io/grpc-gateway/docs/operations/inject_router/. This example will panic. and show error message. main.go: package main
import (
"context"
"net/http"
"testgw/helloworld"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)
func main() {
ctx := context.TODO()
mux := runtime.NewServeMux()
// Register generated routes to mux
err := helloworld.RegisterGreeterHandlerServer(ctx, mux, &GreeterServer{})
if err != nil {
panic(err)
}
// Register custom route for GET /hello/{name}
// This route is OK!
err = mux.HandlePath("GET", "/hello/{name}", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
w.Write([]byte("hello " + pathParams["name"]))
})
if err != nil {
panic(err)
}
// This route is OK!
err = mux.HandlePath("GET", "/test/{name}", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
w.Write([]byte("test with name " + pathParams["name"]))
})
if err != nil {
panic(err)
}
// This route is OK!
err = mux.HandlePath("GET", "/test", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
w.Write([]byte("this is test " + pathParams["name"]))
})
if err != nil {
panic(err)
}
// This route will be PANIC
// Return: panic: parsing path pattern: segment neither wildcards, literal or variable: expected "{" but got "\x00": /
err = mux.HandlePath("GET", "/", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
w.Write([]byte("this is homepage"))
})
if err != nil {
panic(err)
}
http.ListenAndServe(":8080", mux)
}
// GreeterServer is the server API for Greeter service.
type GreeterServer struct {
}
// SayHello implement to say hello
func (h *GreeterServer) SayHello(ctx context.Context, req *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
return &helloworld.HelloReply{
Message: "hello " + req.Name,
}, nil
} return panic: parsing path pattern: segment neither wildcards, literal or variable: expected "{" but got "\x00": /
goroutine 1 [running]:
main.main()
C:/Users/User/go/src/testgw/main.go:46 +0x2c4
exit status 2 How to run example:1.mkdir testgw in GOPATH & Copy MY Example main.go code into your project |
Yeah this is easily reproducible with a test:
Unfortunately, I'm not sure what the fix should be. You'll want to dive into https://github.com/grpc-ecosystem/grpc-gateway/blob/master/internal/httprule/parse.go#L19 to see what we should be returning in this case. |
Hello, I have feedback on slack. I tested in my app and I think this is a bug
Follow this example, I want to add some routes of my own.
This is normal. err == nil, I can successfully access through the browser www.my-url.com/abc
But!!!!!!
This is abnormal. I cannot access through www.my-url.com/, and the
err
returnThe text was updated successfully, but these errors were encountered: