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


Golang ResponseWriter.WriteHeader方法代碼示例

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


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

示例1: 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

示例2: CheckReleaseFilter

func CheckReleaseFilter(w traffic.ResponseWriter, r *traffic.Request) {
	artistId := r.URL.Query().Get("artist_id")
	releaseId := r.URL.Query().Get("release_id")

	if releaseId == "" {
		return
	}

	if !models.IsValidUUID(releaseId) {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	if artistId != "" {
		if !models.IsValidUUID(artistId) {
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		if artist.HasRelease(artistId, releaseId) {
			return
		}

		ReleaseNotFoundHandler(w, r)
		return
	}

	if !release.Exists(releaseId) {
		ReleaseNotFoundHandler(w, r)
		return
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:32,代碼來源:common_handlers.go

示例3: CheckForDdosString

func CheckForDdosString(w traffic.ResponseWriter, r *traffic.Request) {
	for k, _ := range uriFragments {
		fmt.Printf("Searching for %s in %s", k, r.RequestURI)
		if strings.Contains(r.RequestURI, k) {
			fmt.Printf("Found %s", k)
			w.WriteHeader(http.StatusGatewayTimeout)
			w.WriteText("connection timed out")
		}
	}
}
開發者ID:dvwallin,項目名稱:goddos,代碼行數:10,代碼來源:goddos.go

示例4: deleteHandler

func (api *ImgServerApi) deleteHandler(w traffic.ResponseWriter, r *traffic.Request) {

	filename := r.URL.Query().Get("image")
	err := api.imageDir.DeleteFile(filename)
	api.imageCache.Remove(api.imageCache.FindKeys(filename))

	if err != nil {
		traffic.Logger().Print(err.Error())
		w.WriteHeader(http.StatusNotFound)
	} else {
		w.WriteHeader(http.StatusOK)
	}
}
開發者ID:jhyle,項目名稱:imgserver,代碼行數:13,代碼來源:api.go

示例5: ArtistHandler

func ArtistHandler(w traffic.ResponseWriter, r *traffic.Request) {
	id := r.URL.Query().Get("id")
	Artist, err := artist.ById(id)

	if err == nil {
		json.NewEncoder(w).Encode(Artist)
	} else if err == sql.ErrNoRows {
		ArtistNotFoundHandler(w, r)
	} else if _, ok := err.(models.InvalidUUID); ok {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		panic(err)
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:14,代碼來源:artist_handler.go

示例6: listHandler

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)
	}
}
開發者ID:jhyle,項目名稱:imgserver,代碼行數:15,代碼來源:api.go

示例7: ReleaseGroupsHandler

func ReleaseGroupsHandler(w traffic.ResponseWriter, r *traffic.Request) {
	artistId := r.URL.Query().Get("artist_id")

	ReleaseGroups, err := releasegroup.AllByArtistId(artistId)

	if err == nil {
		json.NewEncoder(w).Encode(ReleaseGroups)
	} else if err == sql.ErrNoRows {
		w.WriteHeader(http.StatusNotFound)
	} else if _, ok := err.(models.InvalidUUID); ok {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		panic(err)
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:15,代碼來源:release_groups_handler.go

示例8: CheckArtistFilter

func CheckArtistFilter(w traffic.ResponseWriter, r *traffic.Request) {
	artistId := r.URL.Query().Get("artist_id")
	if artistId == "" {
		return
	}

	if !models.IsValidUUID(artistId) {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	if !artist.Exists(artistId) {
		ArtistNotFoundHandler(w, r)
		return
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:16,代碼來源:common_handlers.go

示例9: RecordingsHandler

func RecordingsHandler(w traffic.ResponseWriter, r *traffic.Request) {
	releaseId := r.URL.Query().Get("release_id")

	var recordings []*models.Recording
	var err error

	recordings, err = recording.AllByReleaseId(releaseId)

	if err == nil {
		json.NewEncoder(w).Encode(recordings)
	} else if err == sql.ErrNoRows {
		w.WriteHeader(http.StatusNotFound)
	} else if _, ok := err.(models.InvalidUUID); ok {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		panic(err)
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:18,代碼來源:recordings_handler.go

示例10: ReleaseGroupHandler

func ReleaseGroupHandler(w traffic.ResponseWriter, r *traffic.Request) {
	artistId := r.URL.Query().Get("artist_id")
	id := r.URL.Query().Get("id")

	var ReleaseGroup *models.ReleaseGroup
	var err error

	if artistId != "" {
		ReleaseGroup, err = releasegroup.ByArtistIdAndId(artistId, id)
	} else {
		ReleaseGroup, err = releasegroup.ById(id)
	}

	if err == nil {
		json.NewEncoder(w).Encode(ReleaseGroup)
	} else if err == sql.ErrNoRows {
		ReleaseGroupNotFoundHandler(w, r)
	} else if _, ok := err.(models.InvalidUUID); ok {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		panic(err)
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:23,代碼來源:release_group_handler.go

示例11: RecordingHandler

func RecordingHandler(w traffic.ResponseWriter, r *traffic.Request) {
	releaseId := r.URL.Query().Get("release_id")
	id := r.URL.Query().Get("id")

	var Recording *models.Recording
	var err error

	if releaseId != "" {
		Recording, err = recording.ByReleaseIdAndId(releaseId, id)
	} else {
		Recording, err = recording.ById(id)
	}

	if err == nil {
		json.NewEncoder(w).Encode(Recording)
	} else if err == sql.ErrNoRows {
		RecordingNotFoundHandler(w, r)
	} else if _, ok := err.(models.InvalidUUID); ok {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		panic(err)
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:23,代碼來源:recording_handler.go

示例12: ReleasesHandler

func ReleasesHandler(w traffic.ResponseWriter, r *traffic.Request) {
	artistId := r.URL.Query().Get("artist_id")
	releaseGroupId := r.URL.Query().Get("release_group_id")

	var releases []*models.Release
	var err error

	if releaseGroupId == "" {
		releases, err = release.AllByArtistId(artistId)
	} else {
		releases, err = release.AllByReleaseGroupId(releaseGroupId)
	}

	if err == nil {
		json.NewEncoder(w).Encode(releases)
	} else if err == sql.ErrNoRows {
		ReleaseNotFoundHandler(w, r)
	} else if _, ok := err.(models.InvalidUUID); ok {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		panic(err)
	}
}
開發者ID:pilu,項目名稱:cerebellum,代碼行數:23,代碼來源:releases_handler.go

示例13: uploadHandler

func (api *ImgServerApi) uploadHandler(w traffic.ResponseWriter, r *traffic.Request) {

	filename := r.URL.Query().Get("image")
	uploadedImage, _, err := image.Decode(r.Body)

	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		img := drawOnWhite(uploadedImage.Bounds().Size(), image.Pt(0, 0), image.Pt(0, 0), uploadedImage)
		err = api.imageDir.WriteImage(filename, img, 100)
		if err != nil {
			traffic.Logger().Print(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
		} else {
			w.WriteHeader(http.StatusOK)
		}
	}
}
開發者ID:jhyle,項目名稱:imgserver,代碼行數:18,代碼來源:api.go

示例14: copyHandler

func (api *ImgServerApi) copyHandler(w traffic.ResponseWriter, r *traffic.Request) {

	src := r.URL.Query().Get("src")
	dst := r.URL.Query().Get("dst")

	data, err := api.imageDir.ReadFile(src)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	err = api.imageDir.WriteFile(dst, data)
	if err != nil {
		traffic.Logger().Print(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
}
開發者ID:jhyle,項目名稱:imgserver,代碼行數:20,代碼來源:api.go

示例15: imageHandler

func (api *ImgServerApi) imageHandler(w traffic.ResponseWriter, r *traffic.Request) {

	params := r.URL.Query()
	width := toInt(params.Get("width"), 0)
	height := toInt(params.Get("height"), 0)
	imagefile := params.Get("image")
	cacheKey := imagefile + string(width) + string(height)

	modTime := api.imageDir.ModTime(imagefile)
	if modTime == nil {
		api.imageCache.Remove(api.imageCache.FindKeys(imagefile))
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if cachedImage := api.imageCache.Get(cacheKey, *modTime); cachedImage != nil {
		sendJpeg(w, cachedImage)
	} else {
		origImage, err := api.imageDir.ReadImage(imagefile)
		if err != nil {
			traffic.Logger().Print(err.Error())
			w.WriteHeader(http.StatusNotFound)
		} else {
			semaphore <- struct{}{}
			buffer := new(bytes.Buffer)
			sizedImage := resizeImage(origImage, width, height, api.faceDetection)
			err = jpeg.Encode(buffer, sizedImage, &jpeg.Options{95})
			<-semaphore
			if err != nil {
				traffic.Logger().Print(err.Error())
				w.WriteHeader(http.StatusInternalServerError)
			} else {
				data := make([]byte, buffer.Len())
				buffer.Read(data)
				api.imageCache.Put(cacheKey, data, *modTime)
				sendJpeg(w, data)
			}
		}
	}
}
開發者ID:jhyle,項目名稱:imgserver,代碼行數:40,代碼來源:api.go


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