diff --git a/cmd/server/main.go b/cmd/server/main.go index b05b943..6570210 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -12,6 +12,7 @@ import ( "github.com/speakeasy-api/speakeasy-api-test-service/internal/ecommerce" "github.com/speakeasy-api/speakeasy-api-test-service/internal/errors" "github.com/speakeasy-api/speakeasy-api-test-service/internal/eventstreams" + "github.com/speakeasy-api/speakeasy-api-test-service/internal/jsonLines" "github.com/speakeasy-api/speakeasy-api-test-service/internal/method" "github.com/speakeasy-api/speakeasy-api-test-service/internal/middleware" "github.com/speakeasy-api/speakeasy-api-test-service/internal/pagination" @@ -63,6 +64,8 @@ func main() { r.HandleFunc("/eventstreams/chat-chunked", eventstreams.HandleEventStreamChat).Methods(http.MethodPost) r.HandleFunc("/eventstreams/differentdataschemas", eventstreams.HandleEventStreamDifferentDataSchemas).Methods(http.MethodPost) r.HandleFunc("/eventstreams/differentdataschemas-flat", eventstreams.HandleEventStreamDifferentDataSchemasFlatten).Methods(http.MethodPost) + r.HandleFunc("/jsonl", jsonLines.HandleJSONLinesRich).Methods(http.MethodGet) + r.HandleFunc("/jsonl/chunks", jsonLines.HandleJSONLinesChunksRich).Methods(http.MethodGet) r.HandleFunc("/clientcredentials/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost) r.HandleFunc("/clientcredentials/authenticatedrequest", clientcredentials.HandleAuthenticatedRequest).Methods(http.MethodPost) r.HandleFunc("/clientcredentials/alt/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost) diff --git a/internal/jsonLines/service.go b/internal/jsonLines/service.go new file mode 100644 index 0000000..8bc5e74 --- /dev/null +++ b/internal/jsonLines/service.go @@ -0,0 +1,56 @@ +package jsonLines + +import ( + "fmt" + "net/http" + "time" +) + +func pushEvents(rw http.ResponseWriter, events [][]string) { + for _, event := range events { + for _, line := range event { + fmt.Fprint(rw, line) + } + + if f, ok := rw.(http.Flusher); ok { + f.Flush() + } + + time.Sleep(100 * time.Millisecond) + } +} + +func pushChunks(rw http.ResponseWriter, chunks []string) { + for _, chunk := range chunks { + fmt.Fprint(rw, chunk) + + if f, ok := rw.(http.Flusher); ok { + f.Flush() + } + + time.Sleep(100 * time.Millisecond) + } +} + +func HandleJSONLinesChunksRich(rw http.ResponseWriter, _ *http.Request) { + rw.Header().Add("Content-Type", "application/jsonl") + + pushChunks(rw, []string{ + "{\"name\": \"Peter\", \"skills\": [\"Go\"", + ", \"Python\"]}\n{\"name\": \"John\"", + ", \"skills\": [\"Go\", \"Rust\"]}\n", + }) +} + +func HandleJSONLinesRich(rw http.ResponseWriter, _ *http.Request) { + rw.Header().Add("Content-Type", "application/jsonl") + + pushEvents(rw, [][]string{ + { + "{\"name\": \"Peter\", \"skills\": [\"Go\", \"Python\"]}\n", + }, + { + "{\"name\": \"John\", \"skills\": [\"Go\", \"Rust\"]}\n", + }, + }) +}