Golang Context.AbortWithStatus Examples

Golang Context.AbortWithStatus - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.AbortWithStatus extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func AddRepository(c *gin.Context) {
	project := c.MustGet("project").(models.Project)

	var repository struct {
		Name     string `json:"name" binding:"required"`
		GitUrl   string `json:"git_url" binding:"required"`
		SshKeyID int    `json:"ssh_key_id" binding:"required"`
	}
	if err := c.Bind(&repository); err != nil {
		return
	}

	res, err := database.Mysql.Exec("insert into project__repository set project_id=?, git_url=?, ssh_key_id=?, name=?", project.ID, repository.GitUrl, repository.SshKeyID, repository.Name)
	if err != nil {
		panic(err)
	}

	insertID, _ := res.LastInsertId()
	insertIDInt := int(insertID)
	objType := "repository"

	desc := "Repository (" + repository.GitUrl + ") created"
	if err := (models.Event{
		ProjectID:   &project.ID,
		ObjectType:  &objType,
		ObjectID:    &insertIDInt,
		Description: &desc,
	}.Insert()); err != nil {
		panic(err)
	}

	c.AbortWithStatus(204)
}
Example #2
0
func EnvironmentMiddleware(c *gin.Context) {
	project := c.MustGet("project").(models.Project)
	envID, err := util.GetIntParam("environment_id", c)
	if err != nil {
		return
	}

	query, args, _ := squirrel.Select("*").
		From("project__environment").
		Where("project_id=?", project.ID).
		Where("id=?", envID).
		ToSql()

	var env models.Environment
	if err := database.Mysql.SelectOne(&env, query, args...); err != nil {
		if err == sql.ErrNoRows {
			c.AbortWithStatus(404)
			return
		}

		panic(err)
	}

	c.Set("environment", env)
	c.Next()
}
Example #3
0
func EditReviewFormR(c *gin.Context) {
	// dependency injection
	var (
		reviewStore = c.MustGet(ReviewStoreKey).(ReviewStore)
		user        = GetUser(c)
	)

	// only authenticated users can perform this action
	if !user.Authenticated() {
		c.AbortWithStatus(http.StatusUnauthorized)
		return
	}

	// fetch review
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	review, err := reviewStore.GetReview(id)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.HTML(http.StatusOK, "review-form.html", gin.H{
		"Action": "Edit",
		"User":   user,
		"Form":   review,
		"Errors": map[string]string{},
	})
}
Example #4
0
// openVPNHandler generate the openvpn config
func (r *openvpnAuthd) openVPNHandler(cx *gin.Context) {
	// step: grab the authentication headers
	emailAddress := cx.Request.Header.Get(r.config.AuthHeader)
	if emailAddress == "" {
		cx.AbortWithStatus(http.StatusForbidden)
		return
	}

	// step: generate a certificate for them
	cert, err := r.vault.GetCertificate(emailAddress, r.config.VaultPath, r.config.SessionDuration)
	if err != nil {
		glog.Errorf("failed to generate the certificate for openvpn account, reason: %s", err)
		cx.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	// step: template out the config
	cx.HTML(http.StatusOK, "openvpn.html", gin.H{
		"openvpn_servers": r.config.servers,
		"ttl":             cert.TTL,
		"expires_in":      time.Now().Add(cert.TTL),
		"certificate":     cert.Certificate,
		"private_key":     cert.PrivateKey,
		"issuing_ca":      cert.IssuingCA,
		"email":           emailAddress,
	})
}
Example #5
0
func InventoryMiddleware(c *gin.Context) {
	project := c.MustGet("project").(models.Project)
	inventoryID, err := util.GetIntParam("inventory_id", c)
	if err != nil {
		return
	}

	query, args, _ := squirrel.Select("*").
		From("project__inventory").
		Where("project_id=?", project.ID).
		Where("id=?", inventoryID).
		ToSql()

	var inventory models.Inventory
	if err := database.Mysql.SelectOne(&inventory, query, args...); err != nil {
		if err == sql.ErrNoRows {
			c.AbortWithStatus(404)
			return
		}

		panic(err)
	}

	c.Set("inventory", inventory)
	c.Next()
}
Example #6
0
func AddUser(c *gin.Context) {
	project := c.MustGet("project").(models.Project)
	var user struct {
		UserID int  `json:"user_id" binding:"required"`
		Admin  bool `json:"admin"`
	}

	if err := c.Bind(&user); err != nil {
		return
	}

	if _, err := database.Mysql.Exec("insert into project__user set user_id=?, project_id=?, admin=?", user.UserID, project.ID, user.Admin); err != nil {
		panic(err)
	}

	objType := "user"
	desc := "User ID " + strconv.Itoa(user.UserID) + " added to team"
	if err := (models.Event{
		ProjectID:   &project.ID,
		ObjectType:  &objType,
		ObjectID:    &user.UserID,
		Description: &desc,
	}.Insert()); err != nil {
		panic(err)
	}

	c.AbortWithStatus(204)
}