| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package main
- import (
- "context"
- "log/slog"
- "net/http"
- "strconv"
- "time"
- "github.com/jackc/pgx/v5/pgtype"
- )
- func SendRSSResponse(w http.ResponseWriter, ticketarray []any, lastupdated pgtype.Timestamp, whmcs string) (int, error) {
- rss, err := TicketsToRSS(ticketarray, lastupdated.Time, whmcs)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return 0, err
- }
- w.Header().Set("Content-Type", "application/rss+xml")
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Length", strconv.Itoa(len(rss)))
- n, err := w.Write([]byte(rss))
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return 0, err
- }
- return n, nil
- }
- // TODO: implement function to add or remove upstream servers
- func (cfg Config) AddUpstream(w http.ResponseWriter, r *http.Request) {}
- func (cfg Config) DelUpstream(w http.ResponseWriter, r *http.Request) {}
- // TODO: use `whmcs` to fetch details of api from server and construct server obj
- func (cfg Config) GetTickets(w http.ResponseWriter, r *http.Request) {
- // this will look for application/x-www-form-urlencoded
- // content type
- err := r.ParseForm()
- if err != nil {
- // if there was an error here, we return the error
- // as response along with a 400 http response code
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- whmcs := r.FormValue("whmcs")
- if whmcs == "" {
- http.Error(w, "No WHMCS upstream name provided. Please provide one as a urlencoded form", http.StatusBadRequest)
- return
- }
- cache := r.FormValue("disallowcache") == ""
- slog.Debug("GetTickets handler",
- "whmcs", whmcs,
- "nocache", !cache,
- )
- slog.Info("Running callback GetTickets", "whmcs", whmcs)
- // Get last ticket update from db
- row := cfg.dbpool.QueryRow(
- context.Background(),
- "SELECT response,lastupdated FROM requestlog WHERE server = $1 ORDER BY lastupdated LIMIT 1",
- whmcs,
- )
- var lastupdated pgtype.Timestamp
- var response []any
- if cache {
- err = row.Scan(&response, &lastupdated)
- now := time.Now()
- diff := now.Sub(lastupdated.Time)
- // check if we have rows and if they are there, are they recent enough
- if err == nil && diff.Abs().Minutes() >= 5.0 {
- slog.Info("DB has cached response.", "lastupdated", lastupdated.Time)
- SendRSSResponse(w, response, lastupdated, whmcs)
- return
- }
- }
- slog.Debug("DB does not have cached response. Requesting server.")
- ticketarray, err := cfg.GetUpstreamTickets(w)
- if err != nil {
- return
- }
- err = cfg.StoreTicketResponse(ticketarray, whmcs)
- if err != nil {
- slog.Error("Error while updating API response in database", "error", err.Error())
- }
- _, err = SendRSSResponse(w, ticketarray, lastupdated, whmcs)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- }
|