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


Golang Context.SetHeader方法代碼示例

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


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

示例1: api

func api(ctx *web.Context, username string) string {
	user, err := getUser(username)
	ctx.SetHeader("Content-Type", "application/prs.kevinburke.snapchat-v1+json", true)
	if err != nil {
		checkError(err)
		ctx.NotFound("User not found")
		return ""
	}
	friends, err := getFriendsById(user.Id)
	checkError(err)
	var links Links
	var users []User
	users = append(users, *user)
	for i := 0; i < len(friends.Friends); i++ {
		friend := friends.Friends[i]
		link := Link{friend.UserId, friend.FriendId, friend.Index}
		links.Links = append(links.Links, link)
		user, err := getUserById(friend.UserId)
		checkError(err)
		users = append(users, *user)
	}
	response := Response{links.Links, users}
	bytes, err := json.Marshal(response)
	checkError(err)
	return string(bytes)
}
開發者ID:kevinburke,項目名稱:snapchat-friends,代碼行數:26,代碼來源:hello.go

示例2: Get

func (h *RestQueueHandler) Get(ctx *web.Context, val string) {
	queue := redisq.NewRedisQueue(h.redis, val)
	if !queue.Exists() {
		writeError(ctx, 404, QueueNotFound)
		return
	}
	if queue.Empty() {
		writeError(ctx, 400, EmptyQueue)
		return
	}
	mesg, err := queue.GetNoWait()
	if err != nil {
		writeError(ctx, 500, GetError)
		if Settings.Debug {
			debug := fmt.Sprintf("Debug: %s", err)
			ctx.WriteString(debug)
		}
		h.logger.Printf("Dequeue from <%s> Error:%s", val, err)
		return
	}
	resp, _ := json.Marshal(mesg)
	//mesg.(type) is iteface{}
	//resp.(type) is []byte

	ctx.SetHeader("Content-Type", "application/json; charset=UTF-8", true)
	ctx.WriteString(string(resp))

}
開發者ID:kenshinx,項目名稱:restmq.go,代碼行數:28,代碼來源:web.go

示例3: vnstat

func vnstat(ctx *web.Context, iface string, debug string) string {
	app := "vnstat"

	arg0 := "-i"
	arg2 := "--xml"

	ctx.SetHeader("Content-Type", "application/json", true)

	rawxml := runcmd(app, arg0, iface, arg2)

	var q Vnstat
	xml.Unmarshal([]byte(rawxml), &q)

	var jsondata []byte
	var err error

	if debug == "debug" {
		jsondata, err = json.MarshalIndent(q, "", "  ")
	} else {
		jsondata, err = json.Marshal(q)
	}

	if err != nil {
		return "Error"
	}
	return string(jsondata)
}
開發者ID:orvice,項目名稱:iVnstat,代碼行數:27,代碼來源:server.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: players

func players(ctx *web.Context) []byte {
	ctx.SetHeader("Content-Type", "application/json", true)
	players, err := arena.GetPlayers()
	checkError(err)
	jsonPlayers, err := json.Marshal(players)
	checkError(err)
	return jsonPlayers
}
開發者ID:kevinburke,項目名稱:arena,代碼行數:8,代碼來源:server.go

示例6: List

func (h *RestQueueHandler) List(ctx *web.Context) {

	var keys []string
	keys, _ = h.redis.Keys("*")
	resp, _ := json.Marshal(keys)
	ctx.SetHeader("Content-Type", "application/json; charset=UTF-8", true)
	ctx.WriteString(string(resp))
}
開發者ID:kenshinx,項目名稱:restmq.go,代碼行數:8,代碼來源:web.go

示例7: serveCaptchaAudio

func serveCaptchaAudio(ctx *web.Context, id string) {
	//tell the user's browser not to cache the audio file
	//(would cause old audio file to be used even if user has reloaded the CAPTCHA)
	ctx.SetHeader("Cache-Control", "no-cache", true)

	err := captcha.WriteAudio(ctx, id, "english")
	if err != nil {
		logger.Println("Error, could not write CAPTCHA audio")
		logger.Println(err.Error())
	}
}
開發者ID:JGets,項目名稱:reduse,代碼行數:11,代碼來源:pageFunc.go

示例8: uploader

func uploader(ctx *web.Context, key string) {
	updateRemoteAddr(ctx)

	if _, ok := uploadRequests[key]; ok {
		// key exists
		ctx.Forbidden()
		return
	}

	wait := make(chan string)

	defer delete(uploadRequests, key)
	uploadRequests[key] = uploadRequest{ctx.Request, wait}

	var result string
	select {
	case result = <-wait:
	case <-time.After(WAIT_CLIENT_TIMEOUT):
		result = "wait client timeout"
	}

	if result == "connected" {
		// wait actual result
		result = <-wait
	}

	var body string

	if xrw := ctx.Request.Header.Get("X-Requested-With"); xrw == "XMLHttpRequest" {
		body = "{\"result\": \"" + result + "\"}\n"

		ctx.SetHeader("Content-Type", "application/json", true)
	} else {
		tmpl, err := template.New("uploader").Parse(uploadTemplate)

		if err != nil {
			panic(err)
		}

		var buf bytes.Buffer
		tmpl.Execute(&buf, UploadTemplateValue{result})
		body = buf.String()
	}

	if result == "ok" {
		ctx.WriteString(body)
	} else {
		ctx.Abort(500, body)
		ctx.Request.Body.Close()
	}
}
開發者ID:nakamuray,項目名稱:fileproxy,代碼行數:51,代碼來源:fileproxy.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: serveCaptchaImage

/*
	Serves a CAPTCHA image
	Parameters:
		ctx:	The context of the http request
		id:		The ID of the captcha to serve
*/
func serveCaptchaImage(ctx *web.Context, id string) {

	width, err := strconv.Atoi(ctx.Params["width"])
	if err != nil {
		logger.Printf("Error: could not parse captcha image width of '%v'\n%v\n", ctx.Params["width"], err.Error())
		width = captcha.StdWidth
	}

	height, err := strconv.Atoi(ctx.Params["height"])
	if err != nil {
		logger.Printf("Error: could not parse captcha image height of '%v'\n%v\n", ctx.Params["height"], err.Error())
		height = captcha.StdHeight
	}

	//tell the user's browser not to cache the image file
	ctx.SetHeader("Cache-Control", "no-cache", true)

	err = captcha.WriteImage(ctx, id, width, height)
	if err != nil {
		logger.Println("Error, could not write CAPTCHA image")
		logger.Println(err.Error())
	}
}
開發者ID:JGets,項目名稱:reduse,代碼行數:29,代碼來源:pageFunc.go

示例11: downloader

func downloader(ctx *web.Context, key string) {
	updateRemoteAddr(ctx)

	up, ok := uploadRequests[key]

	if !ok {
		ctx.NotFound("key doesn't exist")
		return
	}

	up.wait <- "connected"

	result := "ng"
	defer func() { up.wait <- result }()

	mr, err := up.request.MultipartReader()
	if err != nil {
		ctx.Abort(500, err.Error())
		return
	}

	p, err := mr.NextPart()
	if p.FormName() == "size" {
		fileSize, err := ioutil.ReadAll(p)
		if err == nil {
			fileSize, err := strconv.ParseInt(string(fileSize), 10, 64)
			if err == nil && fileSize >= 0 {
				ctx.SetHeader("Content-Length", strconv.FormatInt(fileSize, 10), true)
			}
		}
	}

	p, err = mr.NextPart()
	if err != nil {
		ctx.Abort(500, err.Error())
		return
	}
	if p.FormName() != "file" {
		ctx.Abort(500, "invalid POST (upload) request")
		return
	}

	if contentType := p.Header.Get("Content-Type"); contentType != "" {
		ctx.SetHeader("Content-Type", contentType, true)
	}

	ctx.SetHeader("Content-Disposition", "attachment; filename="+p.FileName(), true)

	_, err = io.Copy(ctx, p)

	if err == nil {
		result = "ok"
	} else {
		// XXX: may expose too many infomation (such as client IP address)
		//result = err.Error()
	}
}
開發者ID:nakamuray,項目名稱:fileproxy,代碼行數:57,代碼來源:fileproxy.go

示例12: getPaste

func getPaste(ctx *web.Context, uri string) string {
	c, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		Log(err)
		return mustache.Render("Cannot connect to DB \r\n")
	}
	defer c.Close()
	y := "PASTE_DOES_NOT_EXIST"
	if matched, _ := regexp.MatchString("[a-zA-Z0-9]+", uri); matched {
		y = "paste_" + uri
	}
	x, err := redis.String(c.Do("GET", y))
	ctx.SetHeader("Content-Type", "text/plain", true)
	ctx.SetHeader("X-Powered-By", "web.go", true)
	ctx.SetHeader("Connection", "close", true)
	return mustache.Render(x)
}
開發者ID:boopathi,項目名稱:pastebin,代碼行數:17,代碼來源:paste.go

示例13: APIResponse

func APIResponse(ctx *web.Context, resp interface{}) string {
	resp_text, _ := json.Marshal(resp)
	ctx.SetHeader("Content-Type", "application/json", true)
	return string(resp_text[:])
}
開發者ID:jahrain,項目名稱:gmb,代碼行數:5,代碼來源:mbroker.go


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