本文整理匯總了Golang中github.com/pgpst/pgpst/internal/github.com/gin-gonic/gin.Context.Param方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Param方法的具體用法?Golang Context.Param怎麽用?Golang Context.Param使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pgpst/pgpst/internal/github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Param方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getAccountAddresses
func (a *API) getAccountAddresses(c *gin.Context) {
// Token and account from context
var (
ownAccount = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Resolve the ID from the URL
id := c.Param("id")
if id == "me" {
id = ownAccount.ID
}
// Check the scope
if id == ownAccount.ID {
if !models.InScope(token.Scope, []string{"addresses:read"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
} else {
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
}
// Get addresses from database
cursor, err := r.Table("addresses").GetAllByIndex("owner", id).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
defer cursor.Close()
var addresses []*models.Address
if err := cursor.All(&addresses); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
// Write the response
c.JSON(200, addresses)
return
}
示例2: readResource
func (a *API) readResource(c *gin.Context) {
// Get token and account info from the context
var (
account = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Resolve the resource ID and fetch it from database
id := c.Param("id")
cursor, err := r.Table("resources").Get(id).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
defer cursor.Close()
var resource *models.Resource
if err := cursor.All(&resource); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
if resource.Owner == account.ID {
// Check the scope
if !models.InScope(token.Scope, []string{"resources:read"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
} else {
// Check the scope
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
}
c.JSON(200, resource)
}
示例3: getAccountResources
func (a *API) getAccountResources(c *gin.Context) {
// Token and account from context
var (
ownAccount = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Resolve the ID from the URL
id := c.Param("id")
if id == "me" {
id = ownAccount.ID
}
// Check the scope
if id == ownAccount.ID {
if !models.InScope(token.Scope, []string{"resources:read"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
} else {
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
}
// 1. Owner filter
query := r.Table("resources").GetAllByIndex("owner", id)
// 2. Tag filter
if tagstr := c.Query("tags"); tagstr != "" {
tags := strings.Split(tagstr, ",")
// Cast to []interface{}
tagsi := []interface{}{}
for _, tag := range tags {
tagsi = append(tagsi, tag)
}
query = query.Filter(func(row r.Term) r.Term {
return row.Field("tags").Contains(tagsi...)
})
}
// 3. Meta filter
// not needed right now
// 4. Date created and date modified
ts := func(field string) error {
if dm := c.Query(field); dm != "" {
dmp := strings.Split(dm, ",")
if len(dmp) == 1 || dmp[1] == "" {
// parse dmp[0]
d0, err := time.Parse(time.RFC3339, dmp[0])
if err != nil {
return err
}
// after dmp[0]
query = query.Filter(func(row r.Term) r.Term {
return row.Field(field).Ge(d0)
})
} else {
// parse dmp[1]
d1, err := time.Parse(time.RFC3339, dmp[1])
if err != nil {
return err
}
if dmp[0] == "" {
// until dmp[1]
query = query.Filter(func(row r.Term) r.Term {
return row.Field(field).Le(d1)
})
} else {
// parse dmp[0]
d0, err := time.Parse(time.RFC3339, dmp[0])
if err != nil {
return err
}
// between dmp[0] and dmp[1]
query = query.Filter(func(row r.Term) r.Term {
return row.Field(field).Ge(d0).And(row.Field(field).Le(d1))
})
}
}
}
return nil
}
if err := ts("date_modified"); err != nil {
c.JSON(500, &gin.H{
"code": 0,
//.........這裏部分代碼省略.........
示例4: getAccountLabels
func (a *API) getAccountLabels(c *gin.Context) {
// Token and account from context
var (
ownAccount = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Resolve the ID from the URL
id := c.Param("id")
if id == "me" {
id = ownAccount.ID
}
// Check the scope
if id == ownAccount.ID {
if !models.InScope(token.Scope, []string{"labels:read"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
} else {
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
}
// Get labels from database
cursor, err := r.Table("labels").GetAllByIndex("owner", id).Map(func(label r.Term) r.Term {
return label.Merge(map[string]interface{}{
"total_threads": r.Table("threads").GetAllByIndex("labels", label.Field("id")).Count(),
"unread_threads": r.Table("threads").GetAllByIndex("labelsIsRead", []interface{}{
label.Field("id"),
false,
}).Count(),
})
}).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
defer cursor.Close()
var labels []struct {
models.Label
TotalThreads int `json:"total_threads" gorethink:"total_threads"`
UnreadThreads int `json:"unread_threads" gorethink:"unread_threads"`
}
if err := cursor.All(&labels); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
// Write the response
c.JSON(200, labels)
return
}
示例5: getLabelThreads
func (a *API) getLabelThreads(c *gin.Context) {
// Token and account from context
var (
account = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Resolve the ID from the URL
id := c.Param("id")
// Get label from the database
cursor, err := r.Table("labels").Get(id).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
var label *models.Label
if err := cursor.One(&label); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
// Check the ownership and scope
if label.Owner == account.ID {
if !models.InScope(token.Scope, []string{"labels:read"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
} else {
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
}
// Get threads from the database
cursor, err = r.Table("threads").GetAllByIndex("labels", label.ID).OrderBy(r.Desc("date_modified")).Map(func(thread r.Term) r.Term {
return thread.Merge(map[string]interface{}{
"manifest": r.Table("emails").GetAllByIndex("thread", thread.Field("id")).OrderBy("date_modified").CoerceTo("array"),
}).Do(func(thread r.Term) r.Term {
return r.Branch(
thread.Field("manifest").Count().Gt(0),
thread.Merge(map[string]interface{}{
"manifest": thread.Field("manifest").Nth(0).Field("manifest"),
}),
thread.Without("manifest"),
)
})
}).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
var threads []*extendedThread
if err := cursor.All(&threads); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
if threads == nil {
threads = []*extendedThread{}
}
// Write the response
c.JSON(200, threads)
return
}
示例6: updateAccount
func (a *API) updateAccount(c *gin.Context) {
// Get token and account info from the context
var (
account = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Decode the input
var input struct {
MainAddress string `json:"main_address"`
NewPassword []byte `json:"new_password"`
OldPassword []byte `json:"old_password"`
}
if err := c.Bind(&input); err != nil {
c.JSON(422, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
var newAddress *models.Address
if input.MainAddress != "" {
// Fetch address from the database
cursor, err := r.Table("addresses").Get(input.MainAddress).Default(map[string]interface{}{}).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
defer cursor.Close()
if err := cursor.One(&newAddress); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
// Verify that we got something
if newAddress.ID == "" {
c.JSON(422, &gin.H{
"code": 0,
"message": "No such address exists",
})
return
}
}
// Resolve the account ID and check scope accordingly
id := c.Param("id")
if id == "me" {
// Swap the ID to own account's ID
id = account.ID
}
if id == account.ID {
// Check the scope
if !models.InScope(token.Scope, []string{"account:modify"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
// Validate password
valid, _, err := account.VerifyPassword(input.OldPassword)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
if !valid {
c.JSON(401, &gin.H{
"code": 0,
"message": "Invalid password",
})
return
}
} else {
// Check the scope
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
// Fetch the account from the database
cursor, err := r.Table("accounts").Get(id).Default(map[string]interface{}{}).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
//.........這裏部分代碼省略.........
示例7: readAccount
func (a *API) readAccount(c *gin.Context) {
// Get token and account info from the context
var (
ownAccount = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Resolve the account ID and check scope accordingly
id := c.Param("id")
if id == "me" {
// Swap the ID to own account's ID
id = ownAccount.ID
}
if id == ownAccount.ID {
// Check the scope
if !models.InScope(token.Scope, []string{"account:read"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
// Get addresses from database
cursor, err := r.Table("addresses").GetAllByIndex("owner", id).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
defer cursor.Close()
var addresses []*models.Address
if err := cursor.All(&addresses); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
ownAccount.Password = nil
// Write the response
c.JSON(200, struct {
*models.Account
Addresses []*models.Address `json:"addresses"`
}{
Account: ownAccount,
Addresses: addresses,
})
return
}
// Check the scope
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
// Fetch the account
cursor, err := r.Table("accounts").Get(id).Do(func(account r.Term) r.Term {
return r.Branch(
account.Eq(nil),
map[string]interface{}{},
account.Without("password").Merge(map[string]interface{}{
"addresses": r.Table("addresses").GetAllByIndex("owner", account.Field("id")),
}),
)
}).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
defer cursor.Close()
var result struct {
models.Account
Addresses []*models.Address `json:"addresses"`
}
if err := cursor.One(&result); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"error": err.Error(),
})
return
}
if result.ID == "" {
c.JSON(404, &gin.H{
"code": 0,
"error": "Account not found",
})
return
//.........這裏部分代碼省略.........
示例8: readKey
func (a *API) readKey(c *gin.Context) {
// Resolve the ID param
id := c.Param("id")
if len(id) != 40 {
// Look up by email
cursor, err := r.Table("addresses").Get(id).Default(map[string]interface{}{}).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
defer cursor.Close()
var address *models.Address
if err := cursor.One(&address); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
// Check if we've found that address
if address.ID == "" {
c.JSON(404, &gin.H{
"code": 0,
"message": "Address not found",
})
return
}
// Set the ID accordingly
id = address.PublicKey
}
// Fetch the key from database
cursor, err := r.Table("keys").Get(id).Default(map[string]interface{}{}).Run(a.Rethink)
if err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
defer cursor.Close()
var key *models.Key
if err := cursor.One(&key); err != nil {
c.JSON(500, &gin.H{
"code": 0,
"message": err.Error(),
})
return
}
// Ensure that it exists, write the response
if key.ID == "" {
c.JSON(404, &gin.H{
"code": 0,
"message": "Key not found",
})
return
}
c.JSON(200, key)
}