cloudwego / cloudwego.github.io

Website for CloudWeGo
https://www.cloudwego.io
Apache License 2.0
126 stars 239 forks source link

hz 自定义模板使用 #1067

Open xuyang404 opened 2 months ago

xuyang404 commented 2 months ago
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}}用不了

li-jin-gou commented 2 months ago

参考 https://github.com/cloudwego/cwgo/blob/main/tpl/hertz/server/standard/layout.yaml#L515

li-jin-gou commented 2 months ago

image

看意思直接用就可以

Skyenought commented 2 months ago

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

xuyang404 commented 2 months ago

image

看意思直接用就可以

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

xuyang404 commented 2 months ago

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

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

Skyenought commented 2 months ago

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

Skyenought commented 2 months ago

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

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

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

FGYFFFF commented 2 months ago

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 commented 2 months ago

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

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