Skip to content

Commit

Permalink
update shorturl doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Aug 29, 2020
1 parent 6e3d99e commit 6c4a4be
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 144 deletions.
Binary file added doc/images/shorturl-benchmark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
381 changes: 349 additions & 32 deletions doc/shorturl.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ go get -u github.com/tal-tech/go-zero

## 6. Quick Start

0. 完整示例请查看[从0到1快速构建一个高并发的微服务系统](doc/shorturl.md)

1. 编译goctl工具

```shell
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
func {{.handlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logic.{{.logic}}(r.Context(), ctx)
{{.handlerBody}}
}
}
Expand All @@ -39,6 +38,7 @@ func {{.handlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
}
`
hasRespTemplate = `
l := logic.{{.logic}}(r.Context(), ctx)
{{.logicResponse}} l.{{.callee}}({{.req}})
if err != nil {
httpx.Error(w, err)
Expand Down Expand Up @@ -84,6 +84,7 @@ func genHandler(dir string, group spec.Group, route spec.Route) error {
var logicBodyBuilder strings.Builder
t := template.Must(template.New("hasRespTemplate").Parse(hasRespTemplate))
if err := t.Execute(&logicBodyBuilder, map[string]string{
"logic": "New" + strings.TrimSuffix(strings.Title(handler), "Handler") + "Logic",
"callee": strings.Title(strings.TrimSuffix(handler, "Handler")),
"req": req,
"logicResponse": logicResponse,
Expand Down Expand Up @@ -134,7 +135,6 @@ func doGenToFile(dir, handler string, group spec.Group, route spec.Route, bodyBu
t := template.Must(template.New("handlerTemplate").Parse(handlerTemplate))
buffer := new(bytes.Buffer)
err = t.Execute(buffer, map[string]string{
"logic": "New" + strings.TrimSuffix(strings.Title(handler), "Handler") + "Logic",
"importPackages": genHandlerImports(group, route, parentPkg),
"handlerName": handler,
"handlerBody": strings.TrimSpace(bodyBuilder.String()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ type ServiceContext struct {
Config {{.config}}
}
func NewServiceContext(config {{.config}}) *ServiceContext {
return &ServiceContext{Config: config}
func NewServiceContext(c {{.config}}) *ServiceContext {
return &ServiceContext{Config: c}
}
`
)

Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/template/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package template

var Delete = `
func (m *{{.upperStartCamelObject}}Model) Delete({{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
{{if .withCache}}{{if .containsIndexCache}}data,err:=m.FindOne({{.lowerStartCamelPrimaryKey}})
{{if .withCache}}{{if .containsIndexCache}}_, err:=m.FindOne({{.lowerStartCamelPrimaryKey}})
if err!=nil{
return err
}{{end}}
Expand Down
1 change: 0 additions & 1 deletion tools/goctl/model/sql/template/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var (
"database/sql"
"fmt"
"strings"
"time"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/sqlc"
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/template/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package template

var Insert = `
func (m *{{.upperStartCamelObject}}Model) Insert(data {{.upperStartCamelObject}}) (sql.Result, error) {
query := ` + "`" + `insert into ` + "`" + ` + m.table + ` + "`(` + " + `{{.lowerStartCamelObject}}RowsExpectAutoSet` + " + `) value ({{.expression}})` " + `
query := ` + "`" + `insert into ` + "`" + ` + m.table + ` + "` (` + " + `{{.lowerStartCamelObject}}RowsExpectAutoSet` + " + `) values ({{.expression}})` " + `
return m.{{if .withCache}}ExecNoCache{{else}}conn.Exec{{end}}(query, {{.expressionValues}})
}
`
38 changes: 13 additions & 25 deletions tools/goctl/rpc/ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,23 @@ import (
const (
flagSrc = "src"
flagDir = "dir"
flagShared = "shared"
flagService = "service"
flagIdea = "idea"
)

type (
RpcContext struct {
ProjectPath string
ProjectName stringx.String
ServiceName stringx.String
CurrentPath string
Module string
ProtoFileSrc string
ProtoSource string
TargetDir string
SharedDir string
console.Console
}
)
type RpcContext struct {
ProjectPath string
ProjectName stringx.String
ServiceName stringx.String
CurrentPath string
Module string
ProtoFileSrc string
ProtoSource string
TargetDir string
console.Console
}

func MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName string, idea bool) *RpcContext {
func MustCreateRpcContext(protoSrc, targetDir, serviceName string, idea bool) *RpcContext {
log := console.NewConsole(idea)
info, err := prepare(log)
log.Must(err)
Expand All @@ -54,15 +50,9 @@ func MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName string, id
if stringx.From(targetDir).IsEmptyOrSpace() {
targetDir = current
}
if stringx.From(sharedDir).IsEmptyOrSpace() {
sharedDir = filepath.Join(current, "shared")
}
targetDirFp, err := filepath.Abs(targetDir)
log.Must(err)

sharedFp, err := filepath.Abs(sharedDir)
log.Must(err)

if stringx.From(serviceName).IsEmptyOrSpace() {
serviceName = getServiceFromRpcStructure(targetDirFp)
}
Expand All @@ -80,7 +70,6 @@ func MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName string, id
ProtoFileSrc: srcFp,
ProtoSource: filepath.Base(srcFp),
TargetDir: targetDirFp,
SharedDir: sharedFp,
Console: log,
}
}
Expand All @@ -93,10 +82,9 @@ func MustCreateRpcContextFromCli(ctx *cli.Context) *RpcContext {
}
protoSrc := ctx.String(flagSrc)
targetDir := ctx.String(flagDir)
sharedDir := ctx.String(flagShared)
serviceName := ctx.String(flagService)
idea := ctx.Bool(flagIdea)
return MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName, idea)
return MustCreateRpcContext(protoSrc, targetDir, serviceName, idea)
}

func getServiceFromRpcStructure(targetDir string) string {
Expand Down
17 changes: 7 additions & 10 deletions tools/goctl/rpc/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ const (
dirConfig = "config"
dirEtc = "etc"
dirSvc = "svc"
dirShared = "shared"
dirHandler = "handler"
dirServer = "server"
dirLogic = "logic"
dirPb = "pb"
dirInternal = "internal"
fileConfig = "config.go"
fileServiceContext = "servicecontext.go"
)

type (
defaultRpcGenerator struct {
dirM map[string]string
Ctx *ctx.RpcContext
ast *parser.PbAst
}
)
type defaultRpcGenerator struct {
dirM map[string]string
Ctx *ctx.RpcContext
ast *parser.PbAst
}

func NewDefaultRpcGenerator(ctx *ctx.RpcContext) *defaultRpcGenerator {
return &defaultRpcGenerator{
Expand Down Expand Up @@ -80,7 +77,7 @@ func (g *defaultRpcGenerator) Generate() (err error) {
return
}

err = g.genShared()
err = g.genCall()
if err != nil {
return
}
Expand Down
Loading

0 comments on commit 6c4a4be

Please sign in to comment.