-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
requestHandler.go
38 lines (32 loc) · 1.19 KB
/
requestHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package http
import (
"context"
"fmt"
"github.com/fullstorydev/grpchan/inprocgrpc"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/plgd-dev/hub/v2/certificate-authority/pb"
grpcService "github.com/plgd-dev/hub/v2/certificate-authority/service/grpc"
"github.com/plgd-dev/hub/v2/http-gateway/serverMux"
)
// RequestHandler for handling incoming request
type RequestHandler struct {
config *Config
mux *runtime.ServeMux
}
// NewHTTP returns HTTP handler
func NewRequestHandler(config *Config, r *mux.Router, certificateAuthorityServer *grpcService.CertificateAuthorityServer) (*RequestHandler, error) {
requestHandler := &RequestHandler{
config: config,
mux: serverMux.New(),
}
ch := new(inprocgrpc.Channel)
pb.RegisterCertificateAuthorityServer(ch, certificateAuthorityServer)
grpcClient := pb.NewCertificateAuthorityClient(ch)
// register grpc-proxy handler
if err := pb.RegisterCertificateAuthorityHandlerClient(context.Background(), requestHandler.mux, grpcClient); err != nil {
return nil, fmt.Errorf("failed to register certificate-authority handler: %w", err)
}
r.PathPrefix("/").Handler(requestHandler.mux)
return requestHandler, nil
}