Skip to content

Commit

Permalink
Changed start to entirely json response
Browse files Browse the repository at this point in the history
  • Loading branch information
COMTOP1 committed Jan 15, 2024
1 parent 0888f53 commit 79b3d49
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
4 changes: 2 additions & 2 deletions server/templates/main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@
contentType: 'application/x-www-form-urlencoded',
data: $('#stream_outs').serialize(),
success: function (data) {
if (data.includes("An error has occurred...\n")) {
if (data.error.length > 0) {
error.html(data);
startPressed = false;
return;
}
$('#unique').html(data);
$('#unique').html(data.unique);
document.getElementById('start_window').hidden = true;
document.getElementById('start_window').style.visibility = "hidden";
document.getElementById('started_window').hidden = false;
Expand Down
68 changes: 43 additions & 25 deletions server/views/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ func (v *Views) StartFunc(c echo.Context) error {
PathOut: c.FormValue("save_path"),
}

var response struct {
Unique string `json:"unique"`
Error string `json:"error"`
}

recording := false
websiteStream := false
if c.FormValue("website_stream") == "on" {
websiteStream = true
if v.websiteCheck(c.FormValue("website_stream_endpoint")) {
fStart.WebsiteOut = c.FormValue("website_stream_endpoint")
} else {
return fmt.Errorf("website key check has failed")
response.Error = "website key check has failed"
return c.JSON(http.StatusOK, response)
}
}

Expand Down Expand Up @@ -77,7 +83,8 @@ func (v *Views) StartFunc(c echo.Context) error {

streams1, err := v.store.GetStreams()
if err != nil {
return fmt.Errorf("failed to get streams: %w", err)
response.Error = fmt.Sprintf("failed to get streams: %+v", err)
return c.JSON(http.StatusOK, response)
}

if len(streams1) == 0 {
Expand All @@ -94,7 +101,8 @@ func (v *Views) StartFunc(c echo.Context) error {

stored, err := v.store.GetStored()
if err != nil {
return fmt.Errorf("failed to get stored: %w", err)
response.Error = fmt.Sprintf("failed to get stored: %+v", err)
return c.JSON(http.StatusOK, response)
}

if len(stored) == 0 {
Expand Down Expand Up @@ -126,27 +134,27 @@ func (v *Views) StartFunc(c echo.Context) error {

var wg sync.WaitGroup
wg.Add(2)
errors := false
var errorMessages []string
go func() {
defer wg.Done()
if c.FormValue("record") == "on" {
recording = true
recorderTransporter := transporter
recorderTransporter.Payload = rStart
response, err := v.wsHelper(server.Recorder, recorderTransporter)
wsResponse, err := v.wsHelper(server.Recorder, recorderTransporter)
if err != nil {
log.Println(err, "Error sending to Recorder for start")
errors = true
log.Printf("failed sending to Recorder for start: %+v", err)
errorMessages = append(errorMessages, fmt.Sprintf("failed sending to Recorder for start: %+v", err))
return
}
if response.Status == wsMessages.Error {
log.Printf("Error sending to Recorder for start: %s", response.Payload)
errors = true
if wsResponse.Status == wsMessages.Error {
log.Printf("failed sending to Recorder for start: %s", wsResponse.Payload)
errorMessages = append(errorMessages, fmt.Sprintf("failed sending to Recorder for start: %s", wsResponse.Payload))
return
}
if response.Status != wsMessages.Okay {
log.Printf("invalid response from Recorder for start: %s", response.Status)
errors = true
if wsResponse.Status != wsMessages.Okay {
log.Printf("invalid response from Recorder for start: %s", wsResponse.Status)
errorMessages = append(errorMessages, fmt.Sprintf("invalid response from Recorder for start: %s", wsResponse.Status))
return
}
}
Expand All @@ -155,26 +163,26 @@ func (v *Views) StartFunc(c echo.Context) error {
defer wg.Done()
forwarderTransporter := transporter
forwarderTransporter.Payload = fStart
response, err := v.wsHelper(server.Forwarder, forwarderTransporter)
wsResponse, err := v.wsHelper(server.Forwarder, forwarderTransporter)
if err != nil {
log.Println(err, "Error sending to Forwarder for start")
errors = true
log.Printf("failed sending to Forwarder for start: %+v", err)
errorMessages = append(errorMessages, fmt.Sprintf("failed sending to Forwarder for start: %+v", err))
return
}
if response.Status == wsMessages.Error {
log.Printf("Error sending to Forwarder for start: %s", response)
errors = true
if wsResponse.Status == wsMessages.Error {
log.Printf("failed sending to Forwarder for start: %s", response)
errorMessages = append(errorMessages, fmt.Sprintf("failed sending to Forwarder for start: %s", response))
return
}
if response.Status != wsMessages.Okay {
if wsResponse.Status != wsMessages.Okay {
log.Printf("invalid response from Forwarder for start: %s", response)
errors = true
errorMessages = append(errorMessages, fmt.Sprintf("invalid response from Forwarder for start: %s", response))
return
}
}()
wg.Wait()

if !errors {
if len(errorMessages) == 0 {
err := v.HandleTXLight(v.conf.TransmissionLight, tx.TransmissionOn)
if err != nil {
log.Printf("failed to turn transmission light on: %+v, ignoring and continuing", err)
Expand All @@ -188,14 +196,24 @@ func (v *Views) StartFunc(c echo.Context) error {
Streams: uint64(len(streams)),
})
if err != nil {
return fmt.Errorf("failed to add stream for start: %w", err)
log.Printf("invalid response from Forwarder for start: %s", response)
errorMessages = append(errorMessages, fmt.Sprintf("invalid response from Forwarder for start: %s", response))
response.Error = strings.Join(errorMessages, ",")
return c.JSON(http.StatusOK, response)
}

if s == nil {
return fmt.Errorf("failed to add stream, data is empty")
log.Printf("failed to add stream, data is empty")
errorMessages = append(errorMessages, "failed to add stream, data is empty")
response.Error = strings.Join(errorMessages, ",")
return c.JSON(http.StatusOK, response)
}

return c.String(http.StatusOK, string(b))
response.Unique = string(b)
return c.JSON(http.StatusOK, response)
} else {
response.Error = strings.Join(errorMessages, ",")
return c.JSON(http.StatusOK, response)
}
}
return echo.NewHTTPError(http.StatusMethodNotAllowed, "invalid method")
Expand Down

0 comments on commit 79b3d49

Please sign in to comment.