html.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 // import "miniflux.app/http/response/html"
  5. import (
  6. "net/http"
  7. "miniflux.app/http/response"
  8. "miniflux.app/logger"
  9. )
  10. // OK creates a new HTML response with a 200 status code.
  11. func OK(w http.ResponseWriter, r *http.Request, body interface{}) {
  12. builder := response.New(w, r)
  13. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  14. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  15. builder.WithBody(body)
  16. builder.Write()
  17. }
  18. // ServerError sends an internal error to the client.
  19. func ServerError(w http.ResponseWriter, r *http.Request, err error) {
  20. logger.Error("[HTTP:Internal Server Error] %s => %v", r.URL, err)
  21. builder := response.New(w, r)
  22. builder.WithStatus(http.StatusInternalServerError)
  23. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  24. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  25. builder.WithBody(err)
  26. builder.Write()
  27. }
  28. // BadRequest sends a bad request error to the client.
  29. func BadRequest(w http.ResponseWriter, r *http.Request, err error) {
  30. logger.Error("[HTTP:Bad Request] %s => %v", r.URL, err)
  31. builder := response.New(w, r)
  32. builder.WithStatus(http.StatusBadRequest)
  33. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  34. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  35. builder.WithBody(err)
  36. builder.Write()
  37. }
  38. // Forbidden sends a forbidden error to the client.
  39. func Forbidden(w http.ResponseWriter, r *http.Request) {
  40. logger.Error("[HTTP:Forbidden] %s", r.URL)
  41. builder := response.New(w, r)
  42. builder.WithStatus(http.StatusForbidden)
  43. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  44. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  45. builder.WithBody("Access Forbidden")
  46. builder.Write()
  47. }
  48. // NotFound sends a page not found error to the client.
  49. func NotFound(w http.ResponseWriter, r *http.Request) {
  50. logger.Error("[HTTP:Not Found] %s", r.URL)
  51. builder := response.New(w, r)
  52. builder.WithStatus(http.StatusNotFound)
  53. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  54. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  55. builder.WithBody("Page Not Found")
  56. builder.Write()
  57. }
  58. // Redirect redirects the user to another location.
  59. func Redirect(w http.ResponseWriter, r *http.Request, uri string) {
  60. http.Redirect(w, r, uri, http.StatusFound)
  61. }