-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add HandlePath * add HandlePath test and docs
- Loading branch information
Showing
4 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
category: documentation | ||
title: Adding custom routes to the mux | ||
--- | ||
|
||
# Adding custom routes to the mux | ||
|
||
The gRPC-gateway allows you to add custom routes to the serve mux, for example if you want to support a use case that isn't supported by the grpc-gateway, like file uploads. | ||
|
||
## Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
pb "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/helloworld" | ||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
) | ||
|
||
func main() { | ||
ctx := context.TODO() | ||
mux := runtime.NewServeMux() | ||
// Register generated routes to mux | ||
err := pb.RegisterGreeterHandlerServer(ctx, mux, &GreeterServer{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// Register custom route for GET /hello/{name} | ||
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) | ||
} | ||
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 *pb.HelloRequest) (*pb.HelloReply, error) { | ||
return &pb.HelloReply{ | ||
Message: "hello " + req.Name, | ||
}, nil | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters