• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang wombat.Context类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中code/minty/io/wombat.Context的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Context类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: ImageHandler

func ImageHandler(ctx wombat.Context, a *articles.Article, path, filename string) {
	ctx.Response.Header().Set("Content-Type", "application/json")

	// Save the image
	img, err := web.SaveImage(ctx.Request, ctx.Response, path, filename)
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
		return
	}

	s := img.Bounds().Size()
	exists := false
	// Add or Update the image
	for _, v := range a.Imgs {
		if v.Src == filename {
			v.W, v.H = s.X, s.Y
			exists = true
		}
	}
	if !exists {
		a.Imgs = append(a.Imgs, articles.Img{filename, "", s.X, s.Y})
	}

	// Update the article's images
	if err = a.SetImgs(a.Imgs); err != nil {
		log.Println("Failed to persit new image: ", filename, " for article: ", a.TitlePath)
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
	} else {
		j := fmt.Sprintf(`{"image":"%s", "w":%d,"h":%d}`, filename, s.X, s.Y)
		ctx.Response.Write([]byte(j))
	}
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:32,代码来源:handlers.go


示例2: ThumbHandler

func ThumbHandler(ctx wombat.Context, a *articles.Article, path, filename string) {
	ctx.Response.Header().Set("Content-Type", "application/json")

	// Save & resize the image to a thumbnail
	img, err := web.SaveImage(ctx.Request, ctx.Response, path, filename)
	if err == nil {
		// Resize the image and save it
		if img, err = imagery.ResizeWidth(img, 200); err == nil {
			err = imagery.WriteTo(web.ImageType(ctx.Request), img, path, filename)
		}
	}
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
		return
	}

	oldThumb := filepath.Join(path, a.TitlePath, a.Img.Src)
	s := img.Bounds().Size()

	// Update the article's thumbnail
	if err = a.SetImg(articles.Img{filename, filename, s.X, s.Y}); err != nil {
		log.Println("Failed to persit new thumbnail: ", filename, " for article: ", a.TitlePath)
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
	} else {
		os.Remove(oldThumb)
		j := fmt.Sprintf(`{"thumb":"%s", "w":%d,"h":%d}`, filename, s.X, s.Y)
		ctx.Response.Write([]byte(j))
	}
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:29,代码来源:handlers.go


示例3: JSONHandler

func JSONHandler(ctx wombat.Context, a *articles.Article, imagePath string, data []byte) {
	// Get the JSONMessage from the request
	var msg JSONMessage
	err := json.Unmarshal(data, &msg)
	if err != nil {
		ctx.HttpError(http.StatusBadRequest, GetError(ctx.Request, err))
		return
	}

	// Perform the given action
	switch msg.Action {
	default:
		// Invalid/missing action
		err = errors.New("Invalid Action")
	case "setSynopsis":
		err = a.SetSynopsis(msg.Data)
	case "setContent":
		err = a.SetContent(msg.Data)
	case "setActive":
		// Toggle
		err = a.Publish(!a.IsPublished)
	case "deleteImage":
		err = RemoveImage(a, msg.Data, imagePath)
	}

	// Report if the action resulted in an error
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
	}
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:30,代码来源:handlers.go


示例4: DeleteArticle

func (h Handler) DeleteArticle(ctx wombat.Context, titlePath string) {
	ctx.Response.Header().Set("Content-Type", "application/json")

	o, ok := h.Article(titlePath, true)
	if !ok {
		ctx.HttpError(http.StatusNotFound)
		return
	}

	a := h.ItoArticle(o)
	if err := a.Delete(); err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
	}
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:14,代码来源:handlers.go


示例5: PostArticles

func (h Handler) PostArticles(ctx wombat.Context) {
	title := ctx.FormValue("title")
	if request.IsApplicationJson(ctx.Request) {
		ctx.Response.Header().Set("Content-Type", "application/json")
		defer ctx.Body.Close()
		if b, err := ioutil.ReadAll(ctx.Body); err != nil {
			m := make(map[string]interface{})
			json.Unmarshal(b, &m)
			title, _ = m["title"].(string)
		}
	}

	if title == "" {
		// Missing title
		ctx.HttpError(http.StatusBadRequest, GetErrorStr(ctx.Request, "Missing title"))
		return
	}

	t, err := CreateArticle(ctx, title)
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
		return
	}

	// When not a JSON request issue redirect to new article
	if !request.IsApplicationJson(ctx.Request) {
		ctx.Redirect(fmt.Sprintf("%s/%s?view=edit", h.BasePath, t))
	}
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:29,代码来源:handlers.go


示例6: ImagesHandler

func ImagesHandler(ctx wombat.Context, a *articles.Article, imagePath string) {
	// Get the name of the image, or random name if missing/empty
	filename := ctx.FormValue("name")
	if filename == "" {
		filename = web.RandName(5)
	}

	// Save the thumbnail, or image
	path := filepath.Join(imagePath, a.TitlePath)
	if t := ctx.FormValue("type"); t == "thumb" {
		ThumbHandler(ctx, a, path, "thumb."+filename)
	} else {
		ImageHandler(ctx, a, path, filename)
	}
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:15,代码来源:handlers.go


示例7: GetArticle

/*----------Article-----------*/
func (h Handler) GetArticle(ctx wombat.Context, titlePath string) {
	isAdmin := ctx.User.IsAdmin()
	o, ok := h.Article(titlePath, isAdmin)
	if !ok {
		ctx.HttpError(http.StatusNotFound)
		return
	}

	tmpl := "view"
	if isAdmin && ctx.FormValue("view") == "edit" {
		tmpl = "edit"
	}

	// Handle HTTP/JSON response
	articleResponse(ctx, &h, o, tmpl, titlePath)
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:17,代码来源:handlers.go


示例8: GetArticles

/*----------Articles----------*/
func (h Handler) GetArticles(ctx wombat.Context) {
	var tmpl string
	var o interface{}

	switch view := ctx.FormValue("view"); {
	default:
		tmpl = "list"
		page, err := strconv.Atoi(ctx.FormValue("page"))
		if err != nil {
			page = 0
		}
		o, _ = h.articles.Recent(h.PageCount, page, ctx.User.IsAdmin())
	case view == "create" && ctx.User.IsAdmin():
		tmpl = "create"
	}

	// Handle HTTP/JSON response
	articleResponse(ctx, &h, o, tmpl, "")
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:20,代码来源:handlers.go


示例9: PutArticle

func (h Handler) PutArticle(ctx wombat.Context, titlePath string) {
	ctx.Response.Header().Set("Content-Type", "application/json")

	o, ok := h.Article(titlePath, true)
	if !ok {
		ctx.HttpError(http.StatusNotFound)
		return
	}
	a := h.ItoArticle(o)

	// JSON message
	if request.IsApplicationJson(ctx.Request) {
		// Get the bytes for JSON processing
		defer ctx.Body.Close()
		if data, err := ioutil.ReadAll(ctx.Body); err != nil {
			ctx.HttpError(http.StatusBadRequest, GetError(ctx.Request, err))
		} else {
			JSONHandler(ctx, a, h.ImagePath, data)
		}
	} else if IsImageRequest(ctx) {
		ImagesHandler(ctx, a, h.ImagePath)
	} else {
		// Nothing could be done for the given request
		ctx.HttpError(http.StatusBadRequest)
	}
}
开发者ID:juztin,项目名称:wombat-articles,代码行数:26,代码来源:handlers.go



注:本文中的code/minty/io/wombat.Context类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang proto.ColumnEncoding类代码示例发布时间:2022-05-24
下一篇:
Golang mgr.Connect函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap