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


Golang ResponseWriter.SetVar方法代碼示例

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


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

示例1: GenerateImageCache

func GenerateImageCache(w traffic.ResponseWriter, r *traffic.Request) {

	if !exists(cache_folder) {
		if err := os.Mkdir(cache_folder, 0644); err != nil {
			panic(err)
		}
	}

	filename := fmt.Sprintf("%s/%dx%d.jpg", cache_folder, w.GetVar("width"), w.GetVar("height"))
	w.SetVar("filename", filename)

	if !exists(filename) {
		// file does not exists, generate a cached version
		width := w.GetVar("width").(int)
		height := w.GetVar("height").(int)

		src_image := loadImageFromFile(image_file)
		pattern := resizeImage(src_image, width, height)

		var dst_image image.Image
		if width > height {
			dst_image = tileImageHorizontally(pattern, width, height)
		} else {
			dst_image = tileImageVertically(pattern, width, height)
		}

		saveImageToFile(filename, dst_image)
	}
}
開發者ID:jmptrader,項目名稱:purrraceholder,代碼行數:29,代碼來源:image_handler.go

示例2: RequireValidImageParameters

func RequireValidImageParameters(w traffic.ResponseWriter, r *traffic.Request) {

	width, err := strconv.Atoi(r.Param("width"))
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	height, err := strconv.Atoi(r.Param("height"))
	if err != nil {
		height = width
	}

	if (width <= 2560 && width > 0) && (height <= 2560 && height > 0) {

		w.SetVar("width", width)
		w.SetVar("height", height)

		// log latest greatest creation
		if err := ioutil.WriteFile(filepath.Join(cache_folder, "/latest"), []byte(fmt.Sprintf("%d/%d", width, height)), 0644); err != nil {
			// panic is trapped by Traffic and show us a nice stack trace in the browser
			// a proper error handling should be provided, but in this simple example
			// it's used to remind you to always check for errors
			panic(err)
		}

	} else {
		// bad request
		w.WriteHeader(http.StatusBadRequest)
		w.Render("400")
	}

}
開發者ID:jmptrader,項目名稱:purrraceholder,代碼行數:33,代碼來源:image_handler.go

示例3: ServeHTTP

// If the request path is "/ping", it writes PONG in the response and returns without calling the next middleware
// Otherwise it sets the variable "PING" with PONG as value and calls the next  middleware.
// The next middleware and the final handler can get that variable with:
//   w.GetVar("ping")
func (c *PingMiddleware) ServeHTTP(w traffic.ResponseWriter, r *traffic.Request, next traffic.NextMiddlewareFunc) {
	if r.URL.Path == "/ping" {
		w.WriteText("pong\n")

		return
	}

	if nextMiddleware := next(); nextMiddleware != nil {
		w.SetVar("ping", "pong")
		nextMiddleware.ServeHTTP(w, r, next)
	}

	return
}
開發者ID:jmptrader,項目名稱:traffic,代碼行數:18,代碼來源:main.go

示例4: ServeHTTP

func (c *Middleware) ServeHTTP(w traffic.ResponseWriter, r *traffic.Request, next traffic.NextMiddlewareFunc) {
	ok := miniprofiler.Enable(r.Request)
	if ok && strings.HasPrefix(r.Request.URL.Path, miniprofiler.PATH) {
		miniprofiler.MiniProfilerHandler(w, r.Request)
		return
	}
	p := miniprofiler.NewProfile(w, r.Request, r.URL.Path)
	w.SetVar("miniprofiler", p.Includes())
	w.SetVar("miniprofiler_timer", p)
	if nextMiddleware := next(); nextMiddleware != nil {
		nextMiddleware.ServeHTTP(w, r, next)
	}
	if ok {
		p.Finalize()
	}
	return
}
開發者ID:jango2015,項目名稱:go,代碼行數:17,代碼來源:miniprofiler_traffic.go

示例5: ServeHTTP

func (middleware *trafficCompositeMiddleware) ServeHTTP(w traffic.ResponseWriter, r *traffic.Request, next traffic.NextMiddlewareFunc) {
	if nextMiddleware := next(); nextMiddleware != nil {
		w.SetVar("field", r.URL.Path)
		nextMiddleware.ServeHTTP(w, r, next)
	}
}
開發者ID:rexk,項目名稱:golang-mux-benchmark,代碼行數:6,代碼來源:mux_bench_test.go


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