html.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 html
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/http/response"
  8. "github.com/miniflux/miniflux/logger"
  9. )
  10. // OK writes a standard HTML response.
  11. func OK(w http.ResponseWriter, r *http.Request, b []byte) {
  12. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  13. response.Compress(w, r, b)
  14. }
  15. // ServerError sends a 500 error to the browser.
  16. func ServerError(w http.ResponseWriter, err error) {
  17. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  18. w.WriteHeader(http.StatusInternalServerError)
  19. if err != nil {
  20. logger.Error("[Internal Server Error] %v", err)
  21. w.Write([]byte("Internal Server Error: " + err.Error()))
  22. } else {
  23. w.Write([]byte("Internal Server Error"))
  24. }
  25. }
  26. // BadRequest sends a 400 error to the browser.
  27. func BadRequest(w http.ResponseWriter, err error) {
  28. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  29. w.WriteHeader(http.StatusBadRequest)
  30. if err != nil {
  31. logger.Error("[Bad Request] %v", err)
  32. w.Write([]byte("Bad Request: " + err.Error()))
  33. } else {
  34. w.Write([]byte("Bad Request"))
  35. }
  36. }
  37. // NotFound sends a 404 error to the browser.
  38. func NotFound(w http.ResponseWriter) {
  39. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  40. w.WriteHeader(http.StatusNotFound)
  41. w.Write([]byte("Page Not Found"))
  42. }
  43. // Forbidden sends a 403 error to the browser.
  44. func Forbidden(w http.ResponseWriter) {
  45. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  46. w.WriteHeader(http.StatusForbidden)
  47. w.Write([]byte("Access Forbidden"))
  48. }