本文整理汇总了Golang中github.com/smallnewer/gogs/modules/middleware.Context.JSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.JSON方法的具体用法?Golang Context.JSON怎么用?Golang Context.JSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/smallnewer/gogs/modules/middleware.Context
的用法示例。
在下文中一共展示了Context.JSON方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: createRepo
func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoOption) {
repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
Name: opt.Name,
Description: opt.Description,
Gitignores: opt.Gitignores,
License: opt.License,
Readme: opt.Readme,
IsPrivate: opt.Private,
AutoInit: opt.AutoInit,
})
if err != nil {
if models.IsErrRepoAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.APIError(422, "", err)
} else {
if repo != nil {
if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil {
log.Error(4, "DeleteRepository: %v", err)
}
}
ctx.APIError(500, "CreateRepository", err)
}
return
}
ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true}))
}
示例2: ListMyRepos
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
func ListMyRepos(ctx *middleware.Context) {
ownRepos, err := models.GetRepositories(ctx.User.Id, true)
if err != nil {
ctx.APIError(500, "GetRepositories", err)
return
}
numOwnRepos := len(ownRepos)
accessibleRepos, err := ctx.User.GetAccessibleRepositories()
if err != nil {
ctx.APIError(500, "GetAccessibleRepositories", err)
return
}
repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
for i := range ownRepos {
repos[i] = ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
}
i := numOwnRepos
for repo, access := range accessibleRepos {
repos[i] = ToApiRepository(repo.Owner, repo, api.Permission{
Admin: access >= models.ACCESS_MODE_ADMIN,
Push: access >= models.ACCESS_MODE_WRITE,
Pull: true,
})
i++
}
ctx.JSON(200, &repos)
}
示例3: MembersAction
func MembersAction(ctx *middleware.Context) {
uid := com.StrTo(ctx.Query("uid")).MustInt64()
if uid == 0 {
ctx.Redirect(ctx.Org.OrgLink + "/members")
return
}
org := ctx.Org.Organization
var err error
switch ctx.Params(":action") {
case "private":
if ctx.User.Id != uid && !ctx.Org.IsOwner {
ctx.Error(404)
return
}
err = models.ChangeOrgUserStatus(org.Id, uid, false)
case "public":
if ctx.User.Id != uid {
ctx.Error(404)
return
}
err = models.ChangeOrgUserStatus(org.Id, uid, true)
case "remove":
if !ctx.Org.IsOwner {
ctx.Error(404)
return
}
err = org.RemoveMember(uid)
if models.IsErrLastOrgOwner(err) {
ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
ctx.Redirect(ctx.Org.OrgLink + "/members")
return
}
case "leave":
err = org.RemoveMember(ctx.User.Id)
if models.IsErrLastOrgOwner(err) {
ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
ctx.Redirect(ctx.Org.OrgLink + "/members")
return
}
}
if err != nil {
log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
ctx.JSON(200, map[string]interface{}{
"ok": false,
"err": err.Error(),
})
return
}
if ctx.Params(":action") != "leave" {
ctx.Redirect(ctx.Org.OrgLink + "/members")
} else {
ctx.Redirect(setting.AppSubUrl + "/")
}
}
示例4: DeleteWebhook
func DeleteWebhook(ctx *middleware.Context) {
if err := models.DeleteWebhook(ctx.QueryInt64("id")); err != nil {
ctx.Flash.Error("DeleteWebhook: " + err.Error())
} else {
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
}
ctx.JSON(200, map[string]interface{}{
"redirect": ctx.Org.OrgLink + "/settings/hooks",
})
}
示例5: DeleteDeployKey
func DeleteDeployKey(ctx *middleware.Context) {
if err := models.DeleteDeployKey(ctx.QueryInt64("id")); err != nil {
ctx.Flash.Error("DeleteDeployKey: " + err.Error())
} else {
ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
}
ctx.JSON(200, map[string]interface{}{
"redirect": ctx.Repo.RepoLink + "/settings/keys",
})
}
示例6: CreateAccessToken
// POST /users/:username/tokens
func CreateAccessToken(ctx *middleware.Context, form CreateAccessTokenForm) {
t := &models.AccessToken{
UID: ctx.User.Id,
Name: form.Name,
}
if err := models.NewAccessToken(t); err != nil {
ctx.APIError(500, "NewAccessToken", err)
return
}
ctx.JSON(201, &api.AccessToken{t.Name, t.Sha1})
}
示例7: ListAccessTokens
// GET /users/:username/tokens
func ListAccessTokens(ctx *middleware.Context) {
tokens, err := models.ListAccessTokens(ctx.User.Id)
if err != nil {
ctx.APIError(500, "ListAccessTokens", err)
return
}
apiTokens := make([]*api.AccessToken, len(tokens))
for i := range tokens {
apiTokens[i] = &api.AccessToken{tokens[i].Name, tokens[i].Sha1}
}
ctx.JSON(200, &apiTokens)
}
示例8: ListRepoHooks
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks
func ListRepoHooks(ctx *middleware.Context) {
hooks, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID)
if err != nil {
ctx.APIError(500, "GetWebhooksByRepoId", err)
return
}
apiHooks := make([]*api.Hook, len(hooks))
for i := range hooks {
apiHooks[i] = ToApiHook(ctx.Repo.RepoLink, hooks[i])
}
ctx.JSON(200, &apiHooks)
}
示例9: SearchRepos
func SearchRepos(ctx *middleware.Context) {
opt := models.SearchOption{
Keyword: path.Base(ctx.Query("q")),
Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
Limit: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opt.Limit == 0 {
opt.Limit = 10
}
// Check visibility.
if ctx.IsSigned && opt.Uid > 0 {
if ctx.User.Id == opt.Uid {
opt.Private = true
} else {
u, err := models.GetUserByID(opt.Uid)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) {
opt.Private = true
}
// FIXME: how about collaborators?
}
}
repos, err := models.SearchRepositoryByName(opt)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results := make([]*api.Repository, len(repos))
for i := range repos {
if err = repos[i].GetOwner(); err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results[i] = &api.Repository{
Id: repos[i].ID,
FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
}
}
ctx.JSON(200, map[string]interface{}{
"ok": true,
"data": results,
})
}
示例10: GetUserInfo
// GET /users/:username
func GetUserInfo(ctx *middleware.Context) {
u, err := models.GetUserByName(ctx.Params(":username"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.Error(404)
} else {
ctx.APIError(500, "GetUserByName", err)
}
return
}
// Hide user e-mail when API caller isn't signed in.
if !ctx.IsSigned {
u.Email = ""
}
ctx.JSON(200, &api.User{u.Id, u.Name, u.FullName, u.Email, u.AvatarLink()})
}
示例11: Action
func Action(ctx *middleware.Context) {
var err error
switch ctx.Params(":action") {
case "watch":
err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, true)
case "unwatch":
err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, false)
case "star":
err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, true)
case "unstar":
err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, false)
case "desc":
if !ctx.Repo.IsOwner() {
ctx.Error(404)
return
}
ctx.Repo.Repository.Description = ctx.Query("desc")
ctx.Repo.Repository.Website = ctx.Query("site")
err = models.UpdateRepository(ctx.Repo.Repository, false)
}
if err != nil {
log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
ctx.JSON(200, map[string]interface{}{
"ok": false,
"err": err.Error(),
})
return
}
redirectTo := ctx.Query("redirect_to")
if len(redirectTo) == 0 {
redirectTo = ctx.Repo.RepoLink
}
ctx.Redirect(redirectTo)
return
ctx.JSON(200, map[string]interface{}{
"ok": true,
})
}
示例12: DeleteUser
func DeleteUser(ctx *middleware.Context) {
u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
if err != nil {
ctx.Handle(500, "GetUserByID", err)
return
}
if err = models.DeleteUser(u); err != nil {
switch {
case models.IsErrUserOwnRepos(err):
ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo"))
ctx.JSON(200, map[string]interface{}{
"redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"),
})
case models.IsErrUserHasOrgs(err):
ctx.Flash.Error(ctx.Tr("admin.users.still_has_org"))
ctx.JSON(200, map[string]interface{}{
"redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"),
})
default:
ctx.Handle(500, "DeleteUser", err)
}
return
}
log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
ctx.Flash.Success(ctx.Tr("admin.users.deletion_success"))
ctx.JSON(200, map[string]interface{}{
"redirect": setting.AppSubUrl + "/admin/users",
})
}
示例13: SearchUsers
func SearchUsers(ctx *middleware.Context) {
opt := models.SearchOption{
Keyword: ctx.Query("q"),
Limit: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opt.Limit == 0 {
opt.Limit = 10
}
us, err := models.SearchUserByName(opt)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results := make([]*api.User, len(us))
for i := range us {
results[i] = &api.User{
ID: us[i].Id,
UserName: us[i].Name,
AvatarUrl: us[i].AvatarLink(),
FullName: us[i].FullName,
}
if ctx.IsSigned {
results[i].Email = us[i].Email
}
}
ctx.Render.JSON(200, map[string]interface{}{
"ok": true,
"data": results,
})
}
示例14: DeleteAuthSource
func DeleteAuthSource(ctx *middleware.Context) {
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
if err != nil {
ctx.Handle(500, "GetLoginSourceByID", err)
return
}
if err = models.DeleteSource(source); err != nil {
switch err {
case models.ErrAuthenticationUserUsed:
ctx.Flash.Error("form.still_own_user")
ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
default:
ctx.Handle(500, "DeleteSource", err)
}
return
}
log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
ctx.JSON(200, map[string]interface{}{
"redirect": setting.AppSubUrl + "/admin/auths",
})
}
示例15: EditRepoHook
// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) {
w, err := models.GetWebhookByID(ctx.ParamsInt64(":id"))
if err != nil {
ctx.APIError(500, "GetWebhookById", err)
return
}
if form.Config != nil {
if url, ok := form.Config["url"]; ok {
w.URL = url
}
if ct, ok := form.Config["content_type"]; ok {
if !models.IsValidHookContentType(ct) {
ctx.APIError(422, "", "Invalid content type")
return
}
w.ContentType = models.ToHookContentType(ct)
}
if w.HookTaskType == models.SLACK {
if channel, ok := form.Config["channel"]; ok {
meta, err := json.Marshal(&models.SlackMeta{
Channel: channel,
Username: form.Config["username"],
IconURL: form.Config["icon_url"],
Color: form.Config["color"],
})
if err != nil {
ctx.APIError(500, "slack: JSON marshal failed", err)
return
}
w.Meta = string(meta)
}
}
}
// Update events
if len(form.Events) == 0 {
form.Events = []string{"push"}
}
w.PushOnly = false
w.SendEverything = false
w.ChooseEvents = true
w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE))
w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH))
if err = w.UpdateEvent(); err != nil {
ctx.APIError(500, "UpdateEvent", err)
return
}
if form.Active != nil {
w.IsActive = *form.Active
}
if err := models.UpdateWebhook(w); err != nil {
ctx.APIError(500, "UpdateWebhook", err)
return
}
ctx.JSON(200, ToApiHook(ctx.Repo.RepoLink, w))
}