diff --git a/README.md b/README.md index 5432be2..db72785 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ import ( - create `pkg/v1/utils/converter` dir, create `converter.go` file in there, to convert camelcase to snake_case - create `pkg/v1/postgres/custom.main.go` to implement all query to database table custom - changes `configs/configs.go` to bundle pg connection -- how to use custom.main.go call function from custom main in api status, check `api/v1/health/status.go` +- how to use custom.main.go call function from api calldb, check `api/v1/health/calldb.go` ### Example of call Other Rest API - add new environtment to `config.yaml` add cert path @@ -184,4 +184,4 @@ import ( - create `pkg/v1/cert` dir, create `cert.go`, this file is for handle Insecure ssl connection (self gen cert) - create `pkg/v1/requestapi` dir, create `requestapi.go`, implement http call - create `services/v1/jsonplaceholder` dir, create service name `jsonplaceholder.go`, implement any of endpoint http call from requestapi.go -- create new method from service health and implement call jsonplaceholder service from there ex: `api/v1/health/callapi.go` +- create new method from service health and implement call jsonplaceholder service from there, check `api/v1/health/callapi.go` diff --git a/api/v1/health/status.go b/api/v1/health/status.go index 7ee3eb8..f03b58d 100644 --- a/api/v1/health/status.go +++ b/api/v1/health/status.go @@ -2,30 +2,13 @@ package health import ( "context" - "errors" - "github.com/ajikamaludin/go-grpc_basic/pkg/v1/postgres" "github.com/ajikamaludin/go-grpc_basic/pkg/v1/utils/constants" hlpb "github.com/ajikamaludin/go-grpc_basic/proto/v1/health" "github.com/golang/protobuf/ptypes/empty" ) func (s *Server) Status(ctx context.Context, req *empty.Empty) (*hlpb.Response, error) { - // select db - rows, err := s.config.Pg.CustomMainSelect(&postgres.CustomMain{UserId: "abc"}) - - if err != nil { - s.logger.Errorf("[HEALTH][GET] ERROR %v", err) - } - - if len(rows) <= 0 { - s.logger.Errorf("[HEALTH][GET] EMPTY ROW") - return nil, errors.New("Invoke empty rows") - } else { - for _, v := range rows { - s.logger.Infof("[HEALTH][GET][ROW] %v", v) - } - } return &hlpb.Response{ Success: true, diff --git a/proto/v1/health/health.pb.go b/proto/v1/health/health.pb.go index 6cb0ef5..066fa59 100644 --- a/proto/v1/health/health.pb.go +++ b/proto/v1/health/health.pb.go @@ -210,7 +210,7 @@ var file_v1_health_health_proto_rawDesc = []byte{ 0x52, 0x04, 0x64, 0x65, 0x73, 0x63, 0x12, 0x2e, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x67, 0x6f, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x76, 0x31, 0x2e, 0x68, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x2e, 0x44, 0x61, 0x74, 0x61, - 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0xd3, 0x01, 0x0a, 0x0d, 0x48, 0x65, 0x61, 0x6c, 0x74, + 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0xb4, 0x02, 0x0a, 0x0d, 0x48, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x5f, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x1e, 0x2e, 0x61, 0x70, 0x69, @@ -223,11 +223,17 @@ var file_v1_health_health_proto_rawDesc = []byte{ 0x70, 0x69, 0x2e, 0x67, 0x6f, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x76, 0x31, 0x2e, 0x68, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1e, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x18, 0x12, 0x16, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x68, 0x65, - 0x61, 0x6c, 0x74, 0x68, 0x2f, 0x63, 0x61, 0x6c, 0x6c, 0x61, 0x70, 0x69, 0x42, 0x37, 0x5a, 0x35, - 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x6a, 0x69, 0x6b, 0x61, - 0x6d, 0x61, 0x6c, 0x75, 0x64, 0x69, 0x6e, 0x2f, 0x67, 0x6f, 0x2d, 0x67, 0x72, 0x70, 0x63, 0x5f, - 0x62, 0x61, 0x73, 0x69, 0x63, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x76, 0x31, 0x2f, 0x68, - 0x65, 0x61, 0x6c, 0x74, 0x68, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x6c, 0x74, 0x68, 0x2f, 0x63, 0x61, 0x6c, 0x6c, 0x61, 0x70, 0x69, 0x12, 0x5f, 0x0a, 0x06, + 0x43, 0x61, 0x6c, 0x6c, 0x44, 0x62, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x1e, + 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x67, 0x6f, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x76, 0x31, 0x2e, 0x68, + 0x65, 0x61, 0x6c, 0x74, 0x68, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1d, + 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x17, 0x12, 0x15, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, + 0x68, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x2f, 0x63, 0x61, 0x6c, 0x6c, 0x64, 0x62, 0x42, 0x37, 0x5a, + 0x35, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x6a, 0x69, 0x6b, + 0x61, 0x6d, 0x61, 0x6c, 0x75, 0x64, 0x69, 0x6e, 0x2f, 0x67, 0x6f, 0x2d, 0x67, 0x72, 0x70, 0x63, + 0x5f, 0x62, 0x61, 0x73, 0x69, 0x63, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x76, 0x31, 0x2f, + 0x68, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -252,10 +258,12 @@ var file_v1_health_health_proto_depIdxs = []int32{ 0, // 0: api.gogrpc.v1.health.Response.data:type_name -> api.gogrpc.v1.health.Data 2, // 1: api.gogrpc.v1.health.HealthService.Status:input_type -> google.protobuf.Empty 2, // 2: api.gogrpc.v1.health.HealthService.CallApi:input_type -> google.protobuf.Empty - 1, // 3: api.gogrpc.v1.health.HealthService.Status:output_type -> api.gogrpc.v1.health.Response - 1, // 4: api.gogrpc.v1.health.HealthService.CallApi:output_type -> api.gogrpc.v1.health.Response - 3, // [3:5] is the sub-list for method output_type - 1, // [1:3] is the sub-list for method input_type + 2, // 3: api.gogrpc.v1.health.HealthService.CallDb:input_type -> google.protobuf.Empty + 1, // 4: api.gogrpc.v1.health.HealthService.Status:output_type -> api.gogrpc.v1.health.Response + 1, // 5: api.gogrpc.v1.health.HealthService.CallApi:output_type -> api.gogrpc.v1.health.Response + 1, // 6: api.gogrpc.v1.health.HealthService.CallDb:output_type -> api.gogrpc.v1.health.Response + 4, // [4:7] is the sub-list for method output_type + 1, // [1:4] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name @@ -326,6 +334,7 @@ const _ = grpc.SupportPackageIsVersion6 type HealthServiceClient interface { Status(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*Response, error) CallApi(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*Response, error) + CallDb(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*Response, error) } type healthServiceClient struct { @@ -354,10 +363,20 @@ func (c *healthServiceClient) CallApi(ctx context.Context, in *emptypb.Empty, op return out, nil } +func (c *healthServiceClient) CallDb(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*Response, error) { + out := new(Response) + err := c.cc.Invoke(ctx, "/api.gogrpc.v1.health.HealthService/CallDb", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // HealthServiceServer is the server API for HealthService service. type HealthServiceServer interface { Status(context.Context, *emptypb.Empty) (*Response, error) CallApi(context.Context, *emptypb.Empty) (*Response, error) + CallDb(context.Context, *emptypb.Empty) (*Response, error) } // UnimplementedHealthServiceServer can be embedded to have forward compatible implementations. @@ -370,6 +389,9 @@ func (*UnimplementedHealthServiceServer) Status(context.Context, *emptypb.Empty) func (*UnimplementedHealthServiceServer) CallApi(context.Context, *emptypb.Empty) (*Response, error) { return nil, status.Errorf(codes.Unimplemented, "method CallApi not implemented") } +func (*UnimplementedHealthServiceServer) CallDb(context.Context, *emptypb.Empty) (*Response, error) { + return nil, status.Errorf(codes.Unimplemented, "method CallDb not implemented") +} func RegisterHealthServiceServer(s *grpc.Server, srv HealthServiceServer) { s.RegisterService(&_HealthService_serviceDesc, srv) @@ -411,6 +433,24 @@ func _HealthService_CallApi_Handler(srv interface{}, ctx context.Context, dec fu return interceptor(ctx, in, info, handler) } +func _HealthService_CallDb_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(emptypb.Empty) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(HealthServiceServer).CallDb(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/api.gogrpc.v1.health.HealthService/CallDb", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(HealthServiceServer).CallDb(ctx, req.(*emptypb.Empty)) + } + return interceptor(ctx, in, info, handler) +} + var _HealthService_serviceDesc = grpc.ServiceDesc{ ServiceName: "api.gogrpc.v1.health.HealthService", HandlerType: (*HealthServiceServer)(nil), @@ -423,6 +463,10 @@ var _HealthService_serviceDesc = grpc.ServiceDesc{ MethodName: "CallApi", Handler: _HealthService_CallApi_Handler, }, + { + MethodName: "CallDb", + Handler: _HealthService_CallDb_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "v1/health/health.proto", diff --git a/proto/v1/health/health.pb.gw.go b/proto/v1/health/health.pb.gw.go index 0d0ff0e..e1e8734 100644 --- a/proto/v1/health/health.pb.gw.go +++ b/proto/v1/health/health.pb.gw.go @@ -70,6 +70,24 @@ func local_request_HealthService_CallApi_0(ctx context.Context, marshaler runtim } +func request_HealthService_CallDb_0(ctx context.Context, marshaler runtime.Marshaler, client HealthServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq emptypb.Empty + var metadata runtime.ServerMetadata + + msg, err := client.CallDb(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_HealthService_CallDb_0(ctx context.Context, marshaler runtime.Marshaler, server HealthServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq emptypb.Empty + var metadata runtime.ServerMetadata + + msg, err := server.CallDb(ctx, &protoReq) + return msg, metadata, err + +} + // RegisterHealthServiceHandlerServer registers the http handlers for service HealthService to "mux". // UnaryRPC :call HealthServiceServer directly. // StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. @@ -122,6 +140,29 @@ func RegisterHealthServiceHandlerServer(ctx context.Context, mux *runtime.ServeM }) + mux.Handle("GET", pattern_HealthService_CallDb_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + var stream runtime.ServerTransportStream + ctx = grpc.NewContextWithServerTransportStream(ctx, &stream) + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_HealthService_CallDb_0(rctx, inboundMarshaler, server, req, pathParams) + md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer()) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_HealthService_CallDb_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } @@ -203,6 +244,26 @@ func RegisterHealthServiceHandlerClient(ctx context.Context, mux *runtime.ServeM }) + mux.Handle("GET", pattern_HealthService_CallDb_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_HealthService_CallDb_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_HealthService_CallDb_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } @@ -210,10 +271,14 @@ var ( pattern_HealthService_Status_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"api", "v1", "health", "status"}, "", runtime.AssumeColonVerbOpt(true))) pattern_HealthService_CallApi_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"api", "v1", "health", "callapi"}, "", runtime.AssumeColonVerbOpt(true))) + + pattern_HealthService_CallDb_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"api", "v1", "health", "calldb"}, "", runtime.AssumeColonVerbOpt(true))) ) var ( forward_HealthService_Status_0 = runtime.ForwardResponseMessage forward_HealthService_CallApi_0 = runtime.ForwardResponseMessage + + forward_HealthService_CallDb_0 = runtime.ForwardResponseMessage ) diff --git a/proto/v1/health/health.proto b/proto/v1/health/health.proto index 7be373a..de949a3 100644 --- a/proto/v1/health/health.proto +++ b/proto/v1/health/health.proto @@ -35,4 +35,10 @@ service HealthService { get: "/api/v1/health/callapi" }; } + + rpc CallDb(google.protobuf.Empty) returns (Response) { + option (google.api.http) = { + get: "/api/v1/health/calldb" + }; + } } \ No newline at end of file