feat(goctl): Support gateway sample generation (#3049)
This commit is contained in:
60
tools/goctl/gateway/cmd.go
Normal file
60
tools/goctl/gateway/cmd.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/internal/cobrax"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
var (
|
||||
varStringHome string
|
||||
varStringRemote string
|
||||
varStringBranch string
|
||||
varStringDir string
|
||||
|
||||
Cmd = cobrax.NewCommand("gateway", cobrax.WithRunE(generateGateway))
|
||||
)
|
||||
|
||||
func init() {
|
||||
Cmd.PersistentFlags().StringVar(&varStringHome, "home")
|
||||
Cmd.PersistentFlags().StringVar(&varStringRemote, "remote")
|
||||
Cmd.PersistentFlags().StringVar(&varStringBranch, "branch")
|
||||
Cmd.PersistentFlags().StringVar(&varStringDir, "dir")
|
||||
}
|
||||
|
||||
func generateGateway(*cobra.Command, []string) error {
|
||||
if err:=pathx.MkdirIfNotExist(varStringDir);err!=nil{
|
||||
return err
|
||||
}
|
||||
|
||||
if _,err:=ctx.Prepare(varStringDir);err!=nil{
|
||||
return err
|
||||
}
|
||||
|
||||
etcContent, err := pathx.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mainContent, err := pathx.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
etcDir := filepath.Join(varStringDir, "etc")
|
||||
if err := pathx.MkdirIfNotExist(etcDir); err != nil {
|
||||
return err
|
||||
}
|
||||
etcFile := filepath.Join(etcDir, "gateway.yaml")
|
||||
if err := os.WriteFile(etcFile, []byte(etcContent), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mainFile := filepath.Join(varStringDir, "main.go")
|
||||
return os.WriteFile(mainFile, []byte(mainContent), 0644)
|
||||
}
|
||||
18
tools/goctl/gateway/conf.yml
Normal file
18
tools/goctl/gateway/conf.yml
Normal file
@@ -0,0 +1,18 @@
|
||||
Name: gateway-example # gateway name
|
||||
Host: localhost # gateway host
|
||||
Port: 8888 # gateway port
|
||||
Upstreams: # upstreams
|
||||
- Grpc: # grpc upstream
|
||||
Target: 0.0.0.0:8080 # grpc target,the direct grpc server address,for only one node
|
||||
# Endpoints: [0.0.0.0:8080,192.168.120.1:8080] # grpc endpoints, the grpc server address list, for multiple nodes
|
||||
# Etcd: # etcd config, if you want to use etcd to discover the grpc server address
|
||||
# Hosts: [127.0.0.1:2378,127.0.0.1:2379] # etcd hosts
|
||||
# Key: greet.grpc # the discovery key
|
||||
# protoset mode
|
||||
ProtoSets:
|
||||
- hello.pb
|
||||
# Mappings can also be written in proto options
|
||||
# Mappings: # routes mapping
|
||||
# - Method: get
|
||||
# Path: /ping
|
||||
# RpcPath: hello.Hello/Ping
|
||||
20
tools/goctl/gateway/gateway.tpl
Normal file
20
tools/goctl/gateway/gateway.tpl
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/gateway"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/gateway.yaml", "config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c gateway.GatewayConf
|
||||
conf.MustLoad(*configFile, &c)
|
||||
gw := gateway.MustNewServer(c)
|
||||
defer gw.Stop()
|
||||
gw.Start()
|
||||
}
|
||||
62
tools/goctl/gateway/template.go
Normal file
62
tools/goctl/gateway/template.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
const (
|
||||
category = "gateway"
|
||||
etcTemplateFileFile = "etc.tpl"
|
||||
mainTemplateFile = "main.tpl"
|
||||
)
|
||||
|
||||
//go:embed conf.yml
|
||||
var etcTemplate string
|
||||
|
||||
//go:embed gateway.tpl
|
||||
var mainTemplate string
|
||||
|
||||
|
||||
var templates = map[string]string{
|
||||
etcTemplateFileFile: etcTemplate,
|
||||
mainTemplateFile: mainTemplate,
|
||||
}
|
||||
|
||||
// GenTemplates is the entry for command goctl template,
|
||||
// it will create the specified category
|
||||
func GenTemplates() error {
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
||||
// RevertTemplate restores the deleted template files
|
||||
func RevertTemplate(name string) error {
|
||||
content, ok := templates[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("%s: no such file name", name)
|
||||
}
|
||||
return pathx.CreateTemplate(category, name, content)
|
||||
}
|
||||
|
||||
// Clean deletes all template files
|
||||
func Clean() error {
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// Update is used to update the template files, it will delete the existing old templates at first,
|
||||
// and then create the latest template files
|
||||
func Update() error {
|
||||
err := Clean()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
||||
// Category returns a const string value for rpc template category
|
||||
func Category() string {
|
||||
return category
|
||||
}
|
||||
Reference in New Issue
Block a user