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


Golang Context.ContentType方法代碼示例

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


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

示例1: getFile

//Handles file retrieval. It uses getURL to send a hash to the urlHandler, and listens on
//sendURL for the proper filename.
func getFile(ctx *web.Context, file string) string {
	dir := file[0:3]
	fname := file[3:]
	path := "files/" + dir + "/" + fname
	//Open the file
	f, err := os.Open(path)
	if err != nil {
		return "Error reading file!\n"
	}

	//Get MIME
	r, err := ioutil.ReadAll(f)
	if err != nil {
		return "Error reading file!\n"
	}
	mime := http.DetectContentType(r)

	_, err = f.Seek(0, 0)
	if err != nil {
		return "Error reading the file\n"
	}
	//This is weird - ServeContent supposedly handles MIME setting
	//But the Webgo content setter needs to be used too
	//In addition, ServeFile doesn't work, ServeContent has to be used
	ctx.ContentType(mime)
	http.ServeContent(ctx.ResponseWriter, ctx.Request, path, time.Now(), f)
	return ""
}
開發者ID:Luminarys,項目名稱:Sto,代碼行數:30,代碼來源:getHandling.go

示例2: fragmentShaderHandler

func fragmentShaderHandler(ctx *web.Context, path string) {
	file, err := os.Open(path)
	check(err)
	ctx.ContentType("x-shader/x-fragment")
	_, err = io.Copy(ctx, file)
	check(err)
}
開發者ID:johnpmayer,項目名稱:commissar,代碼行數:7,代碼來源:game.go

示例3: PostPaths

func (server *Server) PostPaths(ctx *web.Context) string {
	var input struct {
		Paths        []geotypes.NodeEdge
		Country      string
		SpeedProfile int
	}

	var (
		computed []geotypes.Path
	)

	if err := json.NewDecoder(ctx.Request.Body).Decode(&input); err != nil {
		content, _ := ioutil.ReadAll(ctx.Request.Body)
		ctx.Abort(400, "Couldn't parse JSON: "+err.Error()+" in '"+string(content)+"'")
		return ""
	} else {
		var err error
		computed, err = server.Via.CalculatePaths(input.Paths, input.Country, input.SpeedProfile)
		if err != nil {
			ctx.Abort(422, "Couldn't resolve addresses: "+err.Error())
			return ""
		}
	}

	res, err := json.Marshal(computed)
	if err != nil {
		ctx.Abort(500, "Couldn't serialize paths: "+err.Error())
		return ""
	}

	ctx.Header().Set("Access-Control-Allow-Origin", "*")
	ctx.ContentType("application/json")
	return string(res)
}
開發者ID:nfleet,項目名稱:via,代碼行數:34,代碼來源:api.go

示例4: search

func search(ctx *web.Context, collection string) {
	ctx.ContentType("json")
	ctx.SetHeader("Access-Control-Allow-Origin", "*", true)

	query := ctx.Params["query"]

	var limit, offset int64
	var err error

	if limit, err = strconv.ParseInt(ctx.Params["limit"], 10, 32); err != nil {
		limit = 10
	}
	if offset, err = strconv.ParseInt(ctx.Params["offset"], 10, 32); err != nil {
		offset = 0
	}

	results, err := c.Search(collection, query, int(limit), int(offset))

	buf := new(bytes.Buffer)
	encoder := json.NewEncoder(buf)

	if err != nil {
		encoder.Encode(err)
		ctx.WriteHeader(err.(*gorc.OrchestrateError).StatusCode)
	} else {
		encoder.Encode(results)
	}

	ctx.Write(buf.Bytes())
}
開發者ID:Rapurva,項目名稱:orchestrate-heroku-search,代碼行數:30,代碼來源:web.go

示例5: getSong

func getSong(ctx *web.Context, song string) string {
	//Open the file
	f, err := os.Open("static/queue/" + song)
	if err != nil {
		return "Error reading file!\n"
	}

	//Get MIME
	r, err := ioutil.ReadAll(f)
	if err != nil {
		return "Error reading file!\n"
	}
	mime := http.DetectContentType(r)

	_, err = f.Seek(0, 0)
	if err != nil {
		return "Error reading the file\n"
	}
	//This is weird - ServeContent supposedly handles MIME setting
	//But the Webgo content setter needs to be used too
	//In addition, ServeFile doesn't work, ServeContent has to be used
	ctx.ContentType(mime)
	http.ServeContent(ctx.ResponseWriter, ctx.Request, "static/queue/"+song, time.Now(), f)
	return ""
}
開發者ID:Luminarys,項目名稱:patchy,代碼行數:25,代碼來源:getHandling.go

示例6: gamePageHandler

func gamePageHandler(ctx *web.Context) {
	gamePage, err := os.Open("index.html")
	check(err)
	ctx.ContentType("html")
	_, err = io.Copy(ctx, gamePage)
	check(err)
}
開發者ID:johnpmayer,項目名稱:commissar,代碼行數:7,代碼來源:game.go

示例7: getCover

func getCover(ctx *web.Context, album string) string {
	dir := musicDir + "/" + album
	cover := "static/image/missing.png"
	if exists(dir + "/cover.jpg") {
		cover = dir + "/cover.jpg"
	} else if exists(dir + "/cover.png") {
		cover = dir + "/cover.png"
	}
	//Open the file
	f, err := os.Open(cover)
	if err != nil {
		return "Error reading file!\n"
	}

	//Get MIME
	r, err := ioutil.ReadAll(f)
	if err != nil {
		return "Error reading file!\n"
	}
	mime := http.DetectContentType(r)

	_, err = f.Seek(0, 0)
	if err != nil {
		return "Error reading the file\n"
	}
	//This is weird - ServeContent supposedly handles MIME setting
	//But the Webgo content setter needs to be used too
	//In addition, ServeFile doesn't work, ServeContent has to be used
	ctx.ContentType(mime)
	http.ServeContent(ctx.ResponseWriter, ctx.Request, cover, time.Now(), f)
	return ""
}
開發者ID:Luminarys,項目名稱:tenshi,代碼行數:32,代碼來源:main.go

示例8: pngHandler

func pngHandler(ctx *web.Context, path string) {
	file, err := os.Open(path)
	check(err)
	ctx.ContentType("png")
	_, err = io.Copy(ctx, file)
	check(err)
}
開發者ID:johnpmayer,項目名稱:commissar,代碼行數:7,代碼來源:game.go

示例9: Options

func Options(ctx *web.Context, route string) string {
	ctx.SetHeader("Access-Control-Allow-Origin", "*", false)
	ctx.SetHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, If-None-Match", false)
	ctx.SetHeader("Access-control-Allow-Methods", "GET, PUT, POST, DELETE", false)
	ctx.ContentType("application/json")
	return "{}"
}
開發者ID:nfleet,項目名稱:via,代碼行數:7,代碼來源:server.go

示例10: Sendstatic

func Sendstatic(ctx *web.Context, val string) {
	file, _err := ioutil.ReadFile("static/" + val)
	if _err != nil {
		return
	}
	filetype := strings.Split(val, ".")
	ctx.ContentType(filetype[len(filetype)-1])
	ctx.WriteString(string(file))
}
開發者ID:Chownie,項目名稱:SteamMatcher,代碼行數:9,代碼來源:utils.go

示例11: renderContent

func (c StaticController) renderContent(ctx *web.Context, contentType, filepath string) {
	if file, err := os.Open(filepath); err != nil {
		w := ResponseWriter{ctx, "HTML"}
		w.SendError(err)
	} else {
		ctx.ContentType(contentType + "; charset=utf-8")
		reader := bufio.NewReader(file)
		io.Copy(ctx.ResponseWriter, reader)
	}
}
開發者ID:rchargel,項目名稱:zilch,代碼行數:10,代碼來源:static_controller.go

示例12: hexagram

func hexagram(ctx *web.Context, num string) string {
	n, _ := strconv.Atoi(num)
	hexagram, found := iching.GetHexagramByNum(n)
	ctx.ContentType("json")

	if found {
		js, _ := json.Marshal(hexagram)
		return string(js)
	}

	return "Hexagram not found"
}
開發者ID:abrookins,項目名稱:go_iching_api,代碼行數:12,代碼來源:iching_api.go

示例13: reading

func reading(ctx *web.Context) string {
	ctx.Request.ParseForm()
	question := ctx.Request.Form.Get("question")
	ctx.ContentType("json")

	if question == "" {
		question = "No question provided"
	}

	reading := iching.GetReading(question)
	js, _ := json.Marshal(reading)

	return string(js)
}
開發者ID:abrookins,項目名稱:go_iching_api,代碼行數:14,代碼來源:iching_api.go

示例14: GetDeployments

func GetDeployments(ctx *web.Context) {
	fmt.Printf("************************************\n")
	deployments, err := LoadDeployments()
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, deployment := range deployments {
		fmt.Printf(" Deployment %s ...\n", deployment.Name)
	}
	b, err := json.Marshal(deployments)
	if err != nil {
		fmt.Println("error:", err)
	}
	ctx.ContentType("json")
	ctx.Write(b)
}
開發者ID:napsy,項目名稱:goready,代碼行數:17,代碼來源:main.go

示例15: RenderDistributionImage

// RenderDistributionImage renders the distribution image for the globe.
func (c PngController) RenderDistributionImage(ctx *web.Context, scale string) {
	intScale := c.convertScale(scale)

	if img, err := c.getBackgroundImage(intScale); err != nil {
		rw := ResponseWriter{ctx, "JSON"}
		rw.SendError(err)
	} else {
		distributions := c.database.GetDistributions()
		sort.Sort(DistributionSorter(distributions))

		for _, dist := range distributions {
			c.drawDistribution(img, float32(dist.Latitude), float32(dist.Longitude), float32(intScale)/float32(2), int(dist.ZipCodes))
		}

		ctx.ContentType("image/png")
		png.Encode(ctx.ResponseWriter, img)
	}
}
開發者ID:rchargel,項目名稱:zilch,代碼行數:19,代碼來源:png_controller.go


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