Skip to content

Commit

Permalink
Merge pull request #13 from jerry-enebeli/feature/service
Browse files Browse the repository at this point in the history
Feature/service
  • Loading branch information
jerry-enebeli authored Sep 21, 2020
2 parents 5c15ab3 + c285928 commit 4a9aee3
Show file tree
Hide file tree
Showing 12 changed files with 596 additions and 572 deletions.
29 changes: 23 additions & 6 deletions cmd/gateway/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package gateway

import (
"fmt"
"log"
"os"

"github.com/jerry-enebeli/grpc-rest-gateway/pkg/service"
"github.com/jerry-enebeli/grpc-rest-gateway/tools"
"github.com/spf13/cobra"
"log"
"os"
)

var sourceProtoFile string
var sourceJsonFile string

var s = service.NewService()

Expand All @@ -19,7 +21,6 @@ var rootCmd = &cobra.Command{
Long: `Gateway is a api gateway for gRPC application. gateway maps RESTFUL API to gRPC services.Complete documentation is available at https://github.com/jerry-enebeli/grpc-rest-gateway`,
Run: func(cmd *cobra.Command, args []string) {
output, _ := tools.Shell("bash", "gateway --help")

log.Println(output)
},
}
Expand All @@ -28,7 +29,6 @@ var serviceCmd = &cobra.Command{
Use: "service",
Short: "manages gRPC services",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("got this", args)
},
}

Expand All @@ -50,6 +50,18 @@ var serviceListMethodsCmd = &cobra.Command{
},
}

var serviceRunCmd = &cobra.Command{
Use: "run",
Short: "Runs the gRPC service",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("service name required. gateway service run [service name]")
return
}
s.Run("", args[0], sourceJsonFile)
},
}

var serviceCreateCmd = &cobra.Command{
Use: "create",
Short: "create new gRPC services",
Expand All @@ -59,12 +71,17 @@ var serviceCreateCmd = &cobra.Command{
}

func addServiceCreateFlags() {
serviceCreateCmd.Flags().StringVarP(&sourceProtoFile, "source", "s", "", "Source directory to read proto file from")
serviceCreateCmd.Flags().StringVarP(&sourceProtoFile, "source", "s", "", "Source directory to read the proto file from")
}

func addRunServiceFlag() {
serviceRunCmd.Flags().StringVarP(&sourceJsonFile, "source", "s", "", "Source directory to read the rest to rpc mapper json file from")
}

func Execute() {
addServiceCreateFlags()
serviceCmd.AddCommand(serviceCreateCmd, serviceListCmd, serviceListMethodsCmd)
addRunServiceFlag()
serviceCmd.AddCommand(serviceCreateCmd, serviceListCmd, serviceListMethodsCmd, serviceRunCmd)
rootCmd.AddCommand(serviceCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
48 changes: 48 additions & 0 deletions codec/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package codec

import (
"bytes"
"encoding/json"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/encoding"
)

func init() {
encoding.RegisterCodec(JSON{
Marshaler: jsonpb.Marshaler{
EmitDefaults: true,
OrigName: true,
},
})
}

type JSON struct {
jsonpb.Marshaler
jsonpb.Unmarshaler
}

func (_ JSON) Name() string {
return "json"
}

func (j JSON) Marshal(v interface{}) (out []byte, err error) {
if pm, ok := v.(proto.Message); ok {
b := new(bytes.Buffer)
err := j.Marshaler.Marshal(b, pm)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
return json.Marshal(v)
}

func (j JSON) Unmarshal(data []byte, v interface{}) (err error) {
if pm, ok := v.(proto.Message); ok {
b := bytes.NewBuffer(data)
return j.Unmarshaler.Unmarshal(b, pm)
}
return json.Unmarshal(data, v)
}
47 changes: 47 additions & 0 deletions examples/greeter_server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto

// Package main implements a server for Greeter service.
package main

import (
"context"
"fmt"
"log"
"net"

_ "github.com/jerry-enebeli/grpc-rest-gateway/codec"

pb "github.com/jerry-enebeli/grpc-rest-gateway/examples/helloworld"
"google.golang.org/grpc"
)

const (
port = ":50051"
)

// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

fmt.Println("server started at " + port)
s := grpc.NewServer()

pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}

}
Loading

0 comments on commit 4a9aee3

Please sign in to comment.