Last active
February 28, 2022 08:52
-
-
Save sang-w0o/f14aba93fc502af201ac2bc89682fe41 to your computer and use it in GitHub Desktop.
grpc_gateway_2.go
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
func New() { | |
// chi.Router 초기화 및 middleware 적용 | |
// gRPC Service 구현체에 context를 넘기기 위한 코드 | |
annotationFunc := func(ctx context.Context, request *http.Request) metadata.MD { | |
return metadata.New(map[string]string{ | |
"key1": "value1", | |
"key2": "value2", | |
}) | |
} | |
serveMuxOption := runtime.WithMetadata(annotationFunc) | |
// grpc-gateway 사용을 위한 코드 | |
mux := runtime.NewServeMux(serveMuxOption) | |
opts := []grpc.DialOption{ | |
grpc.WithTransportCredentials(insecure.NewCredentials()), | |
} | |
err = adminRPC.RegisterAdminHandlerFromEndpoint(ctx, mux, fmt.Sprintf("localhost:%d", RPCPort), opts) | |
if err != nil { | |
// handle error | |
} | |
router.Route("/api/", func(r chi.Router) { | |
r.HandleFunc("/admin/ex1", func(w http.ResponseWriter, r *http.Request) { | |
mux.ServeHTTP(w, r) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment