Golang Context.Bind Examples

Golang Context.Bind - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.Bind extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func (tr *TodoResource) UpdateTodo(c *gin.Context) {
	id, err := tr.getId(c)
	if err != nil {
		c.JSON(400, api.NewError("problem decoding id sent"))
		return
	}

	var todo api.Todo

	if !c.Bind(&todo) {
		c.JSON(400, api.NewError("problem decoding body"))
		return
	}
	todo.Id = int32(id)

	var existing api.Todo

	if tr.db.First(&existing, id).RecordNotFound() {
		c.JSON(404, api.NewError("not found"))
	} else {
		tr.db.Save(&todo)
		c.JSON(200, todo)
	}

}
Example #2
0
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(),
	})
}
Example #3
0
func loginHandler(c *gin.Context) {
	var (
		login LoginData
	)
	// bind POST data to struct
	err := c.Bind(&login)
	if err != nil {
		c.JSON(401, "Invalid Credentials Provided")
	} else {
		// return 401 if empty
		if login.Username == "" || login.Password == "" {
			c.JSON(401, "Invalid Credentials Provided")
			return
		}

		// get user from database and fill our struct
		dbUserObject, err := validateUser(login.Username, login.Password)
		if err != nil {
			// return 401 if incorect user or password
			c.JSON(401, "Invalid Credentials")
			return
		}
		// generate token
		token := genToken(dbUserObject)

		// return token to user
		c.JSON(200, token)
		return
	}

	// return 400 if any other error is encountered
	c.JSON(400, "Error encountered")
}
Example #4
0
func saveCurrentChannelEndPoint(c *gin.Context) {
	var json = &struct {
		ID   int    `json:"ID"`
		Type string `json:"Type"`
	}{}

	if c.Bind(json) != nil {
		c.JSON(http.StatusBadRequest, gin.H{"ERR": "WRONG_INPUT"})
		return
	}

	userID64, _ := c.Get("userID")
	userID := userID64.(int)

	cc := &CurrentChannel{
		Type: json.Type,
		ID:   json.ID,
	}

	if peer, ok := peers.get(userID); ok {
		maxMsgId, _ := maxMsgIDofRoom(json.ID)
		cc.LastMsgID = maxMsgId
		peer.ccUpdate(cc)
	}

	if _, err := saveCurrentChannel(userID, cc); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"ERR": "INTERNAL_SERVER_ERROR",
		})
	} else {

		c.JSON(http.StatusOK, gin.H{"SUC": "OK"})
	}
}
Example #5
0
func configure(c *gin.Context) {
	var form conf.Conf
	var dat []byte
	var err error

	if err = c.Bind(&form); err == nil {
		errors := form.Validate()
		if len(errors) > 0 {
			c.JSON(http.StatusBadRequest, errors)
			return
		}
		if err = form.FillDefaults(); err != nil {
			fmt.Println("An error occured while filling default values :", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		if dat, err = yaml.Marshal(&form); err != nil {
			fmt.Println("An error occured while marshalling the yaml data :", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		if err = ioutil.WriteFile("conf.yml", dat, 0644); err != nil {
			fmt.Println("An error occured while writing the conf.yml file :", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
	} else {
		fmt.Println("An error occured while reading the form data :", err)
		c.JSON(http.StatusBadRequest, gin.H{})
		return
	}
	c.JSON(http.StatusCreated, gin.H{})
}
Example #6
0
func AddTemplate(c *gin.Context) {
	project := c.MustGet("project").(models.Project)

	var template models.Template
	if err := c.Bind(&template); err != nil {
		return
	}

	res, err := database.Mysql.Exec("insert into project__template set ssh_key_id=?, project_id=?, inventory_id=?, repository_id=?, environment_id=?, playbook=?, arguments=?, override_args=?", template.SshKeyID, project.ID, template.InventoryID, template.RepositoryID, template.EnvironmentID, template.Playbook, template.Arguments, template.OverrideArguments)
	if err != nil {
		panic(err)
	}

	insertID, err := res.LastInsertId()
	if err != nil {
		panic(err)
	}

	template.ID = int(insertID)

	objType := "template"
	desc := "Template ID " + strconv.Itoa(template.ID) + " created"
	if err := (models.Event{
		ProjectID:   &project.ID,
		ObjectType:  &objType,
		ObjectID:    &template.ID,
		Description: &desc,
	}.Insert()); err != nil {
		panic(err)
	}

	c.JSON(201, template)
}