1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package gogen
- import (
- "fmt"
- "strings"
- "git.i2edu.net/i2/go-zero/tools/goctl/api/spec"
- "git.i2edu.net/i2/go-zero/tools/goctl/config"
- ctlutil "git.i2edu.net/i2/go-zero/tools/goctl/util"
- "git.i2edu.net/i2/go-zero/tools/goctl/util/format"
- "git.i2edu.net/i2/go-zero/tools/goctl/vars"
- )
- const (
- contextFilename = "service_context"
- contextTemplate = `package svc
- import (
- {{.configImport}}
- )
- type ServiceContext struct {
- Config {{.config}}
- {{.middleware}}
- }
- func NewServiceContext(c {{.config}}) *ServiceContext {
- return &ServiceContext{
- Config: c,
- {{.middlewareAssignment}}
- }
- }
- `
- )
- func genServiceContext(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
- filename, err := format.FileNamingFormat(cfg.NamingFormat, contextFilename)
- if err != nil {
- return err
- }
- authNames := getAuths(api)
- var auths []string
- for _, item := range authNames {
- auths = append(auths, fmt.Sprintf("%s config.AuthConfig", item))
- }
- var middlewareStr string
- var middlewareAssignment string
- middlewares := getMiddleware(api)
- 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"))
- }
- configImport := "\"" + ctlutil.JoinPackages(rootPkg, configDir) + "\""
- if len(middlewareStr) > 0 {
- configImport += "\n\t\"" + ctlutil.JoinPackages(rootPkg, middlewareDir) + "\""
- configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceURL)
- }
- 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,
- },
- })
- }
|