Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hz 自定义模板使用 #1067

Open
xuyang404 opened this issue Apr 30, 2024 · 9 comments
Open

hz 自定义模板使用 #1067

xuyang404 opened this issue Apr 30, 2024 · 9 comments
Assignees
Labels
Hertz question Further information is requested

Comments

@xuyang404
Copy link

layouts:
  # path 只表示 router.go 的模板,具体的路径由默认路径和 router_dir 决定
  - path: router.go
    delims:
      - '{{'
      - '}}'
    body: |-
      // Code generated by hertz generator. DO NOT EDIT.

      package {{$.PackageName}}
 

      import (
      	"github.com/cloudwego/hertz/pkg/app/server"

      	{{range $k, $v := .HandlerPackages}}{{$k}} "{{$v}}"{{end}}
      )

      /*
       This file will register all the routes of the services in the master idl.
       And it will update automatically when you use the "update" command for the idl.
       So don't modify the contents of the file, or your code will be deleted when it is updated.
       */

      {{define "g"}}
      {{- if eq .Path "/"}}r
      {{- else}}{{.GroupName}}{{end}}
      {{- end}}

      {{define "G"}}
      {{- if ne .Handler ""}}
      	{{- .GroupName}}.{{.HttpMethod}}("{{.Path}}", append({{.MiddleWare}}Mw(), {{.Handler}})...)
      {{- end}}
      {{- if ne (len .Children) 0}}
      {{.MiddleWare}} := {{template "g" .}}.Group("{{.Path}}", {{.MiddleWare}}Mw()...)
      {{- end}}
      {{- range $_, $router := .Children}}
      {{- if ne .Handler ""}}
      	{{template "G" $router}}
      {{- else}}
      	{	{{template "G" $router}}
      	}
      {{- end}}
      {{- end}}
      {{- end}}

      // Register register routes based on the IDL 'api.${HTTP Method}' annotation.
      func Register(r *server.Hertz) {
      {{template "G" .Router}}
      }
  # path 只表示 register.go 的模板,具体的路径由默认路径和 router_dir 决定
  - path: register.go
    delims:
      - ""
      - ""
    body: |-
      // Code generated by hertz generator. DO NOT EDIT.

      package {{.PackageName}}

      import (
      	"github.com/cloudwego/hertz/pkg/app/server"
      	{{$.DepPkgAlias}} "{{$.DepPkg}}"
      )

      // GeneratedRegister registers routers generated by IDL.
      func GeneratedRegister(r *server.Hertz){
      	//INSERT_POINT: DO NOT DELETE THIS LINE!
      	{{$.DepPkgAlias}}.Register(r)
      }

请问上面这个package.yaml的模板中,我想获取服务名该用什么变量呢?文档说的{{$.ServiceName}}用不了

@xuyang404 xuyang404 added the bug Something isn't working label Apr 30, 2024
@li-jin-gou
Copy link
Member

@li-jin-gou
Copy link
Member

image

看意思直接用就可以

@li-jin-gou li-jin-gou added question Further information is requested Hertz and removed bug Something isn't working labels May 2, 2024
@Skyenought
Copy link
Contributor

image
通过添加 -service 参数, 或指定全局渲染参数中的相关值即可

@xuyang404
Copy link
Author

image

看意思直接用就可以

package.yaml的router,go和register.go貌似没有这些参数

@xuyang404
Copy link
Author

image 通过添加 -service 参数, 或指定全局渲染参数中的相关值即可

package.yaml的router,go和register.go貌似没有全局参数

@Skyenought
Copy link
Contributor

@FGYFFFF 我发现 registry 好像是在一些方面写死了吗, 我在模版里添加的任何东西都无法被读取到

@Skyenought
Copy link
Contributor

image 通过添加 -service 参数, 或指定全局渲染参数中的相关值即可

package.yaml的router,go和register.go貌似没有全局参数

router.go 是可以读取到的, 我试了一下

@FGYFFFF
Copy link
Contributor

FGYFFFF commented May 6, 2024

package.yaml的router,go和register.go貌似没有全

文档这块写的有些问题, router.go(https://github.com/cloudwego/hertz/blob/develop/cmd/hz/generator/router.go#L31)/register.go(https://github.com/cloudwego/hertz/blob/develop/cmd/hz/generator/router.go#L298) 用的参数不是文档里的那些参数,是运行时构建的参数。
不推荐修改路由相关的模板

@Skyenought
Copy link
Contributor

@FGYFFFF 主要是我在 router.go 中修改模版是有效的, biz/route/register.go 感觉是干脆就不读取配置文件的 body 了

就是我发现 模版配置在 biz/router/register.go 好像是失效了?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hertz question Further information is requested
Development

No branches or pull requests

4 participants