Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
Add support for batch request (#3)
Browse files Browse the repository at this point in the history
Assumes each line in request body
should be logged as a separate log line.

text\ntext\ntext
{json}\n{json}\n{json}
  • Loading branch information
aslakknutsen authored Sep 19, 2017
1 parent 23fb4a0 commit 2adba70
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
36 changes: 21 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"

Expand Down Expand Up @@ -55,25 +55,31 @@ func Logger(logger *log.Logger) func(http.ResponseWriter, *http.Request) {
ObjectType: vars[ObjectType],
ObjectName: vars[ObjectName],
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(err.Error()))
return
}

if request.Header.Get("Content-Type") == "application/json" {
var jsonBody map[string]interface{}
err := json.Unmarshal(body, &jsonBody)
bodies := bufio.NewScanner(request.Body)
bodies.Split(bufio.ScanLines)
/*
body, err := ioutil.ReadAll(request.Body)
if err != nil {
LogRaw(logger, metadata, string(body))
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(err.Error()))
return
}
LogJSON(logger, metadata, jsonBody)
} else {
LogRaw(logger, metadata, string(body))
*/
for bodies.Scan() {
body := bodies.Bytes()
if request.Header.Get("Content-Type") == "application/json" {
var jsonBody map[string]interface{}
err := json.Unmarshal(body, &jsonBody)
if err != nil {
LogRaw(logger, metadata, string(body))
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(err.Error()))
return
}
LogJSON(logger, metadata, jsonBody)
} else {
LogRaw(logger, metadata, string(body))
}
}
response.WriteHeader(http.StatusOK)
}
Expand Down
16 changes: 16 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func TestRawOutput(t *testing.T) {
assert.Contains(t, log, "test-on")
}

func TestRawMultiLineOutput(t *testing.T) {
log, status := run(t, "text/plain", "a-b-c\nd-e-f")
assert.Equal(t, http.StatusOK, status)

assert.Contains(t, log, "\"msg\":\"a-b-c\"")
assert.Contains(t, log, "\"msg\":\"d-e-f\"")
}

func TestJSONOutput(t *testing.T) {
log, status := run(t, "application/json", "{\"a\":1}")
assert.Equal(t, http.StatusOK, status)
Expand All @@ -30,6 +38,14 @@ func TestJSONOutput(t *testing.T) {
assert.Contains(t, log, "test-on")
}

func TestJSONMultiOutput(t *testing.T) {
log, status := run(t, "application/json", "{\"a\":1}\n{\"b\":1}\n")
assert.Equal(t, http.StatusOK, status)

assert.Contains(t, log, "{\"a\":1,")
assert.Contains(t, log, "{\"b\":1,")
}

func TestJSONOutputMsgFromOriginal(t *testing.T) {
log, status := run(t, "application/json", "{\"msg\":\"a\"}")
assert.Equal(t, http.StatusOK, status)
Expand Down

0 comments on commit 2adba70

Please sign in to comment.