Template Modification

Scenario

Implement a uniformly formatted body accordingly, in the following format.

  1. {
  2. "code": 0,
  3. "msg": "OK",
  4. "data": {}//
  5. }

① 实际相应数据

[!TIP] The code generated by go-zero does not process it

Preparation

We go ahead and write a Response method in the response package under the project with module as greet, with a directory tree similar to the following.

  1. greet
  2. ├── response
  3. └── response.go
  4. └── xxx...

The code is as follows

  1. package response
  2. import (
  3. "net/http"
  4. "github.com/zeromicro/go-zero/rest/httpx"
  5. )
  6. type Body struct {
  7. Code int `json:"code"`
  8. Msg string `json:"msg"`
  9. Data interface{} `json:"data,omitempty"`
  10. }
  11. func Response(w http.ResponseWriter, resp interface{}, err error) {
  12. var body Body
  13. if err != nil {
  14. body.Code = -1
  15. body.Msg = err.Error()
  16. } else {
  17. body.Msg = "OK"
  18. body.Data = resp
  19. }
  20. httpx.OkJson(w, body)
  21. }

Modify the handler template

  1. $ vim ~/.goctl/api/handler.tpl

Replace the template with the following

  1. package handler
  2. import (
  3. "net/http"
  4. "greet/response"// ①
  5. {% raw %}
  6. {{.ImportPackages}}
  7. {% endraw %}
  8. )
  9. {% raw %}
  10. func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
  11. return func(w http.ResponseWriter, r *http.Request) {
  12. {{if .HasRequest}}var req types.{{.RequestType}}
  13. if err := httpx.Parse(r, &req); err != nil {
  14. httpx.Error(w, err)
  15. return
  16. }{{end}}
  17. l := logic.New{{.LogicType}}(r.Context(), ctx)
  18. {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}req{{end}})
  19. {{if .HasResp}}response.Response(w, resp, err){{else}}response.Response(w, nil, err){{end}}//②
  20. }
  21. }
  22. {% endraw %}

① Replace with your real response package name, for reference only

② Customized template content

[!TIP]

1.If there is no local ~/.goctl/api/handler.tpl file, you can initialize it with the template initialization command goctl template init

Comparison

  • Before

    1. func GreetHandler(ctx *svc.ServiceContext) http.HandlerFunc {
    2. return func(w http.ResponseWriter, r *http.Request) {
    3. var req types.Request
    4. if err := httpx.Parse(r, &req); err != nil {
    5. httpx.Error(w, err)
    6. return
    7. }
    8. l := logic.NewGreetLogic(r.Context(), ctx)
    9. resp, err := l.Greet(req)
    10. // The following content will be replaced by custom templates
    11. if err != nil {
    12. httpx.Error(w, err)
    13. } else {
    14. httpx.OkJson(w, resp)
    15. }
    16. }
    17. }
  • After

    1. func GreetHandler(ctx *svc.ServiceContext) http.HandlerFunc {
    2. return func(w http.ResponseWriter, r *http.Request) {
    3. var req types.Request
    4. if err := httpx.Parse(r, &req); err != nil {
    5. httpx.Error(w, err)
    6. return
    7. }
    8. l := logic.NewGreetLogic(r.Context(), ctx)
    9. resp, err := l.Greet(req)
    10. response.Response(w, resp, err)
    11. }
    12. }

Comparison of response body

  • Before

    1. {
    2. "message": "Hello go-zero!"
    3. }
  • After

    1. {
    2. "code": 0,
    3. "msg": "OK",
    4. "data": {
    5. "message": "Hello go-zero!"
    6. }
    7. }

Summary

This document only describes the process of customizing the template for the corresponding example of http, in addition to the following scenarios of customizing the template.

  • model layer adds kmq
  • model layer to generate the model instance of the option to be valid
  • http customize the corresponding format …