From 5e15551459c12f65625c6d43d064fa95a2505669 Mon Sep 17 00:00:00 2001 From: dandan <1033719135@qq.com> Date: Sat, 26 Oct 2024 10:43:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9provider=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/provider/user/contract.go | 8 -- app/provider/user/provider.go | 31 ------ app/provider/user/service.go | 16 --- config/development/cache.yaml | 1 + framework/command/provider.go | 167 +++++++++++++++++++++++++++++- framework/contract/app.go | 3 + framework/provider/app/service.go | 7 +- 7 files changed, 175 insertions(+), 58 deletions(-) delete mode 100644 app/provider/user/contract.go delete mode 100644 app/provider/user/provider.go delete mode 100644 app/provider/user/service.go create mode 100644 config/development/cache.yaml diff --git a/app/provider/user/contract.go b/app/provider/user/contract.go deleted file mode 100644 index 4c2f650..0000000 --- a/app/provider/user/contract.go +++ /dev/null @@ -1,8 +0,0 @@ -package user - -const UserKey = "user" - -type Service interface { - // 请在这里定义你的方法 - Foo() string -} diff --git a/app/provider/user/provider.go b/app/provider/user/provider.go deleted file mode 100644 index 5aca166..0000000 --- a/app/provider/user/provider.go +++ /dev/null @@ -1,31 +0,0 @@ -package user - -import ( - "github.com/Superdanda/hade/framework" -) - -type UserProvider struct { - framework.ServiceProvider - - c framework.Container -} - -func (sp *UserProvider) Name() string { - return UserKey -} - -func (sp *UserProvider) Register(c framework.Container) framework.NewInstance { - return NewUserService -} - -func (sp *UserProvider) IsDefer() bool { - return false -} - -func (sp *UserProvider) Params(c framework.Container) []interface{} { - return []interface{}{c} -} - -func (sp *UserProvider) Boot(c framework.Container) error { - return nil -} diff --git a/app/provider/user/service.go b/app/provider/user/service.go deleted file mode 100644 index cb4c62c..0000000 --- a/app/provider/user/service.go +++ /dev/null @@ -1,16 +0,0 @@ -package user - -import "github.com/Superdanda/hade/framework" - -type UserService struct { - container framework.Container -} - -func NewUserService(params ...interface{}) (interface{}, error) { - container := params[0].(framework.Container) - return &UserService{container: container}, nil -} - -func (s *UserService) Foo() string { - return "" -} diff --git a/config/development/cache.yaml b/config/development/cache.yaml new file mode 100644 index 0000000..6f6f71a --- /dev/null +++ b/config/development/cache.yaml @@ -0,0 +1 @@ +driver: memory diff --git a/framework/command/provider.go b/framework/command/provider.go index 01d8efb..037fbc1 100644 --- a/framework/command/provider.go +++ b/framework/command/provider.go @@ -55,8 +55,8 @@ var providerCreateCommand = &cobra.Command{ RunE: func(c *cobra.Command, args []string) error { container := c.GetContainer() fmt.Println("创建一个服务") - var name string - var folder string + var name, folder string + var interfaceNames []string { prompt := &survey.Input{ Message: "请输入服务名称(服务凭证):", @@ -101,6 +101,36 @@ var providerCreateCommand = &cobra.Command{ return nil } + // Keep asking for interface names until the user presses Enter to stop + for { + prompt := &survey.Input{ + Message: "请输入接口名称(直接按回车结束输入):", + } + + var input string + err := survey.AskOne(prompt, &input) + if err != nil { + return err + } + + // If the user presses Enter without input, break the loop + if strings.TrimSpace(input) == "" { + fmt.Println("接口输入结束") + break + } + + // Add the valid interface name to the slice + interfaceNames = append(interfaceNames, input) + fmt.Printf("已添加接口:%s\n", input) + } + + // Print all the interface names added by the user + if len(interfaceNames) > 0 { + fmt.Println("已添加的接口名称:", interfaceNames) + } else { + fmt.Println("未添加任何接口") + } + // 开始创建文件 if err := os.Mkdir(filepath.Join(pFolder, folder), 0700); err != nil { return err @@ -110,6 +140,7 @@ var providerCreateCommand = &cobra.Command{ data := map[string]interface{}{ "appName": config.GetAppName(), "packageName": name, + "interfaces": interfaceNames, } // 创建title这个模版方法 funcs := template.FuncMap{"title": strings.Title} @@ -152,6 +183,88 @@ var providerCreateCommand = &cobra.Command{ return err } } + + moduleFolder := app.HttpModuleFolder() + pModuleFolder := filepath.Join(moduleFolder, name) + util.EnsureDir(pModuleFolder) + { + // module 目录下 创建 服务包 + + // 创建api 我呢见 + { + // 创建api.go + file := filepath.Join(pModuleFolder, "api.go") + f, err := os.Create(file) + if err != nil { + return err + } + t := template.Must(template.New("api").Funcs(funcs).Parse(apiTmp)) + if err := t.Execute(f, data); err != nil { + return err + } + } + + // 创建api_controller文件 + { + tmpl, err := template.New("controller").Funcs(funcs).Parse(apiControllerTmp) + if err != nil { + fmt.Println("Failed to parse template:", err) + return err + } + // Generate a file for each interface + for _, interfaceName := range interfaceNames { + data := map[string]interface{}{ + "packageName": name, + "appName": config.GetAppName(), + "interfaceName": interfaceName, + } + + // Create the output file + filePath := filepath.Join(pModuleFolder, "api_"+interfaceName+".go") + file, err := os.Create(filePath) + if err != nil { + fmt.Println("Failed to create file:", err) + return errors.New("Failed to create file: " + filePath) + } + defer file.Close() + + // Execute the template with the data + if err := tmpl.Execute(file, data); err != nil { + fmt.Println("Failed to execute template:", err) + return errors.New("Failed to execute template: " + filePath) + } + fmt.Printf("Generated file: %s\n", filePath) + } + } + + //创建 dto 文件 + { + // 创建dto.go + file := filepath.Join(pModuleFolder, "dto.go") + f, err := os.Create(file) + if err != nil { + return err + } + t := template.Must(template.New("dto").Funcs(funcs).Parse(dtoTmp)) + if err := t.Execute(f, data); err != nil { + return err + } + } + + //创建 mapper 文件 + { + // 创建mapper.go + file := filepath.Join(pModuleFolder, "mapper.go") + f, err := os.Create(file) + if err != nil { + return err + } + t := template.Must(template.New("mapper").Funcs(funcs).Parse(mapperTmp)) + if err := t.Execute(f, data); err != nil { + return err + } + } + } fmt.Println("创建服务成功, 文件夹地址:", filepath.Join(pFolder, folder)) fmt.Println("请不要忘记挂载新创建的服务") return nil @@ -219,3 +332,53 @@ func (s *{{.packageName | title}}Service) Foo() string { return "" } ` + +var apiTmp = `package {{.packageName}} +import ( + "github.com/Superdanda/hade/framework/gin" +) + +type {{.packageName | title}}Api struct{} + +func RegisterRoutes(r *gin.Engine) error { + api := {{.packageName | title}}Api{} + + if !r.IsBind({{.packageName}}.{{.packageName | title}}Key) { + r.Bind(&{{.packageName}}.{{.packageName | title}}Provider{}) + } + + {{.packageName}}Group := r.Group("/{{.packageName}}") + { + {{range .interfaces}} + // {{.}} route + {{$.packageName}}Group.POST("/{{.}}", api.{{. | title}}) + {{end}} + } + return nil +} +` + +var apiControllerTmp = `package {{.packageName}} +import ( + "github.com/Superdanda/hade/framework/gin" +) + +// {{.interfaceName | title}} handler +func (api *{{.packageName | title}}Api) {{.interfaceName | title}}(c *gin.Context) { + // TODO: Add logic for {{.interfaceName}} +} +` + +var dtoTmp = `package {{.packageName}} + +type {{.packageName | title}}DTO struct {} +` +var mapperTmp = `package {{.packageName}} + +func Convert{{.packageName | title}}ToDTO({{.packageName}} *{{.packageName}}.{{.packageName | title}}) *{{.packageName | title}}DTO { + if {{.packageName}} == nil { + return nil + } + return &{{.packageName | title}}DTO{} +} +` diff --git a/framework/contract/app.go b/framework/contract/app.go index 1bf5ec6..45b9371 100644 --- a/framework/contract/app.go +++ b/framework/contract/app.go @@ -32,6 +32,9 @@ type App interface { // AppID 表示当前这个app的唯一id, 可以用于分布式锁等 AppId() string + // HttpModuleFolder 表示业务层的模块路径 + HttpModuleFolder() string + LoadAppConfig(mapString map[string]string) AppFolder() string diff --git a/framework/provider/app/service.go b/framework/provider/app/service.go index e32f912..5308373 100644 --- a/framework/provider/app/service.go +++ b/framework/provider/app/service.go @@ -83,7 +83,7 @@ func (h HadeApp) TestFolder() string { } func (h HadeApp) HttpFolder() string { - return filepath.Join(h.BaseFolder(), "http") + return filepath.Join(h.AppFolder(), "http") } func (h HadeApp) ConsoleFolder() string { @@ -100,6 +100,11 @@ func (h HadeApp) DeployFolder() string { return deployFolder } +func (h HadeApp) HttpModuleFolder() string { + deployFolder := filepath.Join(h.HttpFolder(), "module") + return deployFolder +} + func (h HadeApp) AppId() string { return h.appId }