gen api svc add middleware implement temp code (#151)

master
sjatsh 4 years ago committed by GitHub
parent 099d44054d
commit db7dcaa120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -25,8 +25,20 @@ type ServiceContext struct {
} }
func NewServiceContext(c {{.config}}) *ServiceContext { func NewServiceContext(c {{.config}}) *ServiceContext {
return &ServiceContext{Config: c} return &ServiceContext{
Config: c,
{{.middlewareAssignment}}
}
}
{{.middlewareImplement}}
`
middlewareImplementCode = `func %s(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// TODO generate middleware implement function, delete after code implementation
}
} }
` `
) )
@ -57,21 +69,28 @@ func genServiceContext(dir string, api *spec.ApiSpec) error {
} }
var middlewareStr string var middlewareStr string
var middlewareAssignment string
var middlewareImplement string
for _, item := range getMiddleware(api) { for _, item := range getMiddleware(api) {
middlewareStr += fmt.Sprintf("%s rest.Middleware\n", item) middlewareStr += fmt.Sprintf("%s rest.Middleware\n", item)
middlewareAssignment += fmt.Sprintf("%s: %s,\n", item, item)
middlewareImplement += fmt.Sprintf(middlewareImplementCode, item)
} }
var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\"" var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
if len(middlewareStr) > 0 { if len(middlewareStr) > 0 {
configImport += fmt.Sprintf("\n\"%s/rest\"", vars.ProjectOpenSourceUrl) configImport += "\n\t\"net/http\""
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceUrl)
} }
t := template.Must(template.New("contextTemplate").Parse(text)) t := template.Must(template.New("contextTemplate").Parse(text))
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
err = t.Execute(buffer, map[string]string{ err = t.Execute(buffer, map[string]string{
"configImport": configImport, "configImport": configImport,
"config": "config.Config", "config": "config.Config",
"middleware": middlewareStr, "middleware": middlewareStr,
"middlewareAssignment": middlewareAssignment,
"middlewareImplement": middlewareImplement,
}) })
if err != nil { if err != nil {
return nil return nil

Loading…
Cancel
Save