Skip to content

Commit

Permalink
debug: testing SSE API with proxy client
Browse files Browse the repository at this point in the history
  • Loading branch information
exu committed Nov 20, 2021
1 parent 182b5c4 commit f1642a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion internal/app/api/v1/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func (s testkubeAPI) ExecutionLogs() fiber.Handler {
logs, err = s.Executor.Logs(executionID)
s.Log.Debugw("waiting for jobs channel", "channelSize", len(logs))
if err != nil {
// TODO convert to some library for common output
fmt.Fprintf(w, `data: {"type": "error","message": "%s"}\n\n`, err.Error())
s.Log.Errorw("getting logs error", "error", err)
w.Flush()
Expand All @@ -277,7 +278,8 @@ func (s testkubeAPI) ExecutionLogs() fiber.Handler {
s.Log.Debugw("got log", "out", out)
fmt.Fprintf(w, "data: ")
enc.Encode(out)
fmt.Fprintf(w, "\n\n")
// enc.Encode adds \n and we need \n\n after `data: {}` chunk
fmt.Fprintf(w, "\n")
w.Flush()
}
}))
Expand Down
2 changes: 0 additions & 2 deletions pkg/api/v1/client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ func trimDataChunk(in []byte) []byte {
chunk := bytes.Replace(in, prefix, []byte{}, 1)
chunk = bytes.Replace(chunk, postfix, []byte{}, 1)

// chunk = chunk[:len(chunk)-4]

return chunk
}
12 changes: 12 additions & 0 deletions pkg/api/v1/client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -176,6 +177,17 @@ func (c ProxyScriptsAPI) Logs(id string) (logs chan output.Output, err error) {
SetHeader("Accept", "text/event-stream").
Stream(context.Background())

b := make([]byte, 1000)
for {
n, err := resp.Read(b)
fmt.Printf("n = %v err = %v b = %v\n", n, err, b)
fmt.Printf("b[:n] = %q\n", b[:n])
if err == io.EOF {
break
}

}

go func() {
defer close(logs)
defer resp.Close()
Expand Down

0 comments on commit f1642a4

Please sign in to comment.