コード例 #1
0
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")
	}

}
コード例 #2
0
ファイル: main.go プロジェクト: jmptrader/traffic
func pageHandler(w traffic.ResponseWriter, r *traffic.Request) {
	pagePath := r.Param("page_path")

	responseData := &ResponseData{
		PagePath: pagePath,
	}

	w.Render("index", responseData)
}
コード例 #3
0
ファイル: stickerl.go プロジェクト: pilu/stickerl
func codesHandler(w traffic.ResponseWriter, r *traffic.Request) {
	code := r.Param("code")
	url := fmt.Sprintf("%s%s", baseUrl, code)

	grid, err := qrencode.Encode(url, qrencode.ECLevelQ)
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "image/png")
	png.Encode(w, grid.Image(8))
}
コード例 #4
0
ファイル: api.go プロジェクト: jhyle/imgserver
func (api *ImgServerApi) listHandler(w traffic.ResponseWriter, r *traffic.Request) {

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

	files, err := api.imageDir.ListFiles(time.Duration(age) * time.Second)
	if err != nil {
		traffic.Logger().Print(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
	} else {
		w.WriteJSON(files)
	}
}
コード例 #5
0
ファイル: main.go プロジェクト: jmptrader/traffic
func pageHandler(w traffic.ResponseWriter, r *traffic.Request) {
	w.WriteText("Category ID: %s\n", r.Param("category_id"))
	w.WriteText("Page ID: %s\n", r.Param("id"))
}
コード例 #6
0
ファイル: main.go プロジェクト: jmptrader/traffic
func checkPrivatePageApiKey(w traffic.ResponseWriter, r *traffic.Request) {
	if r.Param("private_api_key") != "bar" {
		w.WriteHeader(http.StatusUnauthorized)
		w.WriteText("Not authorized\n")
	}
}
コード例 #7
0
ファイル: main.go プロジェクト: jmptrader/traffic
func checkApiKey(w traffic.ResponseWriter, r *traffic.Request) {
	if r.Param("api_key") != "foo" {
		w.WriteHeader(http.StatusUnauthorized)
		w.WriteText("Not authorized\n")
	}
}