fix: quickstart wrong package when go.mod exists in parent dir (#2048)
* chore: fix typo * fix: quickstart in dir with go.mod * fix: runner failed * chore: refine code * chore: simplify quickstart mono
This commit is contained in:
@@ -4,9 +4,10 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"greet/api/internal/svc"
|
||||
"greet/api/internal/types"{{if .callRPC}}
|
||||
"greet/rpc/greet"{{end}}
|
||||
|
||||
"{{.svcPkg}}"
|
||||
"{{.typesPkg}}"{{if .callRPC}}
|
||||
"{{.rpcClientPkg}}"{{end}}
|
||||
)
|
||||
|
||||
type PingLogic struct {
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"{{.configPkg}}"{{if .callRPC}}
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"greet/api/internal/config"
|
||||
"greet/rpc/greet"
|
||||
"{{.rpcClientPkg}}"{{end}}
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
GreetRpc greet.Greet
|
||||
Config config.Config{{if .callRPC}}
|
||||
GreetRpc greet.Greet{{end}}
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
client := zrpc.MustNewClient(zrpc.RpcClientConf{
|
||||
{{if .callRPC}}client := zrpc.MustNewClient(zrpc.RpcClientConf{
|
||||
Target: "127.0.0.1:8080",
|
||||
})
|
||||
}){{end}}
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
GreetRpc: greet.NewGreet(client),
|
||||
{{if .callRPC}}GreetRpc: greet.NewGreet(client),{{end}}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,7 @@ var (
|
||||
protocContent string
|
||||
//go:embed idl/rpc.yaml
|
||||
rpcEtcContent string
|
||||
|
||||
zRPCWorkDir string
|
||||
zrpcWorkDir string
|
||||
)
|
||||
|
||||
type serviceImpl struct {
|
||||
@@ -32,12 +31,12 @@ func (s serviceImpl) Start() {
|
||||
func (s serviceImpl) Stop() {}
|
||||
|
||||
func initRPCProto() error {
|
||||
zRPCWorkDir = filepath.Join(projectDir, "rpc")
|
||||
if err := pathx.MkdirIfNotExist(zRPCWorkDir); err != nil {
|
||||
zrpcWorkDir = filepath.Join(projectDir, "rpc")
|
||||
if err := pathx.MkdirIfNotExist(zrpcWorkDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
protoFilename := filepath.Join(zRPCWorkDir, protoName)
|
||||
protoFilename := filepath.Join(zrpcWorkDir, protoName)
|
||||
rpcBytes := []byte(protocContent)
|
||||
return ioutil.WriteFile(protoFilename, rpcBytes, 0666)
|
||||
}
|
||||
@@ -54,8 +53,8 @@ func (m micro) mustStartRPCProject() {
|
||||
logx.Must(initRPCProto())
|
||||
log.Debug(">> Generating quickstart zRPC project...")
|
||||
arg := "goctl rpc protoc " + protoName + " --go_out=. --go-grpc_out=. --zrpc_out=. --verbose"
|
||||
execCommand(zRPCWorkDir, arg)
|
||||
etcFile := filepath.Join(zRPCWorkDir, "etc", "greet.yaml")
|
||||
execCommand(zrpcWorkDir, arg)
|
||||
etcFile := filepath.Join(zrpcWorkDir, "etc", "greet.yaml")
|
||||
logx.Must(ioutil.WriteFile(etcFile, []byte(rpcEtcContent), 0666))
|
||||
}
|
||||
|
||||
@@ -65,7 +64,7 @@ func (m micro) start() {
|
||||
sg := service.NewServiceGroup()
|
||||
sg.Add(serviceImpl{func() {
|
||||
log.Debug(">> Ready to start a zRPC server...")
|
||||
goStart(zRPCWorkDir)
|
||||
goStart(zrpcWorkDir)
|
||||
}})
|
||||
sg.Add(serviceImpl{func() {
|
||||
mono.start()
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
@@ -22,9 +23,11 @@ var (
|
||||
apiEtcContent string
|
||||
|
||||
apiWorkDir string
|
||||
rpcWorkDir string
|
||||
)
|
||||
|
||||
func initAPIFlags() error {
|
||||
rpcWorkDir = filepath.Join(projectDir, "rpc")
|
||||
apiWorkDir = filepath.Join(projectDir, "api")
|
||||
if err := pathx.MkdirIfNotExist(apiWorkDir); err != nil {
|
||||
return err
|
||||
@@ -58,15 +61,36 @@ func (m mono) createAPIProject() {
|
||||
etcFile := filepath.Join(apiWorkDir, "etc", "greet.yaml")
|
||||
logx.Must(ioutil.WriteFile(etcFile, []byte(apiEtcContent), 0666))
|
||||
logicFile := filepath.Join(apiWorkDir, "internal", "logic", "pinglogic.go")
|
||||
svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go")
|
||||
configPath := filepath.Join(apiWorkDir, "internal", "config")
|
||||
svcPath := filepath.Join(apiWorkDir, "internal", "svc")
|
||||
typesPath := filepath.Join(apiWorkDir, "internal", "types")
|
||||
svcPkg, err := golang.GetParentPackage(svcPath)
|
||||
logx.Must(err)
|
||||
typesPkg, err := golang.GetParentPackage(typesPath)
|
||||
logx.Must(err)
|
||||
configPkg, err := golang.GetParentPackage(configPath)
|
||||
logx.Must(err)
|
||||
|
||||
logx.Must(util.With("logic").Parse(apiLogicContent).SaveTo(map[string]bool{
|
||||
"callRPC": m.callRPC,
|
||||
var rpcClientPkg string
|
||||
if m.callRPC {
|
||||
rpcClientPath := filepath.Join(rpcWorkDir, "greet")
|
||||
rpcClientPkg, err = golang.GetParentPackage(rpcClientPath)
|
||||
logx.Must(err)
|
||||
}
|
||||
|
||||
logx.Must(util.With("logic").Parse(apiLogicContent).SaveTo(map[string]interface{}{
|
||||
"svcPkg": svcPkg,
|
||||
"typesPkg": typesPkg,
|
||||
"rpcClientPkg": rpcClientPkg,
|
||||
"callRPC": m.callRPC,
|
||||
}, logicFile, true))
|
||||
|
||||
if m.callRPC {
|
||||
svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go")
|
||||
logx.Must(ioutil.WriteFile(svcFile, []byte(svcContent), 0666))
|
||||
}
|
||||
logx.Must(util.With("svc").Parse(svcContent).SaveTo(map[string]interface{}{
|
||||
"rpcClientPkg": rpcClientPkg,
|
||||
"configPkg": configPkg,
|
||||
"callRPC": m.callRPC,
|
||||
}, svcFile, true))
|
||||
}
|
||||
|
||||
func (m mono) start() {
|
||||
|
||||
Reference in New Issue
Block a user