-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
implement ABitOfEverythingService #2
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"net" | ||
|
||
examples "github.com/gengo/grpc-gateway/examples" | ||
sub "github.com/gengo/grpc-gateway/examples/sub" | ||
"github.com/golang/glog" | ||
"github.com/rogpeppe/fastuuid" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// Implements of EchoServiceServer | ||
|
||
type echoServer struct{} | ||
|
||
func (s echoServer) Echo(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { | ||
func newEchoServer() examples.EchoServiceServer { | ||
return new(echoServer) | ||
} | ||
|
||
func (s *echoServer) Echo(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { | ||
glog.Info(msg) | ||
return msg, nil | ||
} | ||
|
||
func (s *echoServer) EchoBody(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { | ||
glog.Info(msg) | ||
return msg, nil | ||
} | ||
|
||
func (s echoServer) EchoBody(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { | ||
// Implements of ABitOfEverythingServiceServer | ||
|
||
var uuidgen = fastuuid.MustNewGenerator() | ||
|
||
type _ABitOfEverythingServer struct { | ||
m map[string]*examples.ABitOfEverything | ||
} | ||
|
||
func newABitOfEverythingServer() examples.ABitOfEverythingServiceServer { | ||
return &_ABitOfEverythingServer{ | ||
m: make(map[string]*examples.ABitOfEverything), | ||
} | ||
} | ||
|
||
func (s *_ABitOfEverythingServer) Create(ctx context.Context, msg *examples.ABitOfEverything) (*examples.ABitOfEverything, error) { | ||
glog.Info(msg) | ||
var uuid string | ||
for { | ||
uuid = fmt.Sprintf("%x", uuidgen.Next()) | ||
if _, ok := s.m[uuid]; !ok { | ||
break | ||
} | ||
} | ||
s.m[uuid] = msg | ||
s.m[uuid].Uuid = uuid | ||
return s.m[uuid], nil | ||
} | ||
|
||
func (s *_ABitOfEverythingServer) CreateBody(ctx context.Context, msg *examples.ABitOfEverything) (*examples.ABitOfEverything, error) { | ||
return s.Create(ctx, msg) | ||
} | ||
|
||
func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *examples.IdMessage) (*examples.ABitOfEverything, error) { | ||
glog.Info(msg) | ||
if a, ok := s.m[msg.Uuid]; ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. data race? |
||
return a, nil | ||
} | ||
return nil, errors.New("not found") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use grpc.Errorf(codes.NotFound, "not found"). |
||
} | ||
|
||
func (s *_ABitOfEverythingServer) Update(ctx context.Context, msg *examples.ABitOfEverything) (*examples.EmptyMessage, error) { | ||
glog.Info(msg) | ||
if _, ok := s.m[msg.Uuid]; ok { | ||
s.m[msg.Uuid] = msg | ||
} else { | ||
return nil, errors.New("not found") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
return new(examples.EmptyMessage), nil | ||
} | ||
|
||
func (s *_ABitOfEverythingServer) Delete(ctx context.Context, msg *examples.IdMessage) (*examples.EmptyMessage, error) { | ||
glog.Info(msg) | ||
if _, ok := s.m[msg.Uuid]; ok { | ||
delete(s.m, msg.Uuid) | ||
} else { | ||
return nil, errors.New("not found") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
} | ||
return new(examples.EmptyMessage), nil | ||
} | ||
|
||
func (s *_ABitOfEverythingServer) Echo(ctx context.Context, msg *sub.StringMessage) (*sub.StringMessage, error) { | ||
glog.Info(msg) | ||
return msg, nil | ||
} | ||
|
@@ -32,7 +107,8 @@ func run() error { | |
return err | ||
} | ||
s := grpc.NewServer() | ||
examples.RegisterEchoServiceServer(s, echoServer{}) | ||
examples.RegisterEchoServiceServer(s, newEchoServer()) | ||
examples.RegisterABitOfEverythingServiceServer(s, newABitOfEverythingServer()) | ||
s.Serve(l) | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
race condition?