Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin48435 committed Oct 9, 2024
1 parent 0a80574 commit b6f4ae1
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 95 deletions.
7 changes: 4 additions & 3 deletions cmd/internal/modular/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func (receiver *Work) Init() {
if err = receiver.runner.Run("go", "work", "use", "main"); err != nil {
panic(err)
}
if err = receiver.runner.Run("go", "work", "sync"); err != nil {
panic(err)
}
// Comment below code due to performance issue
//if err = receiver.runner.Run("go", "work", "sync"); err != nil {
// panic(err)
//}
}
15 changes: 8 additions & 7 deletions cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ func (b *AbstractBaseGenerator) GenService() {
}

func (b *AbstractBaseGenerator) goModTidy() {
wd, _ := os.Getwd()
os.Chdir(filepath.Join(b.Dir))
err := b.runner.Run("go", "mod", "tidy")
if err != nil {
panic(err)
}
os.Chdir(wd)
// here go mod tidy cause performance issue on some computer
//wd, _ := os.Getwd()
//os.Chdir(filepath.Join(b.Dir))
//err := b.runner.Run("go", "mod", "tidy")
//if err != nil {
// panic(err)
//}
//os.Chdir(wd)
}
1 change: 0 additions & 1 deletion cmd/internal/svc/codegen/httphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"github.com/unionj-cloud/go-doudou/v2/framework"
"net/http"
"os"
)
type {{.Meta.Name}}Handler interface {
Expand Down
12 changes: 5 additions & 7 deletions cmd/internal/svc/codegen/httphandlerimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,10 @@ var appendHttpHandlerImplTmpl = `
var importTmpl = `
"context"
"github.com/bytedance/sonic"
"fmt"
"github.com/sirupsen/logrus"
v3 "github.com/unionj-cloud/go-doudou/v2/toolkit/openapi/v3"
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"github.com/unionj-cloud/go-doudou/v2/framework/rest/httprouter"
"github.com/unionj-cloud/go-doudou/v2/toolkit/cast"
{{.ServiceAlias}} "{{.ServicePackage}}"
"{{.DtoPackage}}"
"net/http"
"github.com/pkg/errors"
`

var initHttpHandlerImplTmpl = templates.EditableHeaderTmpl + `package httpsrv
Expand Down Expand Up @@ -521,15 +516,18 @@ func GenHttpHandlerImpl(dir string, ic astutils.InterfaceCollector, config GenHt
}

original = append(original, buf.Bytes()...)
if tpl, err = template.New("himportimpl.go.tmpl").Parse(importTmpl); err != nil {
if tpl, err = template.New(importTmpl).Parse(importTmpl); err != nil {
panic(err)
}
dtoPkg := astutils.GetPkgPath(filepath.Join(dir, "dto"))
if err = tpl.Execute(&importBuf, struct {
ServicePackage string
ServiceAlias string
DtoPackage string
}{
ServicePackage: servicePkg,
ServiceAlias: ic.Package.Name,
DtoPackage: dtoPkg,
}); err != nil {
panic(err)
}
Expand Down
22 changes: 13 additions & 9 deletions cmd/internal/svc/codegen/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"{{.DtoPackage}}"
)

{{ if eq .ProjectType "rest" }}
{{- if eq .ProjectType "rest" }}
//go:generate go-doudou svc http --case {{ .JsonCase }}
{{ else }}
{{- else }}
//go:generate go-doudou svc grpc --http2grpc --case {{ .JsonCase }}
{{ end }}
{{- end }}

type {{.SvcName}} interface {
// You can define your service methods as your need. Below is an example.
Expand Down Expand Up @@ -119,7 +119,6 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/gorilla/handlers v1.5.1
github.com/iancoleman/strcase v0.1.3
github.com/jmoiron/sqlx v1.3.1
github.com/opentracing-contrib/go-stdlib v1.0.0
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -327,8 +326,12 @@ func InitProj(conf InitProjConfig) {
if module {
parser.ParseDto(dir, parser.DEFAULT_DTO_PKGS...)
ic := astutils.BuildInterfaceCollector(filepath.Join(dir, "svc.go"), astutils.ExprString)
genPlugin(dir, ic)
genMainModule(dir)
genPlugin(dir, ic, CodeGenConfig{
ProjectType: projectType,
})
genMain(dir, CodeGenConfig{
ProjectType: projectType,
})
mainMainFile := filepath.Join(filepath.Dir(dir), "main", "cmd", "main.go")
fileContent, err := ioutil.ReadFile(mainMainFile)
if err != nil {
Expand All @@ -337,9 +340,10 @@ func InitProj(conf InitProjConfig) {
pluginPkg := astutils.GetPkgPath(filepath.Join(dir, "plugin"))
original := astutils.AppendImportStatements(fileContent, []byte(fmt.Sprintf(`_ "%s"`, pluginPkg)))
astutils.FixImport(original, mainMainFile)
if err = runner.Run("go", "work", "sync"); err != nil {
panic(err)
}
// Comment below code due to performance issue
//if err = runner.Run("go", "work", "sync"); err != nil {
// panic(err)
//}
}
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/internal/svc/codegen/mainmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/unionj-cloud/go-doudou/v2/version"
)

func genMainModule(dir string) {
func genMain(dir string, conf CodeGenConfig) {
var (
err error
mainfile string
Expand All @@ -31,16 +31,18 @@ func genMainModule(dir string) {
}
defer f.Close()

if tpl, err = template.New(templates.MainModuleTmpl).Parse(templates.MainModuleTmpl); err != nil {
if tpl, err = template.New(templates.MainTmpl).Parse(templates.MainTmpl); err != nil {
panic(err)
}
pluginPkg := astutils.GetPkgPath(filepath.Join(dir, "plugin"))
if err = tpl.Execute(&buf, struct {
CodeGenConfig
PluginPackage string
Version string
}{
PluginPackage: pluginPkg,
Version: version.Release,
CodeGenConfig: conf,
}); err != nil {
panic(err)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/internal/svc/codegen/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import (
"github.com/unionj-cloud/go-doudou/v2/version"
)

func genPlugin(dir string, ic astutils.InterfaceCollector) {
type CodeGenConfig struct {
ProjectType string
}

func genPlugin(dir string, ic astutils.InterfaceCollector, conf CodeGenConfig) {
var (
err error
pluginFile string
Expand Down Expand Up @@ -44,6 +48,7 @@ func genPlugin(dir string, ic astutils.InterfaceCollector) {
svcName := ic.Interfaces[0].Name
alias := ic.Package.Name
if err = tpl.Execute(&buf, struct {
CodeGenConfig
ServicePackage string
ConfigPackage string
TransportGrpcPackage string
Expand All @@ -59,6 +64,7 @@ func genPlugin(dir string, ic astutils.InterfaceCollector) {
ServiceAlias: alias,
SvcName: svcName,
Version: version.Release,
CodeGenConfig: conf,
}); err != nil {
panic(err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/internal/svc/codegen/svcimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var svcimportTmpl = `
"context"
"{{.ConfigPackage}}"
"github.com/jmoiron/sqlx"
"{{.DtoPackage}}"
"github.com/brianvoe/gofakeit/v6"
`

Expand Down Expand Up @@ -66,6 +66,7 @@ var svcimportTmplGrpc = `
"context"
"{{.ConfigPackage}}"
pb "{{.PbPackage}}"
"google.golang.org/protobuf/types/known/emptypb"
`

var appendPartGrpc = `{{- range $m := .GrpcSvc.Rpcs }}
Expand Down Expand Up @@ -192,13 +193,16 @@ func GenSvcImpl(dir string, ic astutils.InterfaceCollector) {
}

original = append(original, buf.Bytes()...)
dtoPkg := astutils.GetPkgPath(filepath.Join(dir, "dto"))
if tpl, err = template.New(svcimportTmpl).Parse(svcimportTmpl); err != nil {
panic(err)
}
if err = tpl.Execute(&importBuf, struct {
ConfigPackage string
DtoPackage string
}{
ConfigPackage: cfgPkg,
DtoPackage: dtoPkg,
}); err != nil {
panic(err)
}
Expand Down
22 changes: 12 additions & 10 deletions cmd/internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@ func (receiver *Svc) Http() {
RoutePatternStrategy: receiver.RoutePatternStrategy,
AllowGetWithReqBody: receiver.AllowGetWithReqBody,
})
runner := receiver.runner
if runner == nil {
runner = executils.CmdRunner{}
}
runner.Run("go", "mod", "tidy")
// here go mod tidy cause performance issue on some computer
//runner := receiver.runner
//if runner == nil {
// runner = executils.CmdRunner{}
//}
//runner.Run("go", "mod", "tidy")
}

// Init inits a project
Expand Down Expand Up @@ -552,9 +553,10 @@ func (receiver *Svc) Grpc() {
}
codegen.FixModGrpc(dir)
codegen.GenMethodAnnotationStore(dir, ic)
runner := receiver.runner
if runner == nil {
runner = executils.CmdRunner{}
}
runner.Run("go", "mod", "tidy")
// here go mod tidy cause performance issue on some computer
//runner := receiver.runner
//if runner == nil {
// runner = executils.CmdRunner{}
//}
//runner.Run("go", "mod", "tidy")
}
12 changes: 11 additions & 1 deletion cmd/internal/templates/mainmodule.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package templates

var MainModuleTmpl = EditableHeaderTmpl + `package main
var MainTmpl = EditableHeaderTmpl + `package main

import (
{{- if ne .ProjectType "rest" }}
"github.com/unionj-cloud/go-doudou/v2/framework/grpcx"
{{- end }}
"github.com/unionj-cloud/go-doudou/v2/framework/plugin"
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"github.com/unionj-cloud/go-doudou/v2/toolkit/zlogger"
Expand All @@ -12,11 +14,17 @@ import (

func main() {
srv := rest.NewRestServer()
{{- if ne .ProjectType "rest" }}
grpcServer := grpcx.NewEmptyGrpcServer()
{{- end }}
plugins := plugin.GetServicePlugins()
for _, key := range plugins.Keys() {
value, _ := plugins.Get(key)
{{- if eq .ProjectType "rest" }}
value.Initialize(srv, nil, nil)
{{- else }}
value.Initialize(srv, grpcServer, nil)
{{- end }}
}
defer func() {
if r := recover(); r != nil {
Expand All @@ -27,9 +35,11 @@ func main() {
value.Close()
}
}()
{{- if ne .ProjectType "rest" }}
go func() {
grpcServer.Run()
}()
{{- end }}
srv.Run()
}
`
28 changes: 20 additions & 8 deletions cmd/internal/templates/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ package templates
var PluginTmpl = EditableHeaderTmpl + `package plugin

import (
"github.com/unionj-cloud/go-doudou/v2/framework/grpcx"
"github.com/unionj-cloud/go-doudou/v2/framework/plugin"
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"github.com/unionj-cloud/go-doudou/v2/toolkit/pipeconn"
"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
{{.ServiceAlias}} "{{.ServicePackage}}"
"{{.ConfigPackage}}"
pb "{{.TransportGrpcPackage}}"
"{{.TransportHttpPackage}}"
"google.golang.org/grpc"
"os"
{{- if ne .ProjectType "rest" }}
pb "{{.TransportGrpcPackage}}"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog/v2"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
Expand All @@ -15,21 +23,19 @@ import (
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/tags"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/unionj-cloud/go-doudou/v2/framework/grpcx"
"github.com/unionj-cloud/go-doudou/v2/framework/plugin"
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"github.com/unionj-cloud/go-doudou/v2/toolkit/pipeconn"
"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
"github.com/unionj-cloud/go-doudou/v2/toolkit/zlogger"
"google.golang.org/grpc"
"os"
{{- end }}
)

var _ plugin.ServicePlugin = (*{{.SvcName}}Plugin)(nil)

type {{.SvcName}}Plugin struct {
grpcConns []*grpc.ClientConn
{{- if eq .ProjectType "rest" }}
serviceInstance service.{{.SvcName}}
{{- else }}
serviceInstance pb.{{.SvcName}}ServiceServer
{{- end }}
}

func (receiver *{{.SvcName}}Plugin) GetServiceInstance() interface{} {
Expand Down Expand Up @@ -58,9 +64,14 @@ func (receiver *{{.SvcName}}Plugin) Initialize(restServer *rest.RestServer, grpc
conf := config.LoadFromEnv()
svc := {{.ServiceAlias}}.New{{.SvcName}}(conf)
receiver.serviceInstance = svc
{{- if eq .ProjectType "rest" }}
routes := httpsrv.Routes(httpsrv.New{{.SvcName}}Handler(svc))
{{- else }}
routes := httpsrv.Routes(httpsrv.New{{.SvcName}}Http2Grpc(svc))
{{- end }}
restServer.GroupRoutes("/{{.SvcName | toLower}}", routes)
restServer.GroupRoutes("/{{.SvcName | toLower}}", rest.DocRoutes(service.Oas))
{{- if ne .ProjectType "rest" }}
if grpcServer.Server == nil {
grpcServer.Server = grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
Expand All @@ -82,6 +93,7 @@ func (receiver *{{.SvcName}}Plugin) Initialize(restServer *rest.RestServer, grpc
)
}
pb.Register{{.SvcName}}ServiceServer(grpcServer, svc)
{{- end }}
}

func init() {
Expand Down
Loading

0 comments on commit b6f4ae1

Please sign in to comment.