Skip to content

Commit

Permalink
Moving the string formatting to before the response is sent to preven…
Browse files Browse the repository at this point in the history
…t massive packages from being sent by accident
  • Loading branch information
COMTOP1 committed Feb 16, 2024
1 parent a1de6b6 commit 49ffc8e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
17 changes: 14 additions & 3 deletions forwarder/forwarder_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"bytes"
"fmt"
commonTransporter "github.com/ystv/streamer/common/transporter"
"log"
"os/exec"
"strings"

commonTransporter "github.com/ystv/streamer/common/transporter"
)

func (v *Views) status(transporter commonTransporter.Transporter) (commonTransporter.ForwarderStatusResponse, error) {
Expand Down Expand Up @@ -66,10 +68,19 @@ func (v *Views) status(transporter commonTransporter.Transporter) (commonTranspo
}

log.Println(15)
var response string
tempRespArr := strings.Split(strings.TrimRight(stdout.String(), "\r"), "\r")
if len(tempRespArr) == 0 {
response = fmt.Sprintf("failed to get message response from forwarder for stream %d", i)
} else {
response = strings.ReplaceAll(tempRespArr[0], "\n", "<br>")
response += "<br>"
response += tempRespArr[len(tempRespArr)-1]
}
if i == 0 {
fStatusResponse.Website = stdout.String()
fStatusResponse.Website = response
} else {
fStatusResponse.Streams[fmt.Sprintf("%d", i)] = stdout.String()
fStatusResponse.Streams[fmt.Sprintf("%d", i)] = response
}
log.Println(16)
}
Expand Down
12 changes: 11 additions & 1 deletion recorder/recorder_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"os/exec"
"strings"

commonTransporter "github.com/ystv/streamer/common/transporter"
)
Expand All @@ -31,5 +32,14 @@ func (v *Views) status(transporter commonTransporter.Transporter) (string, error
return "", fmt.Errorf(errOut)
}

return stdout.String(), nil
var response string
tempRespArr := strings.Split(strings.TrimRight(stdout.String(), "\r"), "\r")
if len(tempRespArr) == 0 {
response = "failed to get message response from recorder"
} else {
response = strings.ReplaceAll(tempRespArr[0], "\n", "<br>")
response += "<br>"
response += tempRespArr[len(tempRespArr)-1]
}
return response, nil
}
57 changes: 28 additions & 29 deletions server/views/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"net/http"
"strings"
"sync"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -164,43 +163,43 @@ func (v *Views) StatusFunc(c echo.Context) error {
}

if len(forwarderStatus.Website) > 0 {
tempRespArr := strings.Split(strings.TrimRight(forwarderStatus.Website, "\r"), "\r")
//tempRespArr := strings.Split(strings.TrimRight(forwarderStatus.Website, "\r"), "\r")
var individualResponse StatusResponseIndividual

Check failure on line 167 in server/views/status.go

View workflow job for this annotation

GitHub Actions / lint-server

S1021: should merge variable declaration with assignment on next line (gosimple)
if len(tempRespArr) == 0 {
individualResponse = StatusResponseIndividual{
Name: "website",
Error: "failed to get message response from forwarder for website",
}
} else {
responseBuild := strings.ReplaceAll(tempRespArr[0], "\n", "<br>")
responseBuild += "<br>"
responseBuild += tempRespArr[len(tempRespArr)-1]
individualResponse = StatusResponseIndividual{
Name: "website",
Response: responseBuild,
}
//if len(tempRespArr) == 0 {
// individualResponse = StatusResponseIndividual{
// Name: "website",
// Error: "failed to get message response from forwarder for website",
// }
//} else {
//responseBuild := strings.ReplaceAll(tempRespArr[0], "\n", "<br>")
//responseBuild += "<br>"
//responseBuild += tempRespArr[len(tempRespArr)-1]
individualResponse = StatusResponseIndividual{
Name: "website",
Response: forwarderStatus.Website,
}
//}
statusResponse.Status = append(statusResponse.Status, individualResponse)
//m["website"] = forwarderStatus.Website
}

for index, streamOut := range forwarderStatus.Streams {
tempRespArr := strings.Split(strings.TrimRight(streamOut, "\r"), "\r")
//tempRespArr := strings.Split(strings.TrimRight(streamOut, "\r"), "\r")
var individualResponse StatusResponseIndividual

Check failure on line 188 in server/views/status.go

View workflow job for this annotation

GitHub Actions / lint-server

S1021: should merge variable declaration with assignment on next line (gosimple)
if len(tempRespArr) == 0 {
individualResponse = StatusResponseIndividual{
Name: index,
Error: "failed to get message response from forwarder for stream " + index,
}
} else {
responseBuild := strings.ReplaceAll(tempRespArr[0], "\n", "<br>")
responseBuild += "<br>"
responseBuild += tempRespArr[len(tempRespArr)-1]
individualResponse = StatusResponseIndividual{
Name: index,
Response: responseBuild,
}
//if len(tempRespArr) == 0 {
// individualResponse = StatusResponseIndividual{
// Name: index,
// Error: "failed to get message response from forwarder for stream " + index,
// }
//} else {
// responseBuild := strings.ReplaceAll(tempRespArr[0], "\n", "<br>")
// responseBuild += "<br>"
// responseBuild += tempRespArr[len(tempRespArr)-1]
individualResponse = StatusResponseIndividual{
Name: index,
Response: streamOut,
}
//}
statusResponse.Status = append(statusResponse.Status, individualResponse)
//m[strconv.Itoa(int(index))] = streamOut
}
Expand Down

0 comments on commit 49ffc8e

Please sign in to comment.