add shorturl example code
This commit is contained in:
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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user