-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Financial-Times/feature/improve-query
Improve Mongo Query
- Loading branch information
Showing
7 changed files
with
178 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package resources | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/Sirupsen/logrus" | ||
) | ||
|
||
// UpdateLogLevel changes the logrus log level dynamically. | ||
func UpdateLogLevel() func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
level := struct { | ||
Level string `json:"level"` | ||
}{} | ||
|
||
dec := json.NewDecoder(r.Body) | ||
err := dec.Decode(&level) | ||
|
||
if err != nil { | ||
writeError("Please specify one of [debug, info]", 400, w) | ||
return | ||
} | ||
|
||
switch level.Level { | ||
case "debug": | ||
logrus.SetLevel(logrus.DebugLevel) | ||
logrus.Debug("Log level updated to debug.") | ||
break | ||
case "info": | ||
logrus.SetLevel(logrus.InfoLevel) | ||
logrus.Info("Log level updated to info.") | ||
break | ||
default: | ||
writeError("Please specify one of [debug, info]", 400, w) | ||
return | ||
} | ||
|
||
writeMessage(200, "Log level changed to "+level.Level, w) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package resources | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDebugLogLevel(t *testing.T) { | ||
req, _ := http.NewRequest("POST", "http://our.host.name/__log", strings.NewReader(`{"level": "debug"}`)) | ||
|
||
w := httptest.NewRecorder() | ||
UpdateLogLevel()(w, req) | ||
|
||
assert.Equal(t, 200, w.Code) | ||
assert.Equal(t, logrus.DebugLevel, logrus.GetLevel()) | ||
} | ||
|
||
func TestInfoLogLevel(t *testing.T) { | ||
req, _ := http.NewRequest("POST", "http://our.host.name/__log", strings.NewReader(`{"level": "info"}`)) | ||
|
||
w := httptest.NewRecorder() | ||
UpdateLogLevel()(w, req) | ||
|
||
assert.Equal(t, 200, w.Code) | ||
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel()) | ||
} | ||
|
||
func TestUnsupportedLogLevel(t *testing.T) { | ||
expected := logrus.GetLevel() | ||
req, _ := http.NewRequest("POST", "http://our.host.name/__log", strings.NewReader(`{"level": "warn"}`)) | ||
|
||
w := httptest.NewRecorder() | ||
UpdateLogLevel()(w, req) | ||
|
||
assert.Equal(t, 400, w.Code) | ||
assert.Equal(t, expected, logrus.GetLevel()) | ||
} | ||
|
||
func TestLevelNotJson(t *testing.T) { | ||
expected := logrus.GetLevel() | ||
req, _ := http.NewRequest("POST", "http://our.host.name/__log", strings.NewReader(`{"level": "where's my closing quote?}`)) | ||
|
||
w := httptest.NewRecorder() | ||
UpdateLogLevel()(w, req) | ||
|
||
assert.Equal(t, 400, w.Code) | ||
assert.Equal(t, expected, logrus.GetLevel()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters