Golang Context.JSON Examples

Golang Context.JSON - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.JSON extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func (u *AccountHandler) sendLoginToken(c *gin.Context, player *ge.PlayerStruct) {
	if token, err := u.createLoginToken(player); err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
	} else {
		c.JSON(http.StatusOK, gin.H{"Token": token})
	}
}
Example #2
0
// ProfileToken displays the users token.
func ProfileToken(c *gin.Context) {
	record := session.Current(c)

	token := token.New(token.UserToken, record.Username)
	result, err := token.SignUnlimited(record.Hash)

	if err != nil {
		logrus.Warnf("Failed to generate token. %s", err)

		c.JSON(
			http.StatusInternalServerError,
			gin.H{
				"status":  http.StatusInternalServerError,
				"message": "Failed to generate token",
			},
		)

		c.Abort()
		return
	}

	c.JSON(
		http.StatusOK,
		result,
	)
}
Example #3
0
// UserOrgIndex retrieves all orgs related to a user.
func UserOrgIndex(c *gin.Context) {
	records, err := store.GetUserOrgs(
		c,
		&model.UserOrgParams{
			User: c.Param("user"),
		},
	)

	if err != nil {
		logrus.Warnf("Failed to fetch user orgs. %s", err)

		c.JSON(
			http.StatusInternalServerError,
			gin.H{
				"status":  http.StatusInternalServerError,
				"message": "Failed to fetch orgs",
			},
		)

		c.Abort()
		return
	}

	c.JSON(
		http.StatusOK,
		records,
	)
}
Example #4
0
func (m *MessagesController) addOrRemoveTag(ctx *gin.Context, messageIn *messageJSON, message models.Message, user models.User) {

	if !user.IsSystem {
		ctx.JSON(http.StatusForbidden, gin.H{"error": "Invalid Action for non-system user"})
		return
	}

	if messageIn.Text == "" {
		ctx.AbortWithError(http.StatusBadRequest, errors.New("Invalid Text for tag"))
		return
	}

	if messageIn.Action == "tag" {
		err := message.AddTag(messageIn.Text)
		if err != nil {
			log.Errorf("Error while adding a tag to a message %s", err)
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}
	} else if messageIn.Action == "untag" {
		err := message.RemoveTag(messageIn.Text)
		if err != nil {
			log.Errorf("Error while remove a tag from a message %s", err)
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}
	} else {
		ctx.AbortWithError(http.StatusBadRequest, errors.New("Invalid action : "+messageIn.Action))
		return
	}
	go models.WSMessage(&models.WSMessageJSON{Action: messageIn.Action, Username: user.Username, Message: message})
	ctx.JSON(http.StatusCreated, "")
}
Example #5
0
func GetUserInstance(c *gin.Context) {
	db := c.MustGet("db").(gorm.DB)
	consumer := c.MustGet("consumer").(models.User)

	id := c.Params.ByName("id")

	var user models.User

	db.First(&user, id)

	if user.Username == "" {
		response := make(map[string]string)
		response["error"] = "Resource not found."
		c.JSON(404, response)
	} else {
		if user.Id != consumer.Id {
			if user.Gravatar == "" {
				user.Gravatar = user.Email
			}
			user.Email = ""
		}
		user.Password = ""
		c.JSON(200, user)
	}
}
Example #6
0
func (page *Tenant) Get(c *gin.Context) {
	fmt.Println("Tenant.Get")
	tenants := make([]model.Tenant, 0)
	idstr := c.Query("id")
	//id:=c.Param("id")
	//fmt.Println("Params:",idstr)
	id, err := strconv.Atoi(idstr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "Params err"})
	}
	where := ""
	if id > 0 {
		where = "Id=" + idstr
	} else {
		where = "Id>0"
	}

	err = vars.Db.Where(where).Find(&tenants)
	if err != nil {
		log.Println(err)
	}
	//fmt.Println(tenants)
	//  tenant:=model.Tenant{Id:10,Name:"hefju520",Phone:"13929961332",Desc:"测试返回json"}
	c.JSON(http.StatusOK, tenants)
}