Golang Context.AbortWithError Examples

Golang Context.AbortWithError - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.AbortWithError extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
File: menu.go Project: iguiyu/demo
func CreateMenu(c *gin.Context) {
	var btn1 menu.Button
	btn1.SetAsScanCodeWaitMsgButton("扫码", "park")
	var btn2 menu.Button
	btn2.SetAsLocationSelectButton("锁号", "number")
	var btn3 menu.Button
	btn3.SetAsClickButton("充值", "encharge")
	var btn4 menu.Button
	btn4.SetAsClickButton("我的", "my")

	btns := []menu.Button{btn1, btn2, btn3, btn4}
	var menu_ menu.Menu
	menu_.Buttons = btns
	err := menu.Create(global.WechatClient, &menu_)
	var wxMenu = &model.WXMenu{
		Id: bson.NewObjectId(),
		Menu: menu.Menu{
			Buttons: btns,
		},
	}

	log.Println("创建菜单 ", err)
	err = mgowrap.Save(wxMenu)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}
	log.Println("保存menu")
}
Example #2
0
func HomeR(c *gin.Context) {
	// dependency injection
	var (
		whiskyStore = c.MustGet(WhiskyStoreKey).(WhiskyStore)
		reviewStore = c.MustGet(ReviewStoreKey).(ReviewStore)
		user        = GetUser(c)
	)

	// temporary variables
	var (
		activity []Review
		trending []Whisky
		err      error
	)

	// get activity stream for user
	activity, err = reviewStore.GetActivity(user.ID, LIMIT)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	// get trending whiskies
	trending, err = whiskyStore.GetTrending(LIMIT)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.HTML(http.StatusOK, "home.html", gin.H{
		"User":     user,
		"Activity": activity,
		"Trending": trending,
	})
}
Example #3
0
func addonZip(ctx *gin.Context, user string, repository string, lastReleaseTag string) {
	release := getReleaseByTag(user, repository, lastReleaseTag)
	// if there a release with an asset that matches a addon zip, use it
	if release != nil {
		client := github.NewClient(nil)
		assets, _, _ := client.Repositories.ListReleaseAssets(user, repository, *release.ID, nil)
		platformStruct := xbmc.GetPlatform()
		platform := platformStruct.OS + "_" + platformStruct.Arch
		var assetAllPlatforms string
		for _, asset := range assets {
			if strings.HasSuffix(*asset.Name, platform+".zip") {
				assetPlatform := *asset.BrowserDownloadURL
				log.Info("Using release asset for " + platform + ": " + assetPlatform)
				ctx.Redirect(302, assetPlatform)
				return
			}
			if addonZipRE.MatchString(*asset.Name) {
				assetAllPlatforms = *asset.BrowserDownloadURL
				log.Info("Found all platforms release asset: " + assetAllPlatforms)
				continue
			}
		}
		if assetAllPlatforms != "" {
			log.Info("Using release asset for all platforms: " + assetAllPlatforms)
			ctx.Redirect(302, assetAllPlatforms)
			return
		}
	}
	ctx.AbortWithError(404, errors.New("Release asset not found."))
}
Example #4
0
func suggest(c *gin.Context) {

	cfg := getConfig(c)

	conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	for {

		movie, err := cfg.Store.GetRandom()

		if err != nil {
			continue
		}

		if err := conn.WriteJSON(movie); err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		time.Sleep(socketWaitFor)
	}

}
Example #5
0
func UserListR(c *gin.Context) {
	// dependency injection
	var (
		userStore = c.MustGet(UserStoreKey).(UserStore)
		user      = GetUser(c)
	)

	// get users
	list, err := userStore.GetAllUser()
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	// transform list into alphabet map
	users := map[string][]User{}
	for _, user := range list {
		group := strings.ToUpper(user.Name[0:1])
		users[group] = append(users[group], user)
	}

	c.HTML(http.StatusOK, "users.html", gin.H{
		"User":  user,
		"Users": users,
	})
}
Example #6
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)
}