【问题标题】:How to write http.HandleFunc() dynamically in golang?如何在golang中动态编写http.HandleFunc()?
【发布时间】:2018-08-18 15:36:58
【问题描述】:

我正在尝试编写简单的 http 服务器,它将为 API 提供请求。这是一个代码:

type Config struct {
    ListenPort int `json:"listenPort"`
    Requests   []struct {
        Request      string `json:"request"`
        ResponceFile string `json:"responceFile"`
    } `json:"requests"`
}
...

func main() {
    ...
    startServer(config)
}

func startServer(config Config) {
    http.HandleFunc(apiPrefix+config.Requests[0].Request,
        func(w http.ResponseWriter, r *http.Request) {
            var dataStruct interface{}
            err := loadJSON(config.Requests[0].ResponseFile, &dataStruct)
            if err != nil {
                w.Write([]byte("Oops! Something was wrong"))
            }
            data, _ := json.Marshal(dataStruct)
            w.Header().Set("Content-Type", "application/json")
            w.Write(data)
        })

    http.HandleFunc(apiPrefix+config.Requests[1].Request,
        func(w http.ResponseWriter, r *http.Request) {
            var dataStruct interface{}
            err := loadJSON(config.Requests[1].ResponseFile, &dataStruct)
            if err != nil {
                w.Write([]byte("Oops! Something was wrong"))
            }
            data, _ := json.Marshal(dataStruct)
            w.Header().Set("Content-Type", "application/json")
            w.Write(data)
        })

    http.HandleFunc("/", http.NotFound)

    port := ""
    if config.ListenPort != 0 {
        port = fmt.Sprintf(":%v", config.ListenPort)
    } else {
        port = ":8080"
    }

    fmt.Printf("Started @%v\n", port)
    log.Fatal(http.ListenAndServe(port, nil))
}

func loadJSON(filePath string, retStruct interface{}) error {
    fmt.Println(filePath)
    fileJSON, err := ioutil.ReadFile(filePath)
    json.Unmarshal(fileJSON, retStruct)
    return err
}

这是配置,其中描述了应通过特定请求返回的文件:

{
    "listenPort": 8080,
    "requests": [
        {
            "request": "switches/brocade",
            "responseFile": "switches.json"
        },
        {
            "request": "smth",
            "responseFile": "smth.json"
        }
    ]
}

所以问题是:为什么这段代码和上面的代码不一样?它只返回最后一个响应文件,在 config.json 中对此文件的所有请求进行了描述?或者编写动态定义的处理程序的正确方法是什么?

func startServer(config Config) {
    for _, req := config.Requests {
        http.HandleFunc(apiPrefix+req.Request,
            func(w http.ResponseWriter, r *http.Request) {
                var dataStruct interface{}
                err := loadJSON(req.ResponseFile, &dataStruct)
                if err != nil {
                    w.Write([]byte("Oops! Something was wrong"))
                }
                data, _ := json.Marshal(dataStruct)
                w.Header().Set("Content-Type", "application/json")
                w.Write(data)
            })
    }

    http.HandleFunc("/", http.NotFound)

【问题讨论】:

  • 也许你应该使用 http.Handle,每个请求路径的新结构,我认为 func 只有一个但 req 改变思想循环,所以它结束了最后一个 req

标签: api http go


【解决方案1】:

这是因为 Go 的 range 循环重用声明的变量 req

迭代变量可以由“range”子句使用 short variable declaration (:=) 的形式。在这种情况下,它们的类型是 设置为各自迭代值的类型,它们的scope 是 “for”语句块;它们在每次迭代中重复使用

(强调我的)

这种行为,加上您在 闭包 中捕获变量这一事实是所有处理程序都引用最后一个变量的值的原因。

函数字面量是闭包:它们可以引用定义在 一个周边功能。然后这些变量在 周围的函数和函数字面量,它们作为 只要它们可以访问。

要解决这个问题,您可以从循环内的迭代变量创建一个新变量,并让闭包使用它。

https://play.golang.org/p/GTNbf1eeFKV

【讨论】:

    【解决方案2】:

    您可以使用存储ResponseFilehttp.Handler 实现创建结构实例并将其传递给http.Handle

    type APIHandleFunc struct {
        ResponseFile string
    }
    
    func (api *APIHandleFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        http.ServeFile(w, r, api.ResponseFile)
    }
    
    // StartServer .
    func StartServer(config Config) {
        for _, req := range config.Requests {
            http.Handle(apiPrefix+"/"+req.Request, &APIHandleFunc{req.ResponceFile}/*new handler*/)
        }
        http.HandleFunc("/", http.NotFound)
    }
    

    或者只是通过传递ResponseFile 来创建http.HandlerFunc

    // create handlerFunc
    func getJSON(responseFile string) http.HandlerFunc {
        return func (w http.ResponseWriter, r *http.Request) {
            http.ServeFile(w, r, responseFile)
        }
    }
    // StartServer .
    func StartServer(config Config) {
        for _, req := range config.Requests {
            http.Handle(apiPrefix+"/"+req.Request, getJSON(req.ResponseFile))
        }
        http.HandleFunc("/", http.NotFound)
    }
    

    如果您只写 json 来响应,请使用 http.ServeFile

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-26
      • 2018-09-17
      • 2017-06-11
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      相关资源
      最近更新 更多