本文整理匯總了Golang中github.com/gin-gonic/gin.Context.IndentedJSON方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.IndentedJSON方法的具體用法?Golang Context.IndentedJSON怎麽用?Golang Context.IndentedJSON使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.IndentedJSON方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PatchUser
func PatchUser(c *gin.Context) {
me := session.User(c)
in := &model.User{}
err := c.Bind(in)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
user, err := store.GetUserLogin(c, c.Param("login"))
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
user.Admin = in.Admin
user.Active = in.Active
// cannot update self
if me.ID == user.ID {
c.AbortWithStatus(http.StatusForbidden)
return
}
err = store.UpdateUser(c, user)
if err != nil {
c.AbortWithStatus(http.StatusConflict)
return
}
c.IndentedJSON(http.StatusOK, user)
}
示例2: Handler
// Handler is a route handler for '/movie.:format' route
func Handler(c *gin.Context) {
requestType := c.Param("format")
term := c.Request.PostFormValue("text")
log.Printf("Movie Query: %s", term)
movie, err := lookupMovie(term)
switch requestType {
case "json":
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"Response": err.Error()})
} else {
c.IndentedJSON(http.StatusOK, movie)
}
case "slack":
text, err := formatSlackResp(movie)
if err != nil {
c.String(http.StatusNotFound, "Not Found")
}
c.String(http.StatusOK, text)
default:
c.JSON(http.StatusUnsupportedMediaType, nil)
}
}
示例3: processCommentHook
func processCommentHook(c *gin.Context, hook *model.Hook) {
repo, user, err := getRepoAndUser(c, hook.Repo.Slug)
if err != nil {
return
}
config, maintainer, err := getConfigAndMaintainers(c, user, repo)
if err != nil {
return
}
approvers, err := buildApprovers(c, user, repo, config, maintainer, hook.Issue)
if err != nil {
return
}
approved := len(approvers) >= config.Approvals
err = remote.SetStatus(c, user, repo, hook.Issue.Number, approved)
if err != nil {
log.Errorf("Error setting status for %s pr %d. %s", repo.Slug, hook.Issue.Number, err)
c.String(500, "Error setting status. %s.", err)
return
}
log.Debugf("processed comment for %s. received %d of %d approvals", repo.Slug, len(approvers), config.Approvals)
c.IndentedJSON(200, gin.H{
"approvers": maintainer.People,
"settings": config,
"approved": approved,
"approved_by": approvers,
})
}
示例4: GetBuild
func GetBuild(c *gin.Context) {
if c.Param("number") == "latest" {
GetBuildLast(c)
return
}
repo := session.Repo(c)
num, err := strconv.Atoi(c.Param("number"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
build, err := store.GetBuildNumber(c, repo, num)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
jobs, _ := store.GetJobList(c, build)
out := struct {
*model.Build
Jobs []*model.Job `json:"jobs"`
}{build, jobs}
c.IndentedJSON(http.StatusOK, &out)
}
示例5: GetRepos
func GetRepos(c *gin.Context) {
user := session.User(c)
remote := remote.FromContext(c)
var repos []*model.RepoLite
// get the repository list from the cache
reposv, ok := c.Get("repos")
if ok {
repos = reposv.([]*model.RepoLite)
} else {
var err error
repos, err = remote.Repos(user)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
}
// for each repository in the remote system we get
// the intersection of those repostiories in Drone
repos_, err := store.GetRepoListOf(c, repos)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Set("repos", repos)
c.IndentedJSON(http.StatusOK, repos_)
}
示例6: processPRHook
func processPRHook(c *gin.Context, prHook *model.PRHook) {
repo, user, err := getRepoAndUser(c, prHook.Repo.Slug)
if err != nil {
return
}
err = remote.SetStatus(c, user, repo, prHook.Number, false)
if err != nil {
log.Errorf("Error setting status. %s", err)
c.String(500, "Error setting status. %s", err)
return
}
config, _, err := getConfigAndMaintainers(c, user, repo)
if err != nil {
return
}
if prHook.Update && config.DoComment {
err = remote.WriteComment(c, user, repo, prHook.Number, "The Pull Request has been updated. No comments before this one will count for approval.")
if err != nil {
log.Errorf("Error writing comment for status. %s", err)
c.String(500, "Error writing comment for status. %s", err)
return
}
}
c.IndentedJSON(200, gin.H{
"number": prHook.Number,
"approved": false,
})
}
示例7: PostsHandler
// PostsHandler is a route handler for '/producthunt/posts.:format' route
func PostsHandler(c *gin.Context) {
requestType := c.Param("format")
daysAgo, err := strconv.Atoi(c.Request.PostFormValue("text"))
if err != nil {
daysAgo = 0
}
log.Printf("Days Ago: %d", daysAgo)
posts, err := getPosts(daysAgo)
switch requestType {
case "json":
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"Response": err.Error()})
} else {
c.IndentedJSON(http.StatusOK, posts)
}
case "slack":
text, err := formatPostsSlackResp(posts)
if err != nil {
c.String(http.StatusNotFound, "Not Found")
}
c.String(http.StatusOK, text)
default:
c.JSON(http.StatusUnsupportedMediaType, nil)
}
}
示例8: GetLoginToken
func GetLoginToken(c *gin.Context) {
remote := remote.FromContext(c)
in := &tokenPayload{}
err := c.Bind(in)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
login, err := remote.Auth(in.Access, in.Refresh)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
}
user, err := store.GetUserLogin(c, login)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
exp := time.Now().Add(time.Hour * 72).Unix()
token := token.New(token.SessToken, user.Login)
tokenstr, err := token.SignExpires(user.Hash, exp)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.IndentedJSON(http.StatusOK, &tokenPayload{
Access: tokenstr,
Expires: exp - time.Now().Unix(),
})
}
示例9: GetSensorData
// return data from a sensor in json format
// This function call reduceData!
func (s *Sensorserver) GetSensorData(c *gin.Context) {
// maximum Timestamp is MaxInt32
// fetchLastData return all records, if duration is
// also MaxInt32
// -> MaxInt32 - MaxInt32 = 0 -> get all data since 01.0.1.1970
const duration int = math.MaxInt32
pretty := c.Request.URL.Query().Get("pretty")
sensor := c.Param("name")
data, err := s.fetchLastData(sensor, duration)
if err != nil {
if pretty == "true" {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
} else {
if pretty == "true" {
c.IndentedJSON(http.StatusOK, s.reduceData(data))
} else {
c.JSON(http.StatusOK, s.reduceData(data))
}
}
}
示例10: DeleteRequest
func DeleteRequest(c *gin.Context) {
message := "success"
// if err := RequestCollection.Remove(bson.M{"_id": bson.ObjectIdHex(c.Param("id"))}); err != nil {
if err := RequestCollection.RemoveId(bson.ObjectIdHex(c.Param("id"))); err != nil {
message = PrepareMessage(err)
}
c.IndentedJSON(200, gin.H{"message": message})
}
示例11: GetUsers
func GetUsers(c *gin.Context) {
users, err := store.GetUserList(c)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.IndentedJSON(http.StatusOK, users)
}
示例12: GetUser
func GetUser(c *gin.Context) {
user, err := store.GetUserLogin(c, c.Param("login"))
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.IndentedJSON(http.StatusOK, user)
}
示例13: GetBuilds
func GetBuilds(c *gin.Context) {
repo := session.Repo(c)
builds, err := store.GetBuildList(c, repo)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.IndentedJSON(http.StatusOK, builds)
}
示例14: GetRequest
func GetRequest(c *gin.Context) {
request := &Request{}
httpCode := 200
message := ""
if err := RequestCollection.FindId(bson.ObjectIdHex(c.Param("id"))).One(request); err != nil {
request = nil
httpCode = 404
message = PrepareMessage(err)
}
c.IndentedJSON(httpCode, gin.H{"data": request, "message": message})
}
示例15: GetRemoteRepos
func GetRemoteRepos(c *gin.Context) {
user := session.User(c)
repos, err := cache.GetRepos(c, user)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.IndentedJSON(http.StatusOK, repos)
}