server_middleware.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package server
  2. import (
  3. "heckel.io/ntfy/util"
  4. "net/http"
  5. )
  6. func (s *Server) limitRequests(next handleFunc) handleFunc {
  7. return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
  8. if util.ContainsIP(s.config.VisitorRequestExemptIPAddrs, v.ip) {
  9. return next(w, r, v)
  10. } else if !v.RequestAllowed() {
  11. return errHTTPTooManyRequestsLimitRequests
  12. }
  13. return next(w, r, v)
  14. }
  15. }
  16. func (s *Server) ensureWebEnabled(next handleFunc) handleFunc {
  17. return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
  18. if !s.config.EnableWeb {
  19. return errHTTPNotFound
  20. }
  21. return next(w, r, v)
  22. }
  23. }
  24. func (s *Server) ensureUserManager(next handleFunc) handleFunc {
  25. return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
  26. if s.userManager == nil {
  27. return errHTTPNotFound
  28. }
  29. return next(w, r, v)
  30. }
  31. }
  32. func (s *Server) ensureUser(next handleFunc) handleFunc {
  33. return s.ensureUserManager(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
  34. if v.User() == nil {
  35. return errHTTPUnauthorized
  36. }
  37. return next(w, r, v)
  38. })
  39. }
  40. func (s *Server) ensurePaymentsEnabled(next handleFunc) handleFunc {
  41. return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
  42. if s.config.StripeSecretKey == "" || s.stripe == nil {
  43. return errHTTPNotFound
  44. }
  45. return next(w, r, v)
  46. }
  47. }
  48. func (s *Server) ensureStripeCustomer(next handleFunc) handleFunc {
  49. return s.ensureUser(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
  50. if v.User().Billing.StripeCustomerID == "" {
  51. return errHTTPBadRequestNotAPaidUser
  52. }
  53. return next(w, r, v)
  54. })
  55. }
  56. func (s *Server) withAccountSync(next handleFunc) handleFunc {
  57. return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
  58. err := next(w, r, v)
  59. if err == nil {
  60. s.publishSyncEventAsync(v)
  61. }
  62. return err
  63. }
  64. }