-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from jerry-enebeli/feature/service
Feature/service
- Loading branch information
Showing
12 changed files
with
596 additions
and
572 deletions.
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
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,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) | ||
} |
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,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) | ||
} | ||
|
||
} |
Oops, something went wrong.