【问题标题】:Golang http server return html or json depending on content typeGolang http 服务器根据内容类型返回 html 或 json
【发布时间】:2019-06-23 09:30:31
【问题描述】:

我正在尝试使用 gorilla mux 在 Golang 中编写简单的 RESTful 应用程序。 我写了几个如下所示的处理程序:

func getUser(w http.ResponseWriter, r *http.Request) {
    if r.Header.Get("Content-type") == "application/json" {
        w.Header().Set("Content-Type", "application/json")
        u, err := _getUser(r)
        if err != nil {
            http.NotFound(w, r)
            return
        }
        json.NewEncoder(w).Encode(u) //asked for json, return json
    } else {
        w.Header().Set("Content-Type", "text/html")
        u, err := _getUser(r)
        if err != nil {
            http.NotFound(w, r)
            return
        }
        renderTemplate(w, "view", u) // asked for html, return html
    }
}
func _getUser(r *http.Request) (*User, error) {
    params := mux.Vars(r)
    for _, u := range users {
        if u.ID == params["id"] {
            return &u, nil
        }
    }
    return nil, errors.New("")
}

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/v1/users/{id}", getUser).Methods("GET")
}

我遇到的问题是我有很多重复。每个 CRUD 方法都必须检查内容类型并返回 json 或 html。

我想过写一个闭包

func htmlOrJSON(fn func(http.ResponseWriter, *http.Request) (interface {}, error), templateName string) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        if r.Header.Get("Content-type") == "application/json" {
            w.Header().Set("Content-Type", "application/json")
            result, err := fn(w, r)
            if err != nil {
                http.NotFound(w, r)
                return
            }
            json.NewEncoder(w).Encode(result)
        } else {
            w.Header().Set("Content-Type", "text/html")
            result, err := fn(w, r)
            if err != nil {
                http.NotFound(w, r)
                return
            }
            renderTemplate(w, templateName, result)
        }
    }
}

// and use as:
router.HandleFunc("/v1/users/{id}", htmlOrJSON(getUser, "view")).Methods("GET")

要删除重复但它看起来也不太好。谁能帮我把这段代码弄得更干净?

【问题讨论】:

    标签: go closures code-duplication


    【解决方案1】:

    虽然这是一个代码审查问题,应该在 CodeReview 社区中,但我会尽力回答。

    编写一个处理 HTML 和 JSON 渲染的通用函数。即使您复制了一些代码,错误处理 IMO 也应该发生在每个处理程序上。它在那里更有意义,并使代码更具可读性和明确性。您很快就会发现还有其他需要特殊处理的错误。

    在逻辑上,大多数 API 接受查询参数http://api.com/user/1?fomtat=json。这更有意义,因为当客户端接受的内容类型超过您的内容类型时,您将陷入困境。

    const JSON = "application/json"
    
    func getUser(w http.ResponseWriter, r *http.Request) {
        u, err := _getUser(r)
        if err != nil {
            http.NotFound(w, r)
            return
        }
        responseBody(u, r.Header.Get("Content-type"), &w)
    }
    
    func responseBody(u User, contentType string, w io.writer) {
        switch contentType {
        case JSON:
            w.Header().Set("Content-Type", JSON)
            json.NewEncoder(w).Encode(u) //asked for json, return json
        default:
            w.Header().Set("Content-Type", "text/html")
            renderTemplate(w, "view", u) // asked for html, return html
        }
    }
    

    【讨论】:

    • 谢谢,但是如果我也想用这个方法传递一部分用户呢?将 User arg 更改为 interface{} 是个好主意吗?而且最好只发送contentType,而不是整个r?如果我发送 r,我就不必每次调用 responseBody 时都读取内容类型
    • 并且renderTemplate 也接收模板名称作为第二个参数。我应该让responseBody 函数接收模板名称作为参数吗?或者这可能太多了?
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多