init
This commit is contained in:
46
cmd/gateway/main.go
Normal file
46
cmd/gateway/main.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"net/http"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
gw "github.com/esinio/geco/gen/service/echo/v1" // Update
|
||||
)
|
||||
|
||||
var (
|
||||
// command-line options:
|
||||
// gRPC server endpoint
|
||||
grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:9090", "gRPC server endpoint")
|
||||
)
|
||||
|
||||
func run() error {
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
// Register gRPC server endpoint
|
||||
// Note: Make sure the gRPC server is running properly and accessible
|
||||
mux := runtime.NewServeMux()
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
err := gw.RegisterEchoServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Start HTTP server (and proxy calls to gRPC server endpoint)
|
||||
return http.ListenAndServe(":8081", mux)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
defer glog.Flush()
|
||||
|
||||
if err := run(); err != nil {
|
||||
glog.Fatal(err)
|
||||
}
|
||||
}
|
||||
47
cmd/server/main.go
Normal file
47
cmd/server/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// grpc server
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
pb "github.com/esinio/geco/gen/service/echo/v1"
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := grpc.NewServer()
|
||||
pb.RegisterEchoServiceServer(s, &service{})
|
||||
lis, err := net.Listen("tcp", ":9090")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer lis.Close()
|
||||
|
||||
defer s.GracefulStop()
|
||||
|
||||
if err := s.Serve(lis); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// UnimplementedEchoServiceServer must be embedded to have forward compatible implementations.
|
||||
type service struct {
|
||||
pb.UnimplementedEchoServiceServer
|
||||
}
|
||||
|
||||
func (s *service) Echo(ctx context.Context, req *pb.StringMessage) (*pb.StringMessage, error) {
|
||||
if req.Value == "" {
|
||||
return nil, status.Errorf(codes.Unimplemented, "value is empty")
|
||||
}
|
||||
return &pb.StringMessage{
|
||||
Value: req.Value,
|
||||
Timestamp: ptypes.TimestampNow(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// func (s *service) mustEmbedUnimplementedEchoServiceServer() {}
|
||||
Reference in New Issue
Block a user