1 回答
TA贡献1893条经验 获得超10个赞
创建一个设置 mux var 的处理程序包装器:
type setVarWrapper struct {
fn func(http.ResponseWriter, *http.Request)
name string
value string
}
func (h *setVarWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mux.Vars(req)[h.name] = h.value
h.fn(w, r)
}
包装用户控制器:
router.Handle("/_Users, &setVarWrapper{fn:controller.AddResource, name:"resourceName", value:"_User"})
一个更简单的选择是编写一个 AddUser 函数来调用通用控制器:
function addUser(req *http.Request, w http.ResponseWriter){
mux.Vars(req)["resourceName"] = "_User"
controller.AddResource(w, req)
}
router.HandleFunc("/_Users", addUser)
也许更简单的是在处理程序中检查“”:
function AddResource(req *http.Request, w http.ResponseWriter){
vars := mux.Vars(req)
resourceName := vars["resourceName"]
if resourceName == "" {
resourceName = "_User"
}
data = request.Body
AddDataToResource(resourceName, data)
}
- 1 回答
- 0 关注
- 174 浏览
添加回答
举报