Skip to content
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

Merged
merged 4 commits into from
Apr 7, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 79 additions & 3 deletions examples/server/main.go
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

race condition?

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data race?

return a, nil
}
return nil, errors.New("not found")
Copy link
Member

Choose a reason for hiding this comment

The 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")
Copy link
Member

Choose a reason for hiding this comment

The 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")
Copy link
Member

Choose a reason for hiding this comment

The 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
}
Expand All @@ -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
}
Expand Down