Skip to content

Commit

Permalink
Add example & Update grpc readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kesonan committed Jul 9, 2022
1 parent e420eec commit 75d8b55
Show file tree
Hide file tree
Showing 29 changed files with 1,796 additions and 550 deletions.
17 changes: 17 additions & 0 deletions tools/goctl/example/rpc/hello.proto
@@ -0,0 +1,17 @@
syntax = "proto3";

package hello;

option go_package = "./hello";

message HelloReq {
string in = 1;
}

message HelloResp {
string msg = 1;
}

service Greet {
rpc SayHello(HelloReq) returns (HelloResp);
}
37 changes: 37 additions & 0 deletions tools/goctl/example/rpc/hello/client/greet/greet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tools/goctl/example/rpc/hello/etc/hello.yaml
@@ -0,0 +1,6 @@
Name: hello.rpc
ListenOn: 127.0.0.1:8080
Etcd:
Hosts:
- 127.0.0.1:2379
Key: hello.rpc
39 changes: 39 additions & 0 deletions tools/goctl/example/rpc/hello/hello.go
@@ -0,0 +1,39 @@
package main

import (
"flag"
"fmt"

"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/config"
greetServer "github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/server/greet"
"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/svc"
"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/pb/hello"

"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

var configFile = flag.String("f", "etc/hello.yaml", "the config file")

func main() {
flag.Parse()

var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)

s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
hello.RegisterGreetServer(grpcServer, greetServer.NewGreetServer(ctx))

if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
defer s.Stop()

fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
s.Start()
}
7 changes: 7 additions & 0 deletions tools/goctl/example/rpc/hello/internal/config/config.go
@@ -0,0 +1,7 @@
package config

import "github.com/zeromicro/go-zero/zrpc"

type Config struct {
zrpc.RpcServerConf
}
@@ -0,0 +1,30 @@
package greetlogic

import (
"context"

"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/svc"
"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/pb/hello"

"github.com/zeromicro/go-zero/core/logx"
)

type SayHelloLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}

func NewSayHelloLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SayHelloLogic {
return &SayHelloLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}

func (l *SayHelloLogic) SayHello(in *hello.HelloReq) (*hello.HelloResp, error) {
// todo: add your logic here and delete this line

return &hello.HelloResp{}, nil
}
28 changes: 28 additions & 0 deletions tools/goctl/example/rpc/hello/internal/server/greet/greetserver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tools/goctl/example/rpc/hello/internal/svc/servicecontext.go
@@ -0,0 +1,13 @@
package svc

import "github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/config"

type ServiceContext struct {
Config config.Config
}

func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
}
}
208 changes: 208 additions & 0 deletions tools/goctl/example/rpc/hello/pb/hello/hello.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 75d8b55

Please sign in to comment.