handler.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "context"
  4. "log/slog"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/jackc/pgx/v5/pgtype"
  9. )
  10. func SendRSSResponse(w http.ResponseWriter, ticketarray []any, lastupdated pgtype.Timestamp, whmcs string) (int, error) {
  11. rss, err := TicketsToRSS(ticketarray, lastupdated.Time, whmcs)
  12. if err != nil {
  13. http.Error(w, err.Error(), http.StatusInternalServerError)
  14. return 0, err
  15. }
  16. w.Header().Set("Content-Type", "application/rss+xml")
  17. w.WriteHeader(http.StatusOK)
  18. w.Header().Set("Content-Length", strconv.Itoa(len(rss)))
  19. n, err := w.Write([]byte(rss))
  20. if err != nil {
  21. http.Error(w, err.Error(), http.StatusInternalServerError)
  22. return 0, err
  23. }
  24. return n, nil
  25. }
  26. // TODO: implement function to add or remove upstream servers
  27. func (cfg Config) AddUpstream(w http.ResponseWriter, r *http.Request) {}
  28. func (cfg Config) DelUpstream(w http.ResponseWriter, r *http.Request) {}
  29. // TODO: use `whmcs` to fetch details of api from server and construct server obj
  30. func (cfg Config) GetTickets(w http.ResponseWriter, r *http.Request) {
  31. // this will look for application/x-www-form-urlencoded
  32. // content type
  33. err := r.ParseForm()
  34. if err != nil {
  35. // if there was an error here, we return the error
  36. // as response along with a 400 http response code
  37. http.Error(w, err.Error(), http.StatusBadRequest)
  38. return
  39. }
  40. whmcs := r.FormValue("whmcs")
  41. if whmcs == "" {
  42. http.Error(w, "No WHMCS upstream name provided. Please provide one as a urlencoded form", http.StatusBadRequest)
  43. return
  44. }
  45. cache := r.FormValue("disallowcache") == ""
  46. slog.Debug("GetTickets handler",
  47. "whmcs", whmcs,
  48. "nocache", !cache,
  49. )
  50. slog.Info("Running callback GetTickets", "whmcs", whmcs)
  51. // Get last ticket update from db
  52. row := cfg.dbpool.QueryRow(
  53. context.Background(),
  54. "SELECT response,lastupdated FROM requestlog WHERE server = $1 ORDER BY lastupdated LIMIT 1",
  55. whmcs,
  56. )
  57. var lastupdated pgtype.Timestamp
  58. var response []any
  59. if cache {
  60. err = row.Scan(&response, &lastupdated)
  61. now := time.Now()
  62. diff := now.Sub(lastupdated.Time)
  63. // check if we have rows and if they are there, are they recent enough
  64. if err == nil && diff.Abs().Minutes() >= 5.0 {
  65. slog.Info("DB has cached response.", "lastupdated", lastupdated.Time)
  66. SendRSSResponse(w, response, lastupdated, whmcs)
  67. return
  68. }
  69. }
  70. slog.Debug("DB does not have cached response. Requesting server.")
  71. ticketarray, err := cfg.GetUpstreamTickets(w)
  72. if err != nil {
  73. return
  74. }
  75. err = cfg.StoreTicketResponse(ticketarray, whmcs)
  76. if err != nil {
  77. slog.Error("Error while updating API response in database", "error", err.Error())
  78. }
  79. _, err = SendRSSResponse(w, ticketarray, lastupdated, whmcs)
  80. if err != nil {
  81. http.Error(w, err.Error(), http.StatusInternalServerError)
  82. return
  83. }
  84. }