response.go 1000 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package response
  5. import (
  6. "net/http"
  7. "time"
  8. )
  9. // Redirect redirects the user to another location.
  10. func Redirect(w http.ResponseWriter, r *http.Request, path string) {
  11. http.Redirect(w, r, path, http.StatusFound)
  12. }
  13. // NotModified sends a response with a 304 status code.
  14. func NotModified(w http.ResponseWriter) {
  15. w.WriteHeader(http.StatusNotModified)
  16. }
  17. // Cache returns a response with caching headers.
  18. func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, content []byte, duration time.Duration) {
  19. w.Header().Set("Content-Type", mimeType)
  20. w.Header().Set("ETag", etag)
  21. w.Header().Set("Cache-Control", "public")
  22. w.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))
  23. if etag == r.Header.Get("If-None-Match") {
  24. w.WriteHeader(http.StatusNotModified)
  25. } else {
  26. w.Write(content)
  27. }
  28. }