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

Adding custom routes to the mux. Cannot add root route. ("/") #1909

Closed
AP0rPi7uQ9 opened this issue Jan 19, 2021 · 2 comments · Fixed by #2202
Closed

Adding custom routes to the mux. Cannot add root route. ("/") #1909

AP0rPi7uQ9 opened this issue Jan 19, 2021 · 2 comments · Fixed by #2202

Comments

@AP0rPi7uQ9
Copy link

AP0rPi7uQ9 commented Jan 19, 2021

Hello, I have feedback on slack. I tested in my app and I think this is a bug

https://grpc-ecosystem.github.io/grpc-gateway/docs/operations/inject_router/

Follow this example, I want to add some routes of my own.

err := mux.HandlePath("GET", "/abc", xxxx)
fmt.Println(err)

This is normal. err == nil, I can successfully access through the browser www.my-url.com/abc

But!!!!!!

err := mux.HandlePath("GET", "/", xxxx)
fmt.Println(err)

This is abnormal. I cannot access through www.my-url.com/, and the err return

parsing path pattern: segment neither wildcards, literal or variable: expected "{" but got "\x00": /
@AP0rPi7uQ9
Copy link
Author

AP0rPi7uQ9 commented Jan 20, 2021

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
2.Copy grpc-gateway/examples/internal/helloworld/ this directory into your project
3.go mod init & go mod vendor
4.go run main.go

@johanbrandhorst
Copy link
Collaborator

Yeah this is easily reproducible with a test:

func TestParse(t *testing.T) {
	_, err := Parse("/")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
}

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.

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

Successfully merging a pull request may close this issue.

2 participants