update example doc

This commit is contained in:
kevin
2020-09-04 08:13:22 +08:00
parent b4aa89fc25
commit c0d0e00803
8 changed files with 67 additions and 21 deletions

View File

@@ -1,3 +1,8 @@
Name: shorturl-api
Host: 0.0.0.0
Port: 8888
Transform:
Etcd:
Hosts:
- localhost:2379
Key: transform.rpc

View File

@@ -1,7 +1,11 @@
package config
import "github.com/tal-tech/go-zero/rest"
import (
"github.com/tal-tech/go-zero/rest"
"github.com/tal-tech/go-zero/rpcx"
)
type Config struct {
rest.RestConf
Transform rpcx.RpcClientConf
}

View File

@@ -5,6 +5,7 @@ import (
"shorturl/api/internal/svc"
"shorturl/api/internal/types"
"shorturl/rpc/transform/transformer"
"github.com/tal-tech/go-zero/core/logx"
)
@@ -24,5 +25,14 @@ func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) ExpandLogic
}
func (l *ExpandLogic) Expand(req types.ExpandReq) (*types.ExpandResp, error) {
return &types.ExpandResp{}, nil
resp, err := l.svcCtx.Transformer.Expand(l.ctx, &transformer.ExpandReq{
Shorten: req.Shorten,
})
if err != nil {
return nil, err
}
return &types.ExpandResp{
Url: resp.Url,
}, nil
}

View File

@@ -5,6 +5,7 @@ import (
"shorturl/api/internal/svc"
"shorturl/api/internal/types"
"shorturl/rpc/transform/transformer"
"github.com/tal-tech/go-zero/core/logx"
)
@@ -24,5 +25,14 @@ func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) ShortenLog
}
func (l *ShortenLogic) Shorten(req types.ShortenReq) (*types.ShortenResp, error) {
return &types.ShortenResp{}, nil
resp, err := l.svcCtx.Transformer.Shorten(l.ctx, &transformer.ShortenReq{
Url: req.Url,
})
if err != nil {
return nil, err
}
return &types.ShortenResp{
Shorten: resp.Shorten,
}, nil
}

View File

@@ -1,11 +1,20 @@
package svc
import "shorturl/api/internal/config"
import (
"shorturl/api/internal/config"
"shorturl/rpc/transform/transformer"
"github.com/tal-tech/go-zero/rpcx"
)
type ServiceContext struct {
Config config.Config
Config config.Config
Transformer transformer.Transformer
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{Config: c}
return &ServiceContext{
Config: c,
Transformer: transformer.NewTransformer(rpcx.MustNewClient(c.Transform)),
}
}