2020-01-22 13:02:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-17 23:39:15 +00:00
|
|
|
"crypto/tls"
|
2020-11-27 13:00:45 +00:00
|
|
|
"errors"
|
2020-01-22 13:02:06 +00:00
|
|
|
"fmt"
|
2020-11-27 13:00:45 +00:00
|
|
|
"io"
|
2021-09-17 23:39:15 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2021-10-13 15:05:59 +00:00
|
|
|
"time"
|
2020-01-22 13:02:06 +00:00
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-23 14:15:47 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2021-07-17 20:40:06 +00:00
|
|
|
"go.linka.cloud/grpc/client"
|
2021-11-21 15:13:43 +00:00
|
|
|
"go.linka.cloud/grpc/interceptors/defaulter"
|
2021-11-21 13:58:49 +00:00
|
|
|
metrics2 "go.linka.cloud/grpc/interceptors/metrics"
|
|
|
|
validation2 "go.linka.cloud/grpc/interceptors/validation"
|
2021-10-13 15:05:59 +00:00
|
|
|
"go.linka.cloud/grpc/logger"
|
2021-07-17 20:40:06 +00:00
|
|
|
"go.linka.cloud/grpc/service"
|
2020-01-22 13:02:06 +00:00
|
|
|
)
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
type GreeterHandler struct {
|
2020-11-27 13:00:45 +00:00
|
|
|
UnimplementedGreeterServer
|
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
|
|
|
|
func hello(name string) string {
|
|
|
|
return fmt.Sprintf("Hello %s !", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GreeterHandler) SayHello(ctx context.Context, req *HelloRequest) (*HelloReply, error) {
|
|
|
|
return &HelloReply{Message: hello(req.Name)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GreeterHandler) SayHelloStream(req *HelloStreamRequest, s Greeter_SayHelloStreamServer) error {
|
2021-11-21 13:58:49 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
for i := int64(0); i < req.Count; i++ {
|
2020-11-27 13:00:45 +00:00
|
|
|
if err := s.Send(&HelloReply{Message: fmt.Sprintf("Hello %s (%d)!", req.Name, i+1)}); err != nil {
|
2020-01-22 13:02:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
// time.Sleep(time.Second)
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:05:59 +00:00
|
|
|
func httpLogger(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
start := time.Now()
|
|
|
|
log := logger.From(request.Context()).WithFields(
|
|
|
|
"method", request.Method,
|
|
|
|
"host", request.Host,
|
|
|
|
"path", request.URL.Path,
|
|
|
|
"remoteAddress", request.RemoteAddr,
|
|
|
|
)
|
|
|
|
next.ServeHTTP(writer, request)
|
|
|
|
log.WithField("duration", time.Since(start)).Info()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func main() {
|
2020-11-08 18:28:33 +00:00
|
|
|
name := "greeter"
|
2021-11-22 13:04:47 +00:00
|
|
|
version := "v0.0.1"
|
2021-09-17 23:39:15 +00:00
|
|
|
secure := true
|
2020-11-08 18:28:33 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
done := make(chan struct{})
|
|
|
|
ready := make(chan struct{})
|
2020-01-22 13:02:06 +00:00
|
|
|
defer cancel()
|
2020-01-22 20:20:18 +00:00
|
|
|
var svc service.Service
|
2020-01-22 13:02:06 +00:00
|
|
|
var err error
|
2021-11-21 13:58:49 +00:00
|
|
|
metrics := metrics2.NewInterceptors()
|
|
|
|
validation := validation2.NewInterceptors(true)
|
2021-11-21 15:13:43 +00:00
|
|
|
defaulter := defaulter.NewInterceptors()
|
2021-09-17 23:39:15 +00:00
|
|
|
address := "0.0.0.0:9991"
|
2020-01-22 20:20:18 +00:00
|
|
|
svc, err = service.New(
|
|
|
|
service.WithContext(ctx),
|
2020-11-08 18:28:33 +00:00
|
|
|
service.WithName(name),
|
2021-11-22 13:04:47 +00:00
|
|
|
service.WithVersion(version),
|
2021-09-17 23:39:15 +00:00
|
|
|
service.WithAddress(address),
|
2021-11-22 13:04:47 +00:00
|
|
|
// service.WithRegistry(mdns.NewRegistry()),
|
2020-08-07 08:59:27 +00:00
|
|
|
service.WithReflection(true),
|
2021-09-17 23:39:15 +00:00
|
|
|
service.WithSecure(secure),
|
2020-01-22 20:20:18 +00:00
|
|
|
service.WithAfterStart(func() error {
|
2020-01-22 13:02:06 +00:00
|
|
|
fmt.Println("Server listening on", svc.Options().Address())
|
2020-11-08 18:28:33 +00:00
|
|
|
close(ready)
|
2020-01-22 13:02:06 +00:00
|
|
|
return nil
|
|
|
|
}),
|
2020-01-22 20:20:18 +00:00
|
|
|
service.WithAfterStop(func() error {
|
2020-01-22 13:02:06 +00:00
|
|
|
fmt.Println("Stopping server")
|
2020-11-08 18:28:33 +00:00
|
|
|
close(done)
|
2020-01-22 13:02:06 +00:00
|
|
|
return nil
|
|
|
|
}),
|
2021-09-17 23:39:15 +00:00
|
|
|
service.WithGateway(RegisterGreeterHandler),
|
|
|
|
service.WithGatewayPrefix("/rest"),
|
|
|
|
service.WithGRPCWeb(true),
|
|
|
|
service.WithGRPCWebPrefix("/grpc"),
|
2021-10-13 15:05:59 +00:00
|
|
|
service.WithMiddlewares(httpLogger),
|
2021-11-21 15:13:43 +00:00
|
|
|
service.WithInterceptors(metrics, defaulter, validation),
|
2020-01-22 13:02:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-27 13:00:45 +00:00
|
|
|
RegisterGreeterServer(svc, &GreeterHandler{})
|
2020-11-08 18:28:33 +00:00
|
|
|
go func() {
|
|
|
|
if err := svc.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-ready
|
|
|
|
s, err := client.New(
|
2021-11-22 13:04:47 +00:00
|
|
|
// client.WithName(name),
|
|
|
|
// client.WithVersion(version),
|
|
|
|
client.WithAddress("localhost:9991"),
|
|
|
|
// client.WithRegistry(mdns.NewRegistry()),
|
2021-09-17 23:39:15 +00:00
|
|
|
client.WithSecure(secure),
|
2021-11-23 14:15:47 +00:00
|
|
|
client.WithUnaryInterceptors(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
|
|
logger.From(ctx).WithFields("party", "client", "method", method).Info(req)
|
|
|
|
return invoker(ctx, method, req, reply, cc, opts...)
|
|
|
|
}),
|
2020-11-08 18:28:33 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2020-11-27 13:00:45 +00:00
|
|
|
g := NewGreeterClient(s)
|
2020-11-08 18:28:33 +00:00
|
|
|
defer cancel()
|
|
|
|
res, err := g.SayHello(context.Background(), &HelloRequest{Name: "test"})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
2020-11-08 18:28:33 +00:00
|
|
|
logrus.Infof("received message: %s", res.Message)
|
2021-11-21 13:58:49 +00:00
|
|
|
res, err = g.SayHello(context.Background(), &HelloRequest{})
|
|
|
|
if err == nil {
|
|
|
|
logrus.Fatal("expected validation error")
|
|
|
|
}
|
2021-11-21 15:13:43 +00:00
|
|
|
stream, err := g.SayHelloStream(context.Background(), &HelloStreamRequest{Name: "test"})
|
2020-11-27 13:00:45 +00:00
|
|
|
if err != nil {
|
2021-09-17 23:39:15 +00:00
|
|
|
logrus.Fatal(err)
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
m, err := stream.Recv()
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-09-17 23:39:15 +00:00
|
|
|
logrus.Fatal(err)
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
logrus.Infof("received stream message: %s", m.Message)
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
scheme := "http://"
|
|
|
|
if secure {
|
|
|
|
scheme = "https://"
|
|
|
|
}
|
|
|
|
httpc := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req := `{"name":"test"}`
|
|
|
|
|
|
|
|
do := func(url, contentType string) {
|
|
|
|
resp, err := httpc.Post(url, contentType, strings.NewReader(req))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
logrus.Info(string(b))
|
|
|
|
}
|
2021-10-13 15:05:59 +00:00
|
|
|
do(scheme+address+"/rest/api/v1/greeter/hello", "application/json")
|
2021-09-17 23:39:15 +00:00
|
|
|
do(scheme+address+"/grpc/helloworld.Greeter/SayHello", "application/grpc-web+json")
|
2020-11-08 18:28:33 +00:00
|
|
|
cancel()
|
|
|
|
<-done
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|