Skip to content

Commit

Permalink
Merge pull request #65 from rogerogers/feat/ch-07
Browse files Browse the repository at this point in the history
feat(gomall): update ch07
  • Loading branch information
baiyutang authored Apr 28, 2024
2 parents 3ab84a5 + 0c47c7f commit 978cf5a
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 173 deletions.
2 changes: 1 addition & 1 deletion gomall/tutorial/ch07/demo/demo_proto/biz/dal/mysql/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func Init() {
if err != nil {
panic(err)
}
DB.AutoMigrate(model.User{})
DB.AutoMigrate(model.User{}) //nolint: errcheck
fmt.Printf("%#v", DB.Debug().Exec("select version()"))
}
6 changes: 2 additions & 4 deletions gomall/tutorial/ch07/demo/demo_proto/biz/dal/redis/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ package redis
import (
"context"

"github.com/redis/go-redis/v9"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/conf"
"github.com/redis/go-redis/v9"
)

var (
RedisClient *redis.Client
)
var RedisClient *redis.Client

func Init() {
RedisClient = redis.NewClient(&redis.Options{
Expand Down
9 changes: 8 additions & 1 deletion gomall/tutorial/ch07/demo/demo_proto/biz/service/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package service

import (
"context"
"fmt"

"github.com/bytedance/gopkg/cloud/metainfo"
pbapi "github.com/cloudwego/biz-demo/gomall/demo/demo_proto/kitex_gen/pbapi"
"github.com/cloudwego/kitex/pkg/kerrors"
)

type EchoService struct {
Expand All @@ -29,6 +32,10 @@ func NewEchoService(ctx context.Context) *EchoService {

// Run create note info
func (s *EchoService) Run(req *pbapi.Request) (resp *pbapi.Response, err error) {

clientName, ok := metainfo.GetPersistentValue(s.ctx, "CLIENT_NAME")
fmt.Println(clientName, ok)
if req.Message == "error" {
return nil, kerrors.NewGRPCBizStatusError(1004001, "client param error")
}
return &pbapi.Response{Message: req.Message}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package service
import (
"context"
"testing"

pbapi "github.com/cloudwego/biz-demo/gomall/demo/demo_proto/kitex_gen/pbapi"
)

Expand All @@ -31,5 +32,4 @@ func TestEcho_Run(t *testing.T) {
t.Logf("resp: %v", resp)

// todo: edit your unit test

}
15 changes: 12 additions & 3 deletions gomall/tutorial/ch07/demo/demo_proto/cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ package main

import (
"context"
"errors"
"fmt"

"github.com/bytedance/gopkg/cloud/metainfo"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/conf"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/kitex_gen/pbapi"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/kitex_gen/pbapi/echo"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/middleware"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/transport"
consul "github.com/kitex-contrib/registry-consul"
Expand All @@ -37,13 +40,19 @@ func main() {
c, err := echo.NewClient("demo_proto", client.WithResolver(r),
client.WithTransportProtocol(transport.GRPC),
client.WithMetaHandler(transmeta.ClientHTTP2Handler),
client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "demo_proto_client"}),
client.WithMiddleware(middleware.Middleware),
)
if err != nil {
panic(err)
}
res, err := c.Echo(context.Background(), &pbapi.Request{Message: "hello"})
ctx := metainfo.WithPersistentValue(context.Background(), "CLIENT_NAME", "demo_proto_client")
res, err := c.Echo(ctx, &pbapi.Request{Message: "error"})
var bizErr *kerrors.GRPCBizStatusError
if err != nil {
ok := errors.As(err, &bizErr)
if ok {
fmt.Printf("%#v", bizErr)
}
klog.Fatal(err)
}
fmt.Printf("%v", res)
Expand Down
2 changes: 1 addition & 1 deletion gomall/tutorial/ch07/demo/demo_proto/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

require (
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/kitex v0.8.0
github.com/joho/godotenv v1.5.1
Expand All @@ -24,7 +25,6 @@ require (
require (
github.com/apache/thrift v0.13.0 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
Expand Down
13 changes: 6 additions & 7 deletions gomall/tutorial/ch07/demo/demo_proto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/biz/dal"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/conf"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/kitex_gen/pbapi/echo"
"github.com/cloudwego/biz-demo/gomall/demo/demo_proto/middleware"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/server"
"github.com/joho/godotenv"
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
Expand Down Expand Up @@ -56,13 +56,12 @@ func kitexInit() (opts []server.Option) {
if err != nil {
panic(err)
}
opts = append(opts, server.WithServiceAddr(addr))
opts = append(opts, server.WithServiceAddr(addr), server.WithMiddleware(middleware.Middleware))

// service info
opts = append(opts,
server.WithMetaHandler(transmeta.ServerHTTP2Handler),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: conf.GetConf().Kitex.Service}),
)
opts = append(opts, server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: conf.GetConf().Kitex.Service,
}))

r, err := consul.NewConsulRegister(conf.GetConf().Registry.RegistryAddress[0])
if err != nil {
Expand All @@ -85,7 +84,7 @@ func kitexInit() (opts []server.Option) {
}
klog.SetOutput(asyncWriter)
server.RegisterShutdownHook(func() {
asyncWriter.Sync()
_ = asyncWriter.Sync()
})
return
}
32 changes: 32 additions & 0 deletions gomall/tutorial/ch07/demo/demo_proto/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package middleware

import (
"context"
"fmt"
"time"

"github.com/cloudwego/kitex/pkg/endpoint"
)

func Middleware(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, resp interface{}) (err error) {
begin := time.Now()
err = next(ctx, req, resp)
fmt.Println(time.Since(begin))
return err
}
}
4 changes: 1 addition & 3 deletions gomall/tutorial/ch07/demo/demo_thrift/biz/dal/redis/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (
"github.com/redis/go-redis/v9"
)

var (
RedisClient *redis.Client
)
var RedisClient *redis.Client

func Init() {
RedisClient = redis.NewClient(&redis.Options{
Expand Down
4 changes: 4 additions & 0 deletions gomall/tutorial/ch07/demo/demo_thrift/biz/service/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package service

import (
"context"
"fmt"

api "github.com/cloudwego/biz-demo/gomall/demo/demo_thrift/kitex_gen/api"
"github.com/cloudwego/kitex/pkg/rpcinfo"
)

type EchoService struct {
Expand All @@ -30,6 +32,8 @@ func NewEchoService(ctx context.Context) *EchoService {
// Run create note info
func (s *EchoService) Run(req *api.Request) (resp *api.Response, err error) {
// Finish your business logic.
info := rpcinfo.GetRPCInfo(s.ctx)
fmt.Println(info.From().ServiceName())

return &api.Response{Message: req.Message}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package service

import (
"context"
api "github.com/cloudwego/biz-demo/gomall/demo/demo_thrift/kitex_gen/api"
"testing"

api "github.com/cloudwego/biz-demo/gomall/demo/demo_thrift/kitex_gen/api"
)

func TestEcho_Run(t *testing.T) {
Expand All @@ -31,5 +32,4 @@ func TestEcho_Run(t *testing.T) {
t.Logf("resp: %v", resp)

// todo: edit your unit test

}
22 changes: 10 additions & 12 deletions gomall/tutorial/ch07/demo/demo_thrift/cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,31 @@ import (
"context"
"fmt"

"github.com/cloudwego/biz-demo/gomall/demo/demo_thrift/conf"
"github.com/cloudwego/biz-demo/gomall/demo/demo_thrift/kitex_gen/api"
"github.com/cloudwego/biz-demo/gomall/demo/demo_thrift/kitex_gen/api/echo"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/transport"
consul "github.com/kitex-contrib/registry-consul"
)

func main() {
r, err := consul.NewConsulResolver(conf.GetConf().Registry.RegistryAddress[0])
if err != nil {
panic(err)
}
c, err := echo.NewClient("demo_thrift", client.WithResolver(r),
client.WithTransportProtocol(transport.TTHeader),
cli, err := echo.NewClient("demo_thrift", client.WithHostPorts("localhost:8888"),
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "demo_thrift_client"}),
client.WithTransportProtocol(transport.TTHeader),
client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: "demo_thrift_client",
}),
)
if err != nil {
panic(err)
}
res, err := c.Echo(context.Background(), &api.Request{Message: "hello"})

res, err := cli.Echo(context.Background(), &api.Request{
Message: "hello",
})
if err != nil {
klog.Fatal(err)
fmt.Println(err)
}
fmt.Printf("%v", res)
}
3 changes: 1 addition & 2 deletions gomall/tutorial/ch07/demo/demo_thrift/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package conf

import (
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -78,7 +77,7 @@ func GetConf() *Config {
func initConf() {
prefix := "conf"
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
content, err := ioutil.ReadFile(confFileRelPath)
content, err := os.ReadFile(confFileRelPath)
if err != nil {
panic(err)
}
Expand Down
14 changes: 0 additions & 14 deletions gomall/tutorial/ch07/demo/demo_thrift/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/apache/thrift v0.13.0
github.com/cloudwego/kitex v0.8.0
github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20231211030816-1f9e0f7bcee3
github.com/kitex-contrib/registry-consul v0.0.0-20230406075225-7d341f036654
github.com/kr/pretty v0.3.1
github.com/redis/go-redis/v9 v9.3.1
go.uber.org/zap v1.26.0
Expand All @@ -20,7 +19,6 @@ require (
)

require (
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand All @@ -36,29 +34,17 @@ require (
github.com/cloudwego/thriftgo v0.3.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect
github.com/hashicorp/consul/api v1.20.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-hclog v1.3.1 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
Loading

0 comments on commit 978cf5a

Please sign in to comment.