add shorturl example code
This commit is contained in:
12
example/shorturl/rpc/transform/etc/transform.yaml
Executable file
12
example/shorturl/rpc/transform/etc/transform.yaml
Executable file
@@ -0,0 +1,12 @@
|
||||
Name: transform.rpc
|
||||
Log:
|
||||
Mode: console
|
||||
ListenOn: 127.0.0.1:8081
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: transform.rpc
|
||||
DataSource: root:@tcp(localhost:3306)/gozero
|
||||
Table: shorturl
|
||||
Cache:
|
||||
- Host: localhost:6379
|
||||
13
example/shorturl/rpc/transform/internal/config/config.go
Executable file
13
example/shorturl/rpc/transform/internal/config/config.go
Executable file
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/rpcx"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rpcx.RpcServerConf
|
||||
DataSource string
|
||||
Table string
|
||||
Cache cache.CacheConf
|
||||
}
|
||||
35
example/shorturl/rpc/transform/internal/logic/expandlogic.go
Executable file
35
example/shorturl/rpc/transform/internal/logic/expandlogic.go
Executable file
@@ -0,0 +1,35 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"shorturl/rpc/transform/internal/svc"
|
||||
transform "shorturl/rpc/transform/pb"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ExpandLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExpandLogic {
|
||||
return &ExpandLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ExpandLogic) Expand(in *transform.ExpandReq) (*transform.ExpandResp, error) {
|
||||
res, err := l.svcCtx.Model.FindOne(in.Shorten)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &transform.ExpandResp{
|
||||
Url: res.Url,
|
||||
}, nil
|
||||
}
|
||||
41
example/shorturl/rpc/transform/internal/logic/shortenlogic.go
Executable file
41
example/shorturl/rpc/transform/internal/logic/shortenlogic.go
Executable file
@@ -0,0 +1,41 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"shorturl/rpc/transform/internal/svc"
|
||||
"shorturl/rpc/transform/model"
|
||||
transform "shorturl/rpc/transform/pb"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/hash"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ShortenLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShortenLogic {
|
||||
return &ShortenLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ShortenLogic) Shorten(in *transform.ShortenReq) (*transform.ShortenResp, error) {
|
||||
key := hash.Md5Hex([]byte(in.Url))[:6]
|
||||
_, err := l.svcCtx.Model.Insert(model.Shorturl{
|
||||
Shorten: key,
|
||||
Url: in.Url,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &transform.ShortenResp{
|
||||
Shorten: key,
|
||||
}, nil
|
||||
}
|
||||
32
example/shorturl/rpc/transform/internal/server/transformerserver.go
Executable file
32
example/shorturl/rpc/transform/internal/server/transformerserver.go
Executable file
@@ -0,0 +1,32 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: transform.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"shorturl/rpc/transform/internal/logic"
|
||||
"shorturl/rpc/transform/internal/svc"
|
||||
transform "shorturl/rpc/transform/pb"
|
||||
)
|
||||
|
||||
type TransformerServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewTransformerServer(svcCtx *svc.ServiceContext) *TransformerServer {
|
||||
return &TransformerServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TransformerServer) Expand(ctx context.Context, in *transform.ExpandReq) (*transform.ExpandResp, error) {
|
||||
l := logic.NewExpandLogic(ctx, s.svcCtx)
|
||||
return l.Expand(in)
|
||||
}
|
||||
|
||||
func (s *TransformerServer) Shorten(ctx context.Context, in *transform.ShortenReq) (*transform.ShortenResp, error) {
|
||||
l := logic.NewShortenLogic(ctx, s.svcCtx)
|
||||
return l.Shorten(in)
|
||||
}
|
||||
20
example/shorturl/rpc/transform/internal/svc/servicecontext.go
Executable file
20
example/shorturl/rpc/transform/internal/svc/servicecontext.go
Executable file
@@ -0,0 +1,20 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"shorturl/rpc/transform/internal/config"
|
||||
"shorturl/rpc/transform/model"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
c config.Config
|
||||
Model *model.ShorturlModel
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
c: c,
|
||||
Model: model.NewShorturlModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table),
|
||||
}
|
||||
}
|
||||
6
example/shorturl/rpc/transform/model/shorturl.sql
Normal file
6
example/shorturl/rpc/transform/model/shorturl.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE `shorturl`
|
||||
(
|
||||
`shorten` varchar(255) NOT NULL COMMENT 'shorten key',
|
||||
`url` varchar(255) NOT NULL COMMENT 'original url',
|
||||
PRIMARY KEY(`shorten`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
86
example/shorturl/rpc/transform/model/shorturlmodel.go
Executable file
86
example/shorturl/rpc/transform/model/shorturlmodel.go
Executable file
@@ -0,0 +1,86 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
shorturlFieldNames = builderx.FieldNames(&Shorturl{})
|
||||
shorturlRows = strings.Join(shorturlFieldNames, ",")
|
||||
shorturlRowsExpectAutoSet = strings.Join(stringx.Remove(shorturlFieldNames, "create_time", "update_time"), ",")
|
||||
shorturlRowsWithPlaceHolder = strings.Join(stringx.Remove(shorturlFieldNames, "shorten", "create_time", "update_time"), "=?,") + "=?"
|
||||
|
||||
cacheShorturlShortenPrefix = "cache#Shorturl#shorten#"
|
||||
)
|
||||
|
||||
type (
|
||||
ShorturlModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
Shorturl struct {
|
||||
Shorten string `db:"shorten"` // shorten key
|
||||
Url string `db:"url"` // original url
|
||||
}
|
||||
)
|
||||
|
||||
func NewShorturlModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *ShorturlModel {
|
||||
return &ShorturlModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ShorturlModel) Insert(data Shorturl) (sql.Result, error) {
|
||||
query := `insert into ` + m.table + ` (` + shorturlRowsExpectAutoSet + `) values (?, ?)`
|
||||
return m.ExecNoCache(query, data.Shorten, data.Url)
|
||||
}
|
||||
|
||||
func (m *ShorturlModel) FindOne(shorten string) (*Shorturl, error) {
|
||||
shorturlShortenKey := fmt.Sprintf("%s%v", cacheShorturlShortenPrefix, shorten)
|
||||
var resp Shorturl
|
||||
err := m.QueryRow(&resp, shorturlShortenKey, func(conn sqlx.SqlConn, v interface{}) error {
|
||||
query := `select ` + shorturlRows + ` from ` + m.table + ` where shorten = ? limit 1`
|
||||
return conn.QueryRow(v, query, shorten)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ShorturlModel) Update(data Shorturl) error {
|
||||
shorturlShortenKey := fmt.Sprintf("%s%v", cacheShorturlShortenPrefix, data.Shorten)
|
||||
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `update ` + m.table + ` set ` + shorturlRowsWithPlaceHolder + ` where shorten = ?`
|
||||
return conn.Exec(query, data.Url, data.Shorten)
|
||||
}, shorturlShortenKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *ShorturlModel) Delete(shorten string) error {
|
||||
_, err := m.FindOne(shorten)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
shorturlShortenKey := fmt.Sprintf("%s%v", cacheShorturlShortenPrefix, shorten)
|
||||
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := `delete from ` + m.table + ` where shorten = ?`
|
||||
return conn.Exec(query, shorten)
|
||||
}, shorturlShortenKey)
|
||||
return err
|
||||
}
|
||||
5
example/shorturl/rpc/transform/model/vars.go
Executable file
5
example/shorturl/rpc/transform/model/vars.go
Executable file
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
230
example/shorturl/rpc/transform/pb/transform.pb.go
Normal file
230
example/shorturl/rpc/transform/pb/transform.pb.go
Normal file
@@ -0,0 +1,230 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: transform.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package transform is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
transform.proto
|
||||
|
||||
It has these top-level messages:
|
||||
ExpandReq
|
||||
ExpandResp
|
||||
ShortenReq
|
||||
ShortenResp
|
||||
*/
|
||||
package transform
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type ExpandReq struct {
|
||||
Shorten string `protobuf:"bytes,1,opt,name=shorten" json:"shorten,omitempty"`
|
||||
}
|
||||
|
||||
func (m *ExpandReq) Reset() { *m = ExpandReq{} }
|
||||
func (m *ExpandReq) String() string { return proto.CompactTextString(m) }
|
||||
func (*ExpandReq) ProtoMessage() {}
|
||||
func (*ExpandReq) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *ExpandReq) GetShorten() string {
|
||||
if m != nil {
|
||||
return m.Shorten
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type ExpandResp struct {
|
||||
Url string `protobuf:"bytes,1,opt,name=url" json:"url,omitempty"`
|
||||
}
|
||||
|
||||
func (m *ExpandResp) Reset() { *m = ExpandResp{} }
|
||||
func (m *ExpandResp) String() string { return proto.CompactTextString(m) }
|
||||
func (*ExpandResp) ProtoMessage() {}
|
||||
func (*ExpandResp) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *ExpandResp) GetUrl() string {
|
||||
if m != nil {
|
||||
return m.Url
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type ShortenReq struct {
|
||||
Url string `protobuf:"bytes,1,opt,name=url" json:"url,omitempty"`
|
||||
}
|
||||
|
||||
func (m *ShortenReq) Reset() { *m = ShortenReq{} }
|
||||
func (m *ShortenReq) String() string { return proto.CompactTextString(m) }
|
||||
func (*ShortenReq) ProtoMessage() {}
|
||||
func (*ShortenReq) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
|
||||
|
||||
func (m *ShortenReq) GetUrl() string {
|
||||
if m != nil {
|
||||
return m.Url
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type ShortenResp struct {
|
||||
Shorten string `protobuf:"bytes,1,opt,name=shorten" json:"shorten,omitempty"`
|
||||
}
|
||||
|
||||
func (m *ShortenResp) Reset() { *m = ShortenResp{} }
|
||||
func (m *ShortenResp) String() string { return proto.CompactTextString(m) }
|
||||
func (*ShortenResp) ProtoMessage() {}
|
||||
func (*ShortenResp) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
|
||||
|
||||
func (m *ShortenResp) GetShorten() string {
|
||||
if m != nil {
|
||||
return m.Shorten
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*ExpandReq)(nil), "transform.expandReq")
|
||||
proto.RegisterType((*ExpandResp)(nil), "transform.expandResp")
|
||||
proto.RegisterType((*ShortenReq)(nil), "transform.shortenReq")
|
||||
proto.RegisterType((*ShortenResp)(nil), "transform.shortenResp")
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for Transformer service
|
||||
|
||||
type TransformerClient interface {
|
||||
Expand(ctx context.Context, in *ExpandReq, opts ...grpc.CallOption) (*ExpandResp, error)
|
||||
Shorten(ctx context.Context, in *ShortenReq, opts ...grpc.CallOption) (*ShortenResp, error)
|
||||
}
|
||||
|
||||
type transformerClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewTransformerClient(cc *grpc.ClientConn) TransformerClient {
|
||||
return &transformerClient{cc}
|
||||
}
|
||||
|
||||
func (c *transformerClient) Expand(ctx context.Context, in *ExpandReq, opts ...grpc.CallOption) (*ExpandResp, error) {
|
||||
out := new(ExpandResp)
|
||||
err := grpc.Invoke(ctx, "/transform.transformer/expand", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *transformerClient) Shorten(ctx context.Context, in *ShortenReq, opts ...grpc.CallOption) (*ShortenResp, error) {
|
||||
out := new(ShortenResp)
|
||||
err := grpc.Invoke(ctx, "/transform.transformer/shorten", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Transformer service
|
||||
|
||||
type TransformerServer interface {
|
||||
Expand(context.Context, *ExpandReq) (*ExpandResp, error)
|
||||
Shorten(context.Context, *ShortenReq) (*ShortenResp, error)
|
||||
}
|
||||
|
||||
func RegisterTransformerServer(s *grpc.Server, srv TransformerServer) {
|
||||
s.RegisterService(&_Transformer_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _Transformer_Expand_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ExpandReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TransformerServer).Expand(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/transform.transformer/Expand",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TransformerServer).Expand(ctx, req.(*ExpandReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Transformer_Shorten_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ShortenReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TransformerServer).Shorten(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/transform.transformer/Shorten",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TransformerServer).Shorten(ctx, req.(*ShortenReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _Transformer_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "transform.transformer",
|
||||
HandlerType: (*TransformerServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "expand",
|
||||
Handler: _Transformer_Expand_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "shorten",
|
||||
Handler: _Transformer_Shorten_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "transform.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("transform.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 163 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x2f, 0x29, 0x4a, 0xcc,
|
||||
0x2b, 0x4e, 0xcb, 0x2f, 0xca, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x84, 0x0b, 0x28,
|
||||
0xa9, 0x72, 0x71, 0xa6, 0x56, 0x14, 0x24, 0xe6, 0xa5, 0x04, 0xa5, 0x16, 0x0a, 0x49, 0x70, 0xb1,
|
||||
0x17, 0x67, 0xe4, 0x17, 0x95, 0xa4, 0xe6, 0x49, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x06, 0xc1, 0xb8,
|
||||
0x4a, 0x72, 0x5c, 0x5c, 0x30, 0x65, 0xc5, 0x05, 0x42, 0x02, 0x5c, 0xcc, 0xa5, 0x45, 0x39, 0x50,
|
||||
0x35, 0x20, 0x26, 0x48, 0x1e, 0xaa, 0x14, 0x64, 0x0e, 0xa6, 0xbc, 0x3a, 0x17, 0x37, 0x5c, 0xbe,
|
||||
0xb8, 0x00, 0xb7, 0x45, 0x46, 0x75, 0x5c, 0xdc, 0x70, 0xc7, 0xa5, 0x16, 0x09, 0x99, 0x72, 0xb1,
|
||||
0x41, 0xec, 0x15, 0x12, 0xd1, 0x43, 0xf8, 0x02, 0xee, 0x62, 0x29, 0x51, 0x2c, 0xa2, 0xc5, 0x05,
|
||||
0x42, 0x16, 0x70, 0xf3, 0x85, 0x90, 0x55, 0x20, 0x9c, 0x28, 0x25, 0x86, 0x4d, 0xb8, 0xb8, 0x20,
|
||||
0x89, 0x0d, 0x1c, 0x42, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x8a, 0x27, 0x78, 0x05, 0x34,
|
||||
0x01, 0x00, 0x00,
|
||||
}
|
||||
40
example/shorturl/rpc/transform/transform.go
Executable file
40
example/shorturl/rpc/transform/transform.go
Executable file
@@ -0,0 +1,40 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: transform.proto
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"shorturl/rpc/transform/internal/config"
|
||||
"shorturl/rpc/transform/internal/server"
|
||||
"shorturl/rpc/transform/internal/svc"
|
||||
transform "shorturl/rpc/transform/pb"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/conf"
|
||||
"github.com/tal-tech/go-zero/rpcx"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/transform.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
transformerSrv := server.NewTransformerServer(ctx)
|
||||
|
||||
s, err := rpcx.NewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
transform.RegisterTransformerServer(grpcServer, transformerSrv)
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
24
example/shorturl/rpc/transform/transform.proto
Normal file
24
example/shorturl/rpc/transform/transform.proto
Normal file
@@ -0,0 +1,24 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package transform;
|
||||
|
||||
message expandReq {
|
||||
string shorten = 1;
|
||||
}
|
||||
|
||||
message expandResp {
|
||||
string url = 1;
|
||||
}
|
||||
|
||||
message shortenReq {
|
||||
string url = 1;
|
||||
}
|
||||
|
||||
message shortenResp {
|
||||
string shorten = 1;
|
||||
}
|
||||
|
||||
service transformer {
|
||||
rpc expand(expandReq) returns(expandResp);
|
||||
rpc shorten(shortenReq) returns(shortenResp);
|
||||
}
|
||||
96
example/shorturl/rpc/transform/transformer/transformer.go
Executable file
96
example/shorturl/rpc/transform/transformer/transformer.go
Executable file
@@ -0,0 +1,96 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: transform.proto
|
||||
|
||||
//go:generate mockgen -destination ./transformer_mock.go -package transformer -source $GOFILE
|
||||
|
||||
package transformer
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
transform "shorturl/rpc/transform/pb"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/jsonx"
|
||||
"github.com/tal-tech/go-zero/rpcx"
|
||||
)
|
||||
|
||||
type (
|
||||
Transformer interface {
|
||||
Expand(ctx context.Context, in *ExpandReq) (*ExpandResp, error)
|
||||
Shorten(ctx context.Context, in *ShortenReq) (*ShortenResp, error)
|
||||
}
|
||||
|
||||
defaultTransformer struct {
|
||||
cli rpcx.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewTransformer(cli rpcx.Client) Transformer {
|
||||
return &defaultTransformer{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultTransformer) Expand(ctx context.Context, in *ExpandReq) (*ExpandResp, error) {
|
||||
var request transform.ExpandReq
|
||||
bts, err := jsonx.Marshal(in)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
err = jsonx.Unmarshal(bts, &request)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
client := transform.NewTransformerClient(m.cli.Conn())
|
||||
resp, err := client.Expand(ctx, &request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ret ExpandResp
|
||||
bts, err = jsonx.Marshal(resp)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
err = jsonx.Unmarshal(bts, &ret)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
func (m *defaultTransformer) Shorten(ctx context.Context, in *ShortenReq) (*ShortenResp, error) {
|
||||
var request transform.ShortenReq
|
||||
bts, err := jsonx.Marshal(in)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
err = jsonx.Unmarshal(bts, &request)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
client := transform.NewTransformerClient(m.cli.Conn())
|
||||
resp, err := client.Shorten(ctx, &request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ret ShortenResp
|
||||
bts, err = jsonx.Marshal(resp)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
err = jsonx.Unmarshal(bts, &ret)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
return &ret, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: transformer.go
|
||||
|
||||
// Package transformer is a generated GoMock package.
|
||||
package transformer
|
||||
|
||||
import (
|
||||
context "context"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockTransformer is a mock of Transformer interface
|
||||
type MockTransformer struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockTransformerMockRecorder
|
||||
}
|
||||
|
||||
// MockTransformerMockRecorder is the mock recorder for MockTransformer
|
||||
type MockTransformerMockRecorder struct {
|
||||
mock *MockTransformer
|
||||
}
|
||||
|
||||
// NewMockTransformer creates a new mock instance
|
||||
func NewMockTransformer(ctrl *gomock.Controller) *MockTransformer {
|
||||
mock := &MockTransformer{ctrl: ctrl}
|
||||
mock.recorder = &MockTransformerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockTransformer) EXPECT() *MockTransformerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Expand mocks base method
|
||||
func (m *MockTransformer) Expand(ctx context.Context, in *ExpandReq) (*ExpandResp, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Expand", ctx, in)
|
||||
ret0, _ := ret[0].(*ExpandResp)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Expand indicates an expected call of Expand
|
||||
func (mr *MockTransformerMockRecorder) Expand(ctx, in interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Expand", reflect.TypeOf((*MockTransformer)(nil).Expand), ctx, in)
|
||||
}
|
||||
|
||||
// Shorten mocks base method
|
||||
func (m *MockTransformer) Shorten(ctx context.Context, in *ShortenReq) (*ShortenResp, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Shorten", ctx, in)
|
||||
ret0, _ := ret[0].(*ShortenResp)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Shorten indicates an expected call of Shorten
|
||||
func (mr *MockTransformerMockRecorder) Shorten(ctx, in interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Shorten", reflect.TypeOf((*MockTransformer)(nil).Shorten), ctx, in)
|
||||
}
|
||||
26
example/shorturl/rpc/transform/transformer/types.go
Executable file
26
example/shorturl/rpc/transform/transformer/types.go
Executable file
@@ -0,0 +1,26 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: transform.proto
|
||||
|
||||
package transformer
|
||||
|
||||
import "errors"
|
||||
|
||||
var errJsonConvert = errors.New("json convert error")
|
||||
|
||||
type (
|
||||
ExpandReq struct {
|
||||
Shorten string `json:"shorten,omitempty"`
|
||||
}
|
||||
|
||||
ExpandResp struct {
|
||||
Url string `json:"url,omitempty"`
|
||||
}
|
||||
|
||||
ShortenReq struct {
|
||||
Url string `json:"url,omitempty"`
|
||||
}
|
||||
|
||||
ShortenResp struct {
|
||||
Shorten string `json:"shorten,omitempty"`
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user