|
|
@@ -7,10 +7,31 @@ import (
|
|
|
"io"
|
|
|
"log/slog"
|
|
|
"net/http"
|
|
|
- "reflect"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
+
|
|
|
+ "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
|
|
|
+}
|
|
|
func RaiseParseError(w http.ResponseWriter, key string, value any) {
|
|
|
slog.Error("Failed to parse JSON response",
|
|
|
"key", key,
|
|
|
@@ -42,12 +63,11 @@ func (cfg Config) StoreTicketResponse(ticketarray []any, servername string) erro
|
|
|
}
|
|
|
|
|
|
// TODO: Generate request using Server struct
|
|
|
-// FIX: this should use Server not *Config
|
|
|
-func (cfg *Config) GetUpstreamTickets(w http.ResponseWriter) ([]any, error) {
|
|
|
+func (srv Server) GetUpstreamTickets() ([]any, error) {
|
|
|
var ticketarray []any
|
|
|
var whmcsresponse JSONObject
|
|
|
// We need a new response from the server
|
|
|
- data := cfg.GenerateFormdata()
|
|
|
+ data := srv.GenerateFormdata()
|
|
|
data.Set("action", "GetTickets")
|
|
|
data.Set("limitnum", "100")
|
|
|
data.Set("status", "Awaiting Reply")
|
|
|
@@ -56,25 +76,21 @@ func (cfg *Config) GetUpstreamTickets(w http.ResponseWriter) ([]any, error) {
|
|
|
req, err := http.NewRequestWithContext(
|
|
|
context.Background(),
|
|
|
http.MethodPost,
|
|
|
- cfg.endpoint,
|
|
|
+ srv.endpoint,
|
|
|
strings.NewReader(data.Encode()),
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
slog.Error("Failed to generate request to upstream server for data")
|
|
|
- http.Error(w,
|
|
|
- fmt.Sprintf("Failed to generate request to upstream server for data.\nResponse: %v", err.Error()),
|
|
|
- http.StatusInternalServerError,
|
|
|
- )
|
|
|
- return ticketarray, err
|
|
|
+ return ticketarray, fmt.Errorf("Failed to generate request to upstream server for data.\nResponse: %v", err.Error())
|
|
|
+
|
|
|
}
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
if err != nil {
|
|
|
slog.Error("Upstream server failed.", "error", err.Error())
|
|
|
- http.Error(w, fmt.Sprintf("Upstream server failed %v", err.Error()), http.StatusInternalServerError)
|
|
|
- return ticketarray, err
|
|
|
+ return ticketarray, fmt.Errorf("Upstream server failed %v", err.Error())
|
|
|
}
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
@@ -84,7 +100,6 @@ func (cfg *Config) GetUpstreamTickets(w http.ResponseWriter) ([]any, error) {
|
|
|
"body", string(bodyBytes),
|
|
|
"responsecode", resp.StatusCode,
|
|
|
)
|
|
|
- http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
return ticketarray, err
|
|
|
}
|
|
|
|
|
|
@@ -95,19 +110,16 @@ func (cfg *Config) GetUpstreamTickets(w http.ResponseWriter) ([]any, error) {
|
|
|
msg = ""
|
|
|
}
|
|
|
slog.Error("Upstream server error", "message", msg)
|
|
|
- http.Error(w, msg, http.StatusInternalServerError)
|
|
|
- return ticketarray, err
|
|
|
+ return ticketarray, fmt.Errorf("Got error from upstream: %v", msg)
|
|
|
}
|
|
|
|
|
|
tickets, ok := whmcsresponse["tickets"].(JSONObject)
|
|
|
if !ok {
|
|
|
- RaiseParseError(w, "tickets", whmcsresponse["tickets"])
|
|
|
return ticketarray, fmt.Errorf("Failed to parse JSON from upstream")
|
|
|
}
|
|
|
|
|
|
ticketarray, ok = tickets["ticket"].([]any)
|
|
|
if !ok {
|
|
|
- RaiseParseError(w, "typeof(ticket)", reflect.TypeOf(tickets["ticket"]))
|
|
|
return ticketarray, fmt.Errorf("Failed to parse JSON from upstream")
|
|
|
}
|
|
|
|