本文整理匯總了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 ""
}
示例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)
}
示例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)
}
示例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())
}
示例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 ""
}
示例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)
}
示例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 ""
}
示例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)
}
示例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 "{}"
}
示例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))
}
示例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)
}
}
示例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"
}
示例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)
}
示例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)
}
示例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)
}
}