代码拉取完成,页面将自动刷新
package handlers
import (
"context"
"fmt"
"gitee.com/guuzaa/microservices/product-api/data"
"net/http"
)
// MiddlewareValidateProduct validates the product in the request and calls next if ok
func (p Products) MiddlewareValidateProduct(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("Content-Type", "application/json")
prod := &data.Product{}
if err := data.FromJSON(prod, r.Body); err != nil {
p.l.Println("[ERROR] deserializing product", err)
http.Error(rw, "Error reading product", http.StatusBadRequest)
return
}
//validate the product
if err := p.v.Validate(prod); len(err) != 0 {
p.l.Println("[ERROR] validating product", err)
http.Error(
rw,
fmt.Sprintf("Error validating product: %s", err),
http.StatusBadRequest)
return
}
// add the product to the context
ctx := context.WithValue(r.Context(), KeyProduct{}, prod)
r = r.WithContext(ctx)
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(rw, r)
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。