html.go 1.6 KB

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