修改provider 实现
This commit is contained in:
parent
b40fc75627
commit
5e15551459
|
@ -1,8 +0,0 @@
|
|||
package user
|
||||
|
||||
const UserKey = "user"
|
||||
|
||||
type Service interface {
|
||||
// 请在这里定义你的方法
|
||||
Foo() string
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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 ""
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
driver: memory
|
|
@ -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{}
|
||||
}
|
||||
`
|
||||
|
|
|
@ -32,6 +32,9 @@ type App interface {
|
|||
// AppID 表示当前这个app的唯一id, 可以用于分布式锁等
|
||||
AppId() string
|
||||
|
||||
// HttpModuleFolder 表示业务层的模块路径
|
||||
HttpModuleFolder() string
|
||||
|
||||
LoadAppConfig(mapString map[string]string)
|
||||
|
||||
AppFolder() string
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue