api add middleware support (#140)
* rebase upstream
* rebase
* trim no need line
* trim no need line
* trim no need line
* update doc
* remove update
* remove no need
* remove no need
* goctl add jwt support
* goctl add jwt support
* goctl add jwt support
* goctl support import
* goctl support import
* support return ()
* revert
* refactor and rename folder to group
* remove no need
* add anonymous annotation
* optimized
* rename
* rename
* update test
* api add middleware support: usage:
@server(
middleware: M1, M2
)
* api add middleware support: usage:
@server(
middleware: M1, M2
)
* simple logic
* should reverse middlewares
* optimized
* optimized
* rename
Co-authored-by: kingxt <dream4kingxt@163.com>
This commit is contained in:
@@ -9,16 +9,20 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
|
||||
const (
|
||||
contextFilename = "servicecontext.go"
|
||||
contextTemplate = `package svc
|
||||
|
||||
import {{.configImport}}
|
||||
import (
|
||||
{{.configImport}}
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config {{.config}}
|
||||
{{.middleware}}
|
||||
}
|
||||
|
||||
func NewServiceContext(c {{.config}}) *ServiceContext {
|
||||
@@ -53,12 +57,22 @@ func genServiceContext(dir string, api *spec.ApiSpec) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var middlewareStr string
|
||||
for _, item := range getMiddleware(api) {
|
||||
middlewareStr += fmt.Sprintf("%s rest.Middleware\n", item)
|
||||
}
|
||||
|
||||
var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
|
||||
if len(middlewareStr) > 0 {
|
||||
configImport += fmt.Sprintf("\n\"%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,
|
||||
})
|
||||
if err != nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user