sunset.go 700 B

12345678910111213141516171819202122232425
  1. package middleware
  2. import (
  3. "net/http"
  4. "time"
  5. )
  6. // Sunset set Deprecation/Sunset header to response
  7. // This can be used to enable Sunset in a route or a route group
  8. // For more: https://www.rfc-editor.org/rfc/rfc8594.html
  9. func Sunset(sunsetAt time.Time, links ...string) func(http.Handler) http.Handler {
  10. return func(next http.Handler) http.Handler {
  11. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  12. if !sunsetAt.IsZero() {
  13. w.Header().Set("Sunset", sunsetAt.Format(http.TimeFormat))
  14. w.Header().Set("Deprecation", sunsetAt.Format(http.TimeFormat))
  15. for _, link := range links {
  16. w.Header().Add("Link", link)
  17. }
  18. }
  19. next.ServeHTTP(w, r)
  20. })
  21. }
  22. }