本文整理汇总了Golang中github.com/gin-gonic/gin.Context.Query方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Query方法的具体用法?Golang Context.Query怎么用?Golang Context.Query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: executeAction
func executeAction(c *gin.Context) {
var args []string
for i := 0; i < 10; i++ {
operation := c.Query(strconv.Itoa(i))
if len(operation) > 0 {
args = append(args, operation)
} else {
break
}
}
fmt.Println(args)
//Turn LED Blue to Indicate event.
setRGB([]string{"0", "255", "255"})
time.Sleep(1 * time.Second)
statusCode := 200
out, err := exec.Command(command, args...).Output()
if err != nil {
statusCode = 500
//Flash LED red to indicate Error
setRGB([]string{"255", "0", "0"})
time.Sleep(1 * time.Second)
}
//Turn LED back to green
setRGB([]string{"0", "255", "0"})
c.JSON(statusCode, gin.H{
"status": "received",
"output": string(out[:]),
})
}
示例2: getMonitoringAction
func (pc *MonitoringController) getMonitoringAction(c *gin.Context) {
host := c.Query("host")
stat := c.DefaultQuery("stat", "all")
t := c.DefaultQuery("time", "last1800")
hosts, err := models.NodeMapper.FetchAll()
if err != nil {
panic(err)
}
var graphs models.Graphs
var target []string
if host != "" {
target = []string{host}
} else {
for _, h := range hosts {
target = append(target, h.Hostname)
}
}
if graphs, err = models.GraphMapper.FetchAll(target, stat, t); err != nil {
panic(err)
}
c.HTML(http.StatusOK, "monitoring_show.html", map[string]interface{}{
"host": host,
"stat": stat,
"time": t,
"hosts": hosts,
"stats": models.GraphStats,
"graphs": graphs,
})
}
示例3: delete
func delete(c *gin.Context) {
series := c.Query("series")
if series == "" {
c.JSON(http.StatusOK, gin.H{
"status": "error",
"message": "series need",
})
return
}
err := db.Batch(func(tx *bolt.Tx) error {
err := tx.DeleteBucket([]byte(series))
if err != nil {
return err
}
return nil
})
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": "error",
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
示例4: deployReplicasModify
func deployReplicasModify(ginCtx *gin.Context) {
name := ginCtx.Params.ByName("name")
num := ginCtx.Params.ByName("num")
force := ginCtx.Query("force")
fs := false
if force == "true" {
fs = true
}
glog.Info("scaling %s to %d instances", name, num)
replicas, err := strconv.Atoi(num)
if err != nil {
glog.Errorf("Could not change instances for %s, caused by %s", name, err)
ginCtx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
ginCtx.Error(err)
return
}
var beReq = &ScaleRequest{Name: name, Replicas: replicas, Force: fs}
_, err = se.Backend.Scale(beReq)
if err != nil {
glog.Errorf("Could not change instances for %s, caused by: %s", name, err.Error())
ginCtx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
ginCtx.Error(err)
return
}
ginCtx.JSON(http.StatusOK, gin.H{})
}
示例5: Post
func (this *CCEvent) Post(c *gin.Context) {
this.Load()
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,Token")
program_id := c.Query("program_id")
user_id := c.Query("created_by")
if user_id != "" {
_, err := userHandler.FetchUserById(user_id)
if err != nil {
fmt.Println("User id not exist: ")
panic(err)
return
}
}
if program_id != "" {
_, err := programHandler.FetchProgram(program_id)
if err != nil {
fmt.Println("Program id not exist: ")
panic(err)
return
}
}
err := eventHandler.AddEvent(c)
if err != nil {
fmt.Println("Data insertion failed ", err.Error())
return
}
fmt.Println("Data Inserted Successfully: ")
}
示例6: Show
func (ctr *AuthController) Show(c *gin.Context) {
email := c.Query("email")
c.JSON(200, gin.H{
"status": "posted",
"email": email,
})
}
示例7: GetCommit
func GetCommit(c *gin.Context) {
repo := session.Repo(c)
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
return repo.Hash, nil
})
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if parsed.Text != repo.FullName {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
commit := c.Param("sha")
branch := c.Query("branch")
if len(branch) == 0 {
branch = repo.Branch
}
build, err := store.GetBuildCommit(c, repo, commit, branch)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.JSON(http.StatusOK, build)
}
示例8: GetAPIDetails
// GetAPIDetails is the API handler for /api/details. Returns details specific
// to a video.
func GetAPIDetails(c *gin.Context) {
resp := gin.H{}
if c.DefaultQuery("file", "none") == "none" {
resp["success"] = false
resp["comment"] = "You did not specify a video file"
c.JSON(http.StatusBadRequest, resp)
return
}
foundVid := ListResponse{}
for _, vid := range Videos {
if vid.File == c.Query("file") {
foundVid = vid
break
}
}
if foundVid.File == "" {
resp["success"] = false
resp["comment"] = "no metadata found. nope. not at all. if it was in an easter egg. we know nothing about it. what's an easter egg?"
c.JSON(http.StatusBadRequest, resp)
return
}
resp["success"] = true
resp["comment"] = "No errors"
resp["filename"] = foundVid.File
resp["title"] = foundVid.Title
resp["source"] = foundVid.Source
resp["song"] = foundVid.Song
c.JSON(http.StatusOK, resp)
}
示例9: handleRequestBalance
func handleRequestBalance(context *gin.Context) {
user_id := context.Query("user")
if user_id == "" {
context.String(422, "User is empty!")
return
}
id, err := strconv.ParseUint(user_id, 10, 64)
if err != nil {
context.String(422, "User is not valid!")
return
}
var u User
db := context.MustGet("DBM").(*sql.DB)
tx, err := db.Begin()
checkErr(err)
defer tx.Rollback()
user := u.Get(id, tx)
if user.Id == 0 {
context.String(422, "User is not valid!")
return
}
tx.Commit()
context.JSON(200, gin.H{
"balance": user.Amount,
})
}
示例10: convertByURLHandler
// convertByURLHandler is the main v1 API handler for converting a HTML to a PDF
// via a GET request. It can either return a JSON string indicating that the
// output of the conversion has been uploaded or it can return the output of
// the conversion to the client (raw bytes).
func convertByURLHandler(c *gin.Context) {
s := c.MustGet("statsd").(*statsd.Client)
r, ravenOk := c.Get("sentry")
url := c.Query("url")
if url == "" {
c.AbortWithError(http.StatusBadRequest, ErrURLInvalid).SetType(gin.ErrorTypePublic)
s.Increment("invalid_url")
return
}
ext := c.Query("ext")
source, err := converter.NewConversionSource(url, nil, ext)
if err != nil {
s.Increment("conversion_error")
if ravenOk {
r.(*raven.Client).CaptureError(err, map[string]string{"url": url})
}
c.Error(err)
return
}
conversionHandler(c, *source)
}
示例11: convertByFileHandler
func convertByFileHandler(c *gin.Context) {
s := c.MustGet("statsd").(*statsd.Client)
r, ravenOk := c.Get("sentry")
file, header, err := c.Request.FormFile("file")
if err != nil {
c.AbortWithError(http.StatusBadRequest, ErrFileInvalid).SetType(gin.ErrorTypePublic)
s.Increment("invalid_file")
return
}
ext := c.Query("ext")
source, err := converter.NewConversionSource("", file, ext)
if err != nil {
s.Increment("conversion_error")
if ravenOk {
r.(*raven.Client).CaptureError(err, map[string]string{"url": header.Filename})
}
c.Error(err)
return
}
conversionHandler(c, *source)
}
示例12: ListPagingByCategory
func (p *postHandler) ListPagingByCategory(c *gin.Context) {
var posts []models.Post
var err error
categoryParam := c.Query("category")
if categoryParam == "" || !bson.IsObjectIdHex(categoryParam) {
p.ListPaging(c)
return
}
pageParam := c.DefaultQuery("page", "1")
pageIndex, err := strconv.Atoi(pageParam)
if err != nil {
pageIndex = 1
}
categoryId := bson.ObjectIdHex(categoryParam)
if posts, err = postResource.ListPagingByCategory(categoryId, pageIndex, ITEMS_PER_PAGE); err != nil {
if err != mgo.ErrNotFound {
log.LogError(c.Request, err, "Error in ListPagingByCategory Post", logger)
c.AbortWithError(500, err)
return
}
c.AbortWithStatus(404)
return
}
c.JSON(200, posts)
}
示例13: GetArticles
func (ctrl *Controller) GetArticles(c *gin.Context) {
id := repository.MaxArticleID
if c.Query("olderthan") != "" {
olderthan, err := strconv.Atoi(c.Query("olderthan"))
if err != nil {
ctrl.Logger.Log("msg", "Can't convert olderthan id to int", "err", err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
}
id = olderthan
}
articles, err := ctrl.Repository.FindArticlesOlderThan(id, ArticlesPerPage)
if err != nil {
if err == repository.ErrArticleNotFound {
ctrl.Logger.Log("msg", "Can't find olderthan article", "id", id)
status := http.StatusNotFound
c.JSON(status, gin.H{
"message": http.StatusText(status),
"status_code": status,
})
}
ctrl.Logger.Log("msg", "Failed to get olderthan articles", "id", id, "err", err)
c.AbortWithStatus(http.StatusInternalServerError)
}
c.JSON(http.StatusOK, marshaller.Articles(articles))
}
示例14: Home
func Home(c *gin.Context) {
ac := getAppConfig(c)
hostPortList := make([]string, 0, 100)
for k, _ := range ac.Instances {
hostPortList = append(hostPortList, k)
}
instanceID := c.Query("instance")
if _, ok := ac.Instances[instanceID]; ok == false {
instanceID = hostPortList[0]
}
targetSource := ac.Instances[instanceID].Source
infoErr := ""
hasInfoErr := false
statsInfo, err := getStatsInfo(targetSource)
if err != nil {
infoErr = err.Error()
hasInfoErr = true
}
structedStatsInfo := statsMap2Struct(statsInfo)
structedStatsInfo.InstanceID = instanceID
structedStatsInfo.Source = targetSource
c.HTML(http.StatusOK, "index.html", gin.H{
"HasInfoErr": hasInfoErr,
"InfoErr": infoErr,
"Instances": ac.Instances,
"StatsInfo": structedStatsInfo,
})
}
示例15: ParseRGB
func ParseRGB(c *gin.Context) (colorful.Color, error) {
rgb := map[string]float64{
"r": 0,
"g": 0,
"b": 0,
}
var err error
for k, v := range rgb {
if v, err = strconv.ParseFloat(c.Query(k), 64); err != nil {
err = errors.New("failed to parse color")
return colorful.Color{}, err
}
if v < 0 || v > 255 {
err = errors.New("invalid color value. must be <= 0 or >= 255")
return colorful.Color{}, err
}
rgb[k] = v
}
color := colorful.Color{
rgb["r"] / 255.0,
rgb["g"] / 255.0,
rgb["b"] / 255.0,
}
return color, nil
}