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