Golang Context.Fail Examples

Golang Context.Fail - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.Fail extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
File: repos.go Project: smira/aptly
// POST /api/repos
func apiReposCreate(c *gin.Context) {
	var b struct {
		Name                string `binding:"required"`
		Comment             string
		DefaultDistribution string
		DefaultComponent    string
	}

	if !c.Bind(&b) {
		return
	}

	repo := deb.NewLocalRepo(b.Name, b.Comment)
	repo.DefaultComponent = b.DefaultComponent
	repo.DefaultDistribution = b.DefaultDistribution

	collection := context.CollectionFactory().LocalRepoCollection()
	collection.Lock()
	defer collection.Unlock()

	err := context.CollectionFactory().LocalRepoCollection().Add(repo)
	if err != nil {
		c.Fail(400, err)
		return
	}

	c.JSON(201, repo)
}
Example #2
0
//PUT /api/repos/:name
func editRepo(c *gin.Context) {
	var b struct {
		Comment             string
		DefaultDistribution string
		DefaultComponent    string
	}

	if !c.Bind(&b) {
		return
	}

	repo, err := api.RepoEdit(c.Params.ByName("name"), func(r *deb.LocalRepo) {
		if b.Comment != "" {
			r.Comment = b.Comment
		}
		if b.DefaultDistribution != "" {
			r.DefaultDistribution = b.DefaultDistribution
		}
		if b.DefaultComponent != "" {
			r.DefaultComponent = b.DefaultComponent
		}
	})
	if err != nil {
		c.Fail(500, err)
		return
	}
	c.JSON(200, repo)
}
// Creates a datafile
func (e EndpointContext) CreateDataFileEndpoint(c *gin.Context) {

	user := c.MustGet(MIDDLEWARE_KEY_USER).(User)
	db := c.MustGet(MIDDLEWARE_KEY_DB).(couch.Database)

	datafile := NewDatafile(e.Configuration)
	datafile.UserID = user.DocId()

	// bind the Datafile to the JSON request, which will bind the
	// url field or throw an error.
	if ok := c.Bind(&datafile); !ok {
		errMsg := fmt.Sprintf("Invalid datafile")
		c.Fail(400, errors.New(errMsg))
		return
	}

	logg.LogTo("REST", "datafile: %+v", datafile)

	// create a new Datafile object in db
	datafile, err := datafile.Save(db)
	if err != nil {
		errMsg := fmt.Sprintf("Error creating new datafile: %v", err)
		c.Fail(500, errors.New(errMsg))
		return
	}

	c.JSON(201, gin.H{"id": datafile.Id})

}
Example #4
0
// GET /publish
func apiPublishList(c *gin.Context) {
	localCollection := context.CollectionFactory().LocalRepoCollection()
	localCollection.RLock()
	defer localCollection.RUnlock()

	snapshotCollection := context.CollectionFactory().SnapshotCollection()
	snapshotCollection.RLock()
	defer snapshotCollection.RUnlock()

	collection := context.CollectionFactory().PublishedRepoCollection()
	collection.RLock()
	defer collection.RUnlock()

	result := make([]*deb.PublishedRepo, 0, collection.Len())

	err := collection.ForEach(func(repo *deb.PublishedRepo) error {
		err := collection.LoadComplete(repo, context.CollectionFactory())
		if err != nil {
			return err
		}

		result = append(result, repo)

		return nil
	})

	if err != nil {
		c.Fail(500, err)
		return
	}

	c.JSON(200, result)
}
Example #5
0
func publishRepoOrSnapshot(c *gin.Context) {
	prefix := c.Params.ByName("prefix")
	var b struct {
		SourceKind     string
		Sources        []data_api.PublishSource
		Distribution   string
		Label          string
		Origin         string
		ForceOverwrite bool
		Architectures  []string
		Signing        data_api.SigningOptions
	}

	if !c.Bind(&b) {
		return
	}
	//prefix, distribution, label, origin string, sourceKind string, sources []PublishSource,
	// forceOverwrite bool, architectures []string, signingOptions SigningOptions
	repo, err := api.PublishRepoOrSnapshot(prefix, b.Distribution, b.Label, b.Origin, b.SourceKind, b.Sources, b.ForceOverwrite,
		b.Architectures, b.Signing)
	if err != nil {
		c.Fail(400, err)
		return
	}
	c.JSON(200, repo)
}
Example #6
0
// GET /files/:dir
func apiFilesListFiles(c *gin.Context) {
	if !verifyDir(c) {
		return
	}

	list := []string{}
	root := filepath.Join(context.UploadPath(), c.Params.ByName("dir"))

	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if path == root {
			return nil
		}

		list = append(list, filepath.Base(path))

		return nil
	})

	if err != nil {
		if os.IsNotExist(err) {
			c.Fail(404, err)
		} else {
			c.Fail(500, err)
		}
		return
	}

	c.JSON(200, list)
}