當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.Fail方法代碼示例

本文整理匯總了Golang中github.com/gin-gonic/gin.Context.Fail方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Fail方法的具體用法?Golang Context.Fail怎麽用?Golang Context.Fail使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/gin-gonic/gin.Context的用法示例。


在下文中一共展示了Context.Fail方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: apiReposCreate

// 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)
}
開發者ID:smira,項目名稱:aptly,代碼行數:29,代碼來源:repos.go

示例2: editRepo

//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)
}
開發者ID:bsundsrud,項目名稱:slapt,代碼行數:29,代碼來源:repos.go

示例3: CreateDataFileEndpoint

// 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})

}
開發者ID:bryanyzhu,項目名稱:elastic-thought,代碼行數:30,代碼來源:endpoint_context.go

示例4: apiPublishList

// 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)
}
開發者ID:pombredanne,項目名稱:aptly,代碼行數:34,代碼來源:publish.go

示例5: publishRepoOrSnapshot

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)
}
開發者ID:bsundsrud,項目名稱:slapt,代碼行數:26,代碼來源:published.go

示例6: apiFilesListFiles

// 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)
}
開發者ID:liftup,項目名稱:aptly,代碼行數:34,代碼來源:files.go

示例7: apiPublishDrop

// DELETE /publish/:prefix/:distribution
func apiPublishDrop(c *gin.Context) {
	force := c.Request.URL.Query().Get("force") == "1"

	param := parseEscapedPath(c.Params.ByName("prefix"))
	storage, prefix := deb.ParsePrefix(param)
	distribution := c.Params.ByName("distribution")

	// published.LoadComplete would touch local repo collection
	localRepoCollection := context.CollectionFactory().LocalRepoCollection()
	localRepoCollection.RLock()
	defer localRepoCollection.RUnlock()

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

	err := collection.Remove(context, storage, prefix, distribution,
		context.CollectionFactory(), context.Progress(), force)
	if err != nil {
		c.Fail(500, fmt.Errorf("unable to drop: %s", err))
		return
	}

	c.JSON(200, gin.H{})
}
開發者ID:pombredanne,項目名稱:aptly,代碼行數:26,代碼來源:publish.go

示例8: SignUpload

func SignUpload(c *gin.Context) {
	user, err := GetUserFromContext(c)

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

	var json UploadJSON
	c.Bind(&json)

	key := `attachments/` + user.Id + `/` + uuid.NewUUID().String() + extensions[json.ContentType]

	f := &Form{
		Key:            key,
		ACL:            "public-read",
		AWSAccessKeyId: os.Getenv("AWS_ACCESS_KEY_ID"),
		CacheControl:   "max-age=31557600",
		ContentType:    json.ContentType,
	}

	f.build()

	href := "https://s3.amazonaws.com/" + os.Getenv("S3_BUCKET") + "/" + key

	c.JSON(200, gin.H{"form": f, "href": href})
}
開發者ID:blarralde,項目名稱:landline-api,代碼行數:26,代碼來源:upload_handler.go

示例9: mirrorEdit

func mirrorEdit(c *gin.Context) {
	var b struct {
		Filter         string
		FilterWithDeps bool
		Architectures  []string
		WithSources    bool
		WithUdebs      bool
	}

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

	mirror, err := api.MirrorEdit(c.Params.ByName("name"), func(m *deb.RemoteRepo) error {
		m.Filter = b.Filter
		m.FilterWithDeps = b.FilterWithDeps
		m.DownloadSources = b.WithSources
		m.DownloadUdebs = b.WithUdebs
		m.Architectures = b.Architectures
		return nil
	})
	if err != nil {
		c.Fail(400, err)
		return
	}

	c.JSON(200, mirror)

}
開發者ID:bsundsrud,項目名稱:slapt,代碼行數:29,代碼來源:mirrors.go

示例10: mirrorCreate

//POST /api/mirrors
func mirrorCreate(c *gin.Context) {
	var b struct {
		Name            string   `binding:"required"`
		ArchiveUrl      string   `binding:"required"`
		Distribution    string   `binding:"required"`
		Components      []string `binding:"required"`
		Filter          string
		FilterWithDeps  bool
		WithSources     bool
		WithUdebs       bool
		ForceComponents bool
	}

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

	//name, url, filter, distribution string, components []string, withSources, withUdebs, filterWithDeps, forceComponents bool
	repo, err := api.MirrorCreate(b.Name, b.ArchiveUrl, b.Filter, b.Distribution, b.Components, b.WithSources, b.WithUdebs, b.FilterWithDeps, b.ForceComponents)
	if err != nil {
		c.Fail(400, err)
		return
	}

	c.JSON(200, repo)
}
開發者ID:bsundsrud,項目名稱:slapt,代碼行數:27,代碼來源:mirrors.go

示例11: apiFilesListDirs

// GET /files
func apiFilesListDirs(c *gin.Context) {
	list := []string{}

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

		if path == context.UploadPath() {
			return nil
		}

		if info.IsDir() {
			list = append(list, filepath.Base(path))
			return filepath.SkipDir
		}

		return nil
	})

	if err != nil && !os.IsNotExist(err) {
		c.Fail(400, err)
		return
	}

	c.JSON(200, list)
}
開發者ID:liftup,項目名稱:aptly,代碼行數:28,代碼來源:files.go

示例12: apiSnapshotsCreateFromMirror

// POST /api/mirrors/:name/snapshots/
func apiSnapshotsCreateFromMirror(c *gin.Context) {
	var (
		err      error
		repo     *deb.RemoteRepo
		snapshot *deb.Snapshot
	)

	var b struct {
		Name        string `binding:"required"`
		Description string
	}

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

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

	snapshotCollection := context.CollectionFactory().SnapshotCollection()
	snapshotCollection.Lock()
	defer snapshotCollection.Unlock()

	repo, err = collection.ByName(c.Params.ByName("name"))
	if err != nil {
		c.Fail(404, err)
		return
	}

	err = repo.CheckLock()
	if err != nil {
		c.Fail(409, err)
		return
	}

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

	snapshot, err = deb.NewSnapshotFromRepository(b.Name, repo)
	if err != nil {
		c.Fail(400, err)
		return
	}

	if b.Description != "" {
		snapshot.Description = b.Description
	}

	err = snapshotCollection.Add(snapshot)
	if err != nil {
		c.Fail(400, err)
		return
	}

	c.JSON(201, snapshot)
}
開發者ID:liftup,項目名稱:aptly,代碼行數:61,代碼來源:snapshot.go

示例13: CreateTrainingJob

// Create Training Job
func (e EndpointContext) CreateTrainingJob(c *gin.Context) {

	// bind to json
	user := c.MustGet(MIDDLEWARE_KEY_USER).(User)
	db := c.MustGet(MIDDLEWARE_KEY_DB).(couch.Database)

	trainingJob := NewTrainingJob(e.Configuration)
	trainingJob.UserID = user.Id

	// bind the input struct to the JSON request
	if ok := c.Bind(trainingJob); !ok {
		errMsg := fmt.Sprintf("Invalid input")
		c.Fail(400, errors.New(errMsg))
		return
	}

	logg.LogTo("REST", "Create new TrainingJob: %+v", trainingJob)

	// save training job in db
	trainingJob, err := trainingJob.Insert(db)
	if err != nil {
		c.Fail(500, err)
		return
	}

	// job will get kicked off by changes listener

	// return solver object
	c.JSON(201, *trainingJob)

}
開發者ID:bryanyzhu,項目名稱:elastic-thought,代碼行數:32,代碼來源:endpoint_context.go

示例14: showRepo

//GET /api/repos/:name
func showRepo(c *gin.Context) {
	repo, err := api.RepoShow(c.Params.ByName("name"))
	if err != nil {
		c.Fail(404, err)
		return
	}
	c.JSON(200, repo)
}
開發者ID:bsundsrud,項目名稱:slapt,代碼行數:9,代碼來源:repos.go

示例15: db

/// Test 2: Single database query
func db(c *gin.Context) {
	var world World
	err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
	if err != nil {
		c.Fail(500, err)
	}
	c.JSON(200, &world)
}
開發者ID:pkdevbox,項目名稱:FrameworkBenchmarks,代碼行數:9,代碼來源:hello.go


注:本文中的github.com/gin-gonic/gin.Context.Fail方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。