Golang Context.String Examples

Golang Context.String - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.String extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func UpdatePetWithFormHandler(c *gin.Context) {
	queryValues := c.Request.URL.Query()

	petId := queryValues.Get("petId")
	if petId == "" {
		c.JSON(http.StatusBadRequest, gin.H{"missing": "petId"})
		return
	}

	name := c.Request.PostFormValue("name")
	if name == "" {
		c.JSON(http.StatusBadRequest, gin.H{"missing": "name"})
		return
	}

	status := c.Request.PostFormValue("status")
	if status == "" {
		c.JSON(http.StatusBadRequest, gin.H{"missing": "status"})
		return
	}

	if err := operations.UpdatePetWithForm(petId, name, status); err == nil {
		c.String(http.StatusOK, "Success")
	} else {
		c.JSON(http.StatusBadRequest, err)
	}
}
// 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.String(400, errMsg)
		return
	}

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

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

	// job will get kicked off by changes listener

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

}
Example #3
0
func (ar *APIResource) Index(c *gin.Context) {
	if c.Request.Header.Get("Authorization") == "authenticated" {
		c.String(http.StatusOK, "Index")
	} else {
		c.String(http.StatusForbidden, "forbidden")
	}
}
Example #4
0
func (rc *ResourceController) GetResources(ctx *gin.Context) {
	req := ctx.Request
	resourceType := getResourceType(req.URL)

	logger.Log.WithFields(
		logrus.Fields{"resource type": resourceType}).Info("GetResources")

	resources := ptm_models.NewSliceForResourceName(resourceType, 0, 0)
	c := rc.Database().C(ptm_models.GetCollectionName(resourceType))
	// retrieve all documents in the collection
	// TODO Restrict this to resource type, just to be extra safe
	query := buildSearchQuery(resourceType, ctx)
	logger.Log.WithFields(
		logrus.Fields{"query": query}).Info("GetResources")
	err := c.Find(query).All(resources)
	if err != nil {
		if err == mgo.ErrNotFound {
			ctx.String(http.StatusNotFound, "Not Found")
			ctx.Abort()
			return
		} else {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
	}

	ctx.JSON(http.StatusOK, resources)
}
Example #5
0
func GetFeed(c *gin.Context) {
	user := session.User(c)
	remote := remote.FromContext(c)
	var repos []*model.RepoLite

	// get the repository list from the cache
	reposv, ok := c.Get("repos")
	if ok {
		repos = reposv.([]*model.RepoLite)
	} else {
		var err error
		repos, err = remote.Repos(user)
		if err != nil {
			c.String(400, err.Error())
			return
		}
	}

	feed, err := store.GetUserFeed(c, repos)
	if err != nil {
		c.String(400, err.Error())
		return
	}
	c.JSON(200, feed)
}
Example #6
0
func ShowIndex(c *gin.Context) {
	user := session.User(c)
	if user == nil {
		c.Redirect(http.StatusSeeOther, "/login")
		return
	}

	// get the repository list from the cache
	repos, err := cache.GetRepos(c, user)
	if err != nil {
		c.String(400, err.Error())
		return
	}

	// filter to only show the currently active ones
	activeRepos, err := store.GetRepoListOf(c, repos)
	if err != nil {
		c.String(400, err.Error())
		return
	}

	c.HTML(200, "index.html", gin.H{
		"User":  user,
		"Repos": activeRepos,
	})
}