本文整理汇总了Golang中github.com/cloudwan/gohan/schema.Schema.GetActionURL方法的典型用法代码示例。如果您正苦于以下问题:Golang Schema.GetActionURL方法的具体用法?Golang Schema.GetActionURL怎么用?Golang Schema.GetActionURL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudwan/gohan/schema.Schema
的用法示例。
在下文中一共展示了Schema.GetActionURL方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MapRouteBySchema
//.........这里部分代码省略.........
func(w http.ResponseWriter, r *http.Request, p martini.Params, identityService middleware.IdentityService, context middleware.Context) {
addParamToQuery(r, schema.FormatParentID(s.Parent), p[s.Parent])
postPluralFunc(w, r, p, identityService, context)
})
//setup create or update route
putSingleFunc := func(w http.ResponseWriter, r *http.Request, p martini.Params, identityService middleware.IdentityService, context middleware.Context) {
addJSONContentTypeHeader(w)
fillInContext(context, dataStore, r, w, s, p, server.sync, identityService, server.queue)
id := p["id"]
dataMap, err := middleware.ReadJSON(r)
if err != nil {
handleError(w, resources.NewResourceError(err, fmt.Sprintf("Failed to parse data: %s", err), resources.WrongData))
return
}
dataMap = removeResourceWrapper(s, dataMap)
if isCreated, err := resources.CreateOrUpdateResource(
context, dataStore, identityService, s, id, dataMap); err != nil {
handleError(w, err)
return
} else if isCreated {
w.WriteHeader(http.StatusCreated)
}
routes.ServeJson(w, context["response"])
}
route.Put(singleURL, middleware.Authorization(schema.ActionUpdate), putSingleFunc)
route.Put(singleURLWithParents, middleware.Authorization(schema.ActionUpdate),
func(w http.ResponseWriter, r *http.Request, p martini.Params, identityService middleware.IdentityService, context middleware.Context) {
addParamToQuery(r, schema.FormatParentID(s.Parent), p[s.Parent])
putSingleFunc(w, r, p, identityService, context)
})
//setup update route
patchSingleFunc := func(w http.ResponseWriter, r *http.Request, p martini.Params, identityService middleware.IdentityService, context middleware.Context) {
addJSONContentTypeHeader(w)
fillInContext(context, dataStore, r, w, s, p, server.sync, identityService, server.queue)
id := p["id"]
dataMap, err := middleware.ReadJSON(r)
if err != nil {
handleError(w, resources.NewResourceError(err, fmt.Sprintf("Failed to parse data: %s", err), resources.WrongData))
return
}
dataMap = removeResourceWrapper(s, dataMap)
if err := resources.UpdateResource(
context, dataStore, identityService, s, id, dataMap); err != nil {
handleError(w, err)
return
}
routes.ServeJson(w, context["response"])
}
route.Patch(singleURL, middleware.Authorization(schema.ActionUpdate), patchSingleFunc)
route.Patch(singleURLWithParents, middleware.Authorization(schema.ActionUpdate),
func(w http.ResponseWriter, r *http.Request, p martini.Params, identityService middleware.IdentityService, context middleware.Context) {
addParamToQuery(r, schema.FormatParentID(s.Parent), p[s.Parent])
patchSingleFunc(w, r, p, identityService, context)
})
//Custom action support
for _, actionExt := range s.Actions {
action := actionExt
ActionFunc := func(w http.ResponseWriter, r *http.Request, p martini.Params,
identityService middleware.IdentityService, auth schema.Authorization, context middleware.Context) {
addJSONContentTypeHeader(w)
fillInContext(context, dataStore, r, w, s, p, server.sync, identityService, server.queue)
id := p["id"]
input := make(map[string]interface{})
if action.InputSchema != nil {
var err error
input, err = middleware.ReadJSON(r)
if err != nil {
handleError(w, resources.NewResourceError(err, fmt.Sprintf("Failed to parse data: %s", err), resources.WrongData))
return
}
}
// TODO use authorization middleware
manager := schema.GetManager()
path := r.URL.Path
policy, role := manager.PolicyValidate(action.ID, path, auth)
if policy == nil {
middleware.HTTPJSONError(w, fmt.Sprintf("No matching policy: %s %s %s", action, path, s.Actions), http.StatusUnauthorized)
return
}
context["policy"] = policy
context["tenant_id"] = auth.TenantID()
context["auth_token"] = auth.AuthToken()
context["role"] = role
context["catalog"] = auth.Catalog()
context["auth"] = auth
if err := resources.ActionResource(
context, dataStore, identityService, s, action, id, input); err != nil {
handleError(w, err)
return
}
routes.ServeJson(w, context["response"])
}
route.AddRoute(action.Method, s.GetActionURL(action.Path), ActionFunc)
}
}