本文整理匯總了Golang中github.com/MG-RAST/golib/stretchr/goweb/context.Context.PathValue方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.PathValue方法的具體用法?Golang Context.PathValue怎麽用?Golang Context.PathValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/MG-RAST/golib/stretchr/goweb/context.Context
的用法示例。
在下文中一共展示了Context.PathValue方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PreAuthRequest
func PreAuthRequest(ctx context.Context) {
id := ctx.PathValue("id")
if p, err := preauth.Load(id); err != nil {
err_msg := "err:@preAuth load: " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, 500, err_msg)
return
} else {
if n, err := node.LoadUnauth(p.NodeId); err == nil {
switch p.Type {
case "download":
filename := n.Id
if fn, has := p.Options["filename"]; has {
filename = fn
}
streamDownload(ctx, n, filename)
preauth.Delete(id)
return
default:
responder.RespondWithError(ctx, 500, "Preauthorization type not supported: "+p.Type)
}
} else {
err_msg := "err:@preAuth loadnode: " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, 500, err_msg)
}
}
return
}
示例2: AclRequest
// GET, POST, PUT, DELETE: /node/{nid}/acl/
// GET is the only action implemented here.
func AclRequest(ctx context.Context) {
nid := ctx.PathValue("nid")
rmeth := ctx.HttpRequest().Method
u, err := request.Authenticate(ctx.HttpRequest())
if err != nil && err.Error() != e.NoAuth {
request.AuthError(err, ctx)
return
}
// public user (no auth) can perform a GET operation with the proper node permissions
if u == nil {
if rmeth == "GET" && conf.ANON_READ {
u = &user.User{Uuid: "public"}
} else {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth)
return
}
}
// Load node by id
n, err := node.Load(nid)
if err != nil {
if err == mgo.ErrNotFound {
responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound)
return
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
err_msg := "[email protected]_Acl:LoadNode: " + nid + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg)
return
}
}
// Only the owner, an admin, or someone with read access can view acl's.
//
// NOTE: If the node is publicly owned, then anyone can view all acl's. The owner can only
// be "public" when anonymous node creation (ANON_WRITE) is enabled in Shock config.
rights := n.Acl.Check(u.Uuid)
if n.Acl.Owner != u.Uuid && u.Admin == false && n.Acl.Owner != "public" && rights["read"] == false {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth)
return
}
if rmeth == "GET" {
query := ctx.HttpRequest().URL.Query()
verbosity := ""
if _, ok := query["verbosity"]; ok {
verbosity = query.Get("verbosity")
}
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
} else {
responder.RespondWithError(ctx, http.StatusNotImplemented, "This request type is not implemented.")
}
return
}
示例3: AclTypedRequest
// GET, POST, PUT, DELETE: /node/{nid}/acl/{type}
func AclTypedRequest(ctx context.Context) {
nid := ctx.PathValue("nid")
rtype := ctx.PathValue("type")
rmeth := ctx.HttpRequest().Method
query := ctx.HttpRequest().URL.Query()
verbosity := ""
if _, ok := query["verbosity"]; ok {
verbosity = query.Get("verbosity")
}
u, err := request.Authenticate(ctx.HttpRequest())
if err != nil && err.Error() != e.NoAuth {
request.AuthError(err, ctx)
return
}
if !validAclTypes[rtype] {
responder.RespondWithError(ctx, http.StatusBadRequest, "Invalid acl type")
return
}
// Load node by id
n, err := node.Load(nid)
if err != nil {
if err == mgo.ErrNotFound {
responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound)
return
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
err_msg := "[email protected]_Acl:LoadNode: " + nid + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg)
return
}
}
// public user (no auth) can perform a GET operation given the proper node permissions
if u == nil {
rights := n.Acl.Check("public")
if rmeth == "GET" && conf.ANON_READ && (rights["read"] || n.Acl.Owner == "public") {
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
} else {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth)
return
}
}
// Users that are not an admin or the node owner can only delete themselves from an ACL.
if n.Acl.Owner != u.Uuid && u.Admin == false {
// Users that are not an admin or the node owner cannot remove public from ACL's.
if rtype == "public_read" || rtype == "public_write" || rtype == "public_delete" || rtype == "public_all" {
responder.RespondWithError(ctx, http.StatusBadRequest, "Users that are not node owners can only delete themselves from ACLs.")
return
}
// Parse user list
ids, err := parseAclRequestTyped(ctx)
if err != nil {
responder.RespondWithError(ctx, http.StatusBadRequest, err.Error())
return
}
if rmeth == "DELETE" {
if len(ids) != 1 || (len(ids) == 1 && ids[0] != u.Uuid) {
responder.RespondWithError(ctx, http.StatusBadRequest, "Users that are not node owners can delete only themselves from ACLs.")
return
}
if rtype == "owner" {
responder.RespondWithError(ctx, http.StatusBadRequest, "Deleting node ownership is not a supported request type.")
return
}
if rtype == "all" {
n.Acl.UnSet(ids[0], map[string]bool{"read": true, "write": true, "delete": true})
} else {
n.Acl.UnSet(ids[0], map[string]bool{rtype: true})
}
n.Save()
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
}
responder.RespondWithError(ctx, http.StatusBadRequest, "Users that are not node owners can only delete themselves from ACLs.")
return
}
// At this point we know we're dealing with an admin or the node owner.
// Admins and node owners can view/edit/delete ACLs
if rmeth == "GET" {
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
} else if rmeth == "POST" || rmeth == "PUT" {
if rtype == "public_read" || rtype == "public_write" || rtype == "public_delete" || rtype == "public_all" {
if rtype == "public_read" {
n.Acl.Set("public", map[string]bool{"read": true})
} else if rtype == "public_write" {
n.Acl.Set("public", map[string]bool{"write": true})
} else if rtype == "public_delete" {
n.Acl.Set("public", map[string]bool{"delete": true})
} else if rtype == "public_all" {
//.........這裏部分代碼省略.........
示例4: IndexTypedRequest
// GET, PUT, DELETE: /node/{nid}/index/{idxType}
func IndexTypedRequest(ctx context.Context) {
nid := ctx.PathValue("nid")
idxType := ctx.PathValue("idxType")
u, err := request.Authenticate(ctx.HttpRequest())
if err != nil && err.Error() != e.NoAuth {
request.AuthError(err, ctx)
return
}
// Fake public user
if u == nil {
u = &user.User{Uuid: ""}
}
// Load node and handle user unauthorized
n, err := node.Load(nid, u.Uuid)
if err != nil {
if err.Error() == e.UnAuth {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth)
return
} else if err.Error() == e.MongoDocNotFound {
responder.RespondWithError(ctx, http.StatusNotFound, "Node not found.")
return
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
err_msg := "[email protected]:LoadNode: " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg)
return
}
}
switch ctx.HttpRequest().Method {
case "DELETE":
rights := n.Acl.Check(u.Uuid)
if !rights["write"] {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth)
return
}
if _, has := n.Indexes[idxType]; has {
if err := n.DeleteIndex(idxType); err != nil {
err_msg := err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg)
}
responder.RespondOK(ctx)
} else {
responder.RespondWithError(ctx, http.StatusBadRequest, fmt.Sprintf("Node %s does not have index of type %s.", n.Id, idxType))
}
case "GET":
if v, has := n.Indexes[idxType]; has {
responder.RespondWithData(ctx, map[string]interface{}{idxType: v})
} else {
responder.RespondWithError(ctx, http.StatusBadRequest, fmt.Sprintf("Node %s does not have index of type %s.", n.Id, idxType))
}
case "PUT":
rights := n.Acl.Check(u.Uuid)
if !rights["write"] {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth)
return
}
// Gather query params
query := ctx.HttpRequest().URL.Query()
_, forceRebuild := query["force_rebuild"]
if _, has := n.Indexes[idxType]; has {
if idxType == "size" {
responder.RespondOK(ctx)
return
} else if !forceRebuild {
responder.RespondWithError(ctx, http.StatusBadRequest, "This index already exists, please add the parameter 'force_rebuild=1' to force a rebuild of the existing index.")
return
}
}
if !n.HasFile() {
responder.RespondWithError(ctx, http.StatusBadRequest, "Node has no file.")
return
} else if idxType == "" {
responder.RespondWithError(ctx, http.StatusBadRequest, "Index create requires type.")
return
}
if _, ok := index.Indexers[idxType]; !ok && idxType != "bai" && idxType != "subset" && idxType != "column" {
responder.RespondWithError(ctx, http.StatusBadRequest, fmt.Sprintf("Index type %s unavailable.", idxType))
return
}
if idxType == "size" {
responder.RespondWithError(ctx, http.StatusBadRequest, fmt.Sprintf("Index type size is a virtual index and does not require index building."))
return
}
if conf.Bool(conf.Conf["perf-log"]) {
logger.Perf("START indexing: " + nid)
//.........這裏部分代碼省略.........