|
|
@ -1,13 +1,10 @@
|
|
|
|
package gogen
|
|
|
|
package gogen
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
|
|
|
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
|
|
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
|
|
@ -33,7 +30,6 @@ func NewServiceContext(c {{.config}}) *ServiceContext {
|
|
|
|
{{.middlewareAssignment}}
|
|
|
|
{{.middlewareAssignment}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -43,15 +39,6 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fp, created, err := util.MaybeCreateFile(dir, contextDir, filename+".go")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !created {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var authNames = getAuths(api)
|
|
|
|
var authNames = getAuths(api)
|
|
|
|
var auths []string
|
|
|
|
var auths []string
|
|
|
|
for _, item := range authNames {
|
|
|
|
for _, item := range authNames {
|
|
|
@ -63,11 +50,6 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text, err := ctlutil.LoadTemplate(category, contextTemplateFile, contextTemplate)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var middlewareStr string
|
|
|
|
var middlewareStr string
|
|
|
|
var middlewareAssignment string
|
|
|
|
var middlewareAssignment string
|
|
|
|
var middlewares = getMiddleware(api)
|
|
|
|
var middlewares = getMiddleware(api)
|
|
|
@ -75,7 +57,8 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|
|
|
for _, item := range middlewares {
|
|
|
|
for _, item := range middlewares {
|
|
|
|
middlewareStr += fmt.Sprintf("%s rest.Middleware\n", item)
|
|
|
|
middlewareStr += fmt.Sprintf("%s rest.Middleware\n", item)
|
|
|
|
name := strings.TrimSuffix(item, "Middleware") + "Middleware"
|
|
|
|
name := strings.TrimSuffix(item, "Middleware") + "Middleware"
|
|
|
|
middlewareAssignment += fmt.Sprintf("%s: %s,\n", item, fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
|
|
|
|
middlewareAssignment += fmt.Sprintf("%s: %s,\n", item,
|
|
|
|
|
|
|
|
fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
|
|
|
|
var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
|
|
|
@ -84,19 +67,19 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|
|
|
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
|
|
|
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
t := template.Must(template.New("contextTemplate").Parse(text))
|
|
|
|
return genFile(fileGenConfig{
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
dir: dir,
|
|
|
|
err = t.Execute(buffer, map[string]string{
|
|
|
|
subdir: contextDir,
|
|
|
|
|
|
|
|
filename: filename + ".go",
|
|
|
|
|
|
|
|
templateName: "contextTemplate",
|
|
|
|
|
|
|
|
category: category,
|
|
|
|
|
|
|
|
templateFile: contextTemplateFile,
|
|
|
|
|
|
|
|
builtinTemplate: contextTemplate,
|
|
|
|
|
|
|
|
data: map[string]string{
|
|
|
|
"configImport": configImport,
|
|
|
|
"configImport": configImport,
|
|
|
|
"config": "config.Config",
|
|
|
|
"config": "config.Config",
|
|
|
|
"middleware": middlewareStr,
|
|
|
|
"middleware": middlewareStr,
|
|
|
|
"middlewareAssignment": middlewareAssignment,
|
|
|
|
"middlewareAssignment": middlewareAssignment,
|
|
|
|
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
formatCode := formatCode(buffer.String())
|
|
|
|
|
|
|
|
_, err = fp.WriteString(formatCode)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|