本文整理汇总了Golang中github.com/gpmgo/switch/modules/middleware.Context.JSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.JSON方法的具体用法?Golang Context.JSON怎么用?Golang Context.JSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/gpmgo/switch/modules/middleware.Context
的用法示例。
在下文中一共展示了Context.JSON方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetRevision
func GetRevision(ctx *middleware.Context) {
importPath := archive.GetRootPath(ctx.Query("pkgname"))
rev := ctx.Query("revision")
n := archive.NewNode(importPath, rev)
if err := n.GetRevision(); err != nil {
ctx.JSON(422, map[string]interface{}{
"error": err.Error(),
})
return
}
ctx.JSON(200, map[string]interface{}{
"sha": n.Revision,
})
}
示例2: Download
func Download(ctx *middleware.Context) {
importPath := archive.GetRootPath(ctx.Query("pkgname"))
rev := ctx.Query("revision")
r, err := models.CheckPkg(importPath, rev)
if err != nil {
ctx.JSON(422, map[string]interface{}{
"error": err.Error(),
})
return
}
if err = models.IncreasePackageDownloadCount(importPath); err != nil {
ctx.JSON(500, map[string]interface{}{
"error": err.Error(),
})
return
} else if err = models.AddDownloader(ctx.RemoteAddr()); err != nil {
ctx.JSON(500, map[string]interface{}{
"error": err.Error(),
})
return
}
ext := archive.GetExtension(importPath)
serveName := path.Base(importPath) + "-" + base.ShortSha(r.Revision) + ext
switch r.Storage {
case models.LOCAL:
ctx.ServeFile(path.Join(setting.ArchivePath, importPath, r.Revision+ext), serveName)
case models.QINIU:
ctx.Redirect("http://" + setting.BucketUrl + "/" + importPath + "-" + r.Revision + ext)
}
}
示例3: ListLargeRevisions
func ListLargeRevisions(ctx *middleware.Context) {
revs, err := models.GetLocalRevisions()
if err != nil {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to get local revisions: %v", err),
})
return
}
largeRevs := make([]*ApiRevesion, 0, len(revs)/2)
for _, rev := range revs {
pkg, err := models.GetPakcageById(rev.PkgId)
if err != nil {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to get package by ID(%d): %v", rev.PkgId, err),
})
return
}
ext := archive.GetExtension(pkg.ImportPath)
localPath := path.Join(pkg.ImportPath, rev.Revision)
fpath := path.Join(setting.ArchivePath, localPath+ext)
if !com.IsFile(fpath) {
continue
}
// Check archive size.
f, err := os.Open(fpath)
if err != nil {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to open file(%s): %v", fpath, err),
})
return
}
fi, err := f.Stat()
if err != nil {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to get file info(%s): %v", fpath, err),
})
return
}
// Greater then MAX_UPLOAD_SIZE.
if fi.Size() > setting.MaxUploadSize<<20 {
largeRevs = append(largeRevs, &ApiRevesion{
Id: rev.Id,
Package: &ApiPackage{
Id: pkg.Id,
ImportPath: pkg.ImportPath,
Created: pkg.Created,
},
Revision: rev.Revision,
Size: fi.Size(),
Updated: rev.Updated,
})
continue
}
}
ctx.JSON(200, map[string]interface{}{
"revisions": &largeRevs,
})
}
示例4: BlockPackage
func BlockPackage(ctx *middleware.Context) {
id := ctx.QueryInt64("id")
pkg, err := models.GetPakcageById(id)
if err != nil {
if err == models.ErrPackageNotExist {
ctx.JSON(404, map[string]string{
"error": err.Error(),
})
} else {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to get package by ID(%d): %v", id, err),
})
}
return
}
revs, err := pkg.GetRevisions()
if err != nil {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to get package revisions by ID(%d): %v", id, err),
})
return
}
// Delete package archives.
ext := archive.GetExtension(pkg.ImportPath)
for _, rev := range revs {
switch rev.Storage {
case models.QINIU:
key := pkg.ImportPath + "-" + rev.Revision + ext
if err = qiniu.DeleteArchive(key); err != nil {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to delete archive(%s): %v", key, err),
})
return
}
}
}
os.RemoveAll(path.Join(setting.ArchivePath, pkg.ImportPath))
if err = models.BlockPackage(pkg, revs, ctx.Query("note")); err != nil {
ctx.JSON(500, map[string]string{
"error": fmt.Sprintf("fail to block package by ID(%d): %v", id, err),
})
return
}
ctx.JSON(200, map[string]interface{}{
"ok": true,
})
}