From 5c34caa1c1ebdb5b8f085358fca54f1a6651cc12 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Mon, 20 Feb 2017 16:14:23 +0000 Subject: [PATCH 1/6] Implement logging to file via dugong (#12) --- .../dendrite/clientapi/clientapi.go | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/clientapi.go b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go index b646904d9..95055b29e 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/clientapi.go +++ b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go @@ -1,18 +1,40 @@ package main import ( - "github.com/matrix-org/dendrite/clientapi/routing" "net/http" "os" + "path/filepath" + + "github.com/matrix-org/dendrite/clientapi/routing" log "github.com/Sirupsen/logrus" + "github.com/matrix-org/dugong" ) +func setupLogging(logDir string) { + _ = os.Mkdir(logDir, os.ModePerm) + log.AddHook(dugong.NewFSHook( + filepath.Join(logDir, "info.log"), + filepath.Join(logDir, "warn.log"), + filepath.Join(logDir, "error.log"), + &log.TextFormatter{ + TimestampFormat: "2006-01-02 15:04:05.000000", + DisableColors: true, + DisableTimestamp: false, + DisableSorting: false, + }, &dugong.DailyRotationSchedule{GZip: true}, + )) +} + func main() { bindAddr := os.Getenv("BIND_ADDRESS") if bindAddr == "" { log.Panic("No BIND_ADDRESS environment variable found.") } + logDir := os.Getenv("LOG_DIR") + if logDir != "" { + setupLogging(logDir) + } log.Info("Starting clientapi") routing.Setup(http.DefaultServeMux, http.DefaultClient) log.Fatal(http.ListenAndServe(bindAddr, nil)) From c165aff29dfa4358b3aa2cae1ac8070eb6be08b1 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Mon, 20 Feb 2017 16:57:55 +0000 Subject: [PATCH 2/6] Add build badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e3fe4e197..636cc49e1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Dendrite +# Dendrite [![Build Status](https://travis-ci.org/matrix-org/dendrite.svg?branch=master)](https://travis-ci.org/matrix-org/dendrite) Dendrite will be a matrix homeserver written in go. From b8cee07d0b6f055f839aee817a81313ff274f65e Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 20 Feb 2017 17:13:59 +0000 Subject: [PATCH 3/6] Add coveralls.io? --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7b652cd25..fb80c957e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,11 @@ install: - go get github.com/constabulary/gb/... - go get github.com/golang/lint/golint - go get github.com/fzipp/gocyclo + - go get github.com/mattn/goveralls -script: gb build github.com/matrix-org/dendrite/roomserver/roomserver && ./hooks/pre-commit +script: + - gb build github.com/matrix-org/dendrite/roomserver/roomserver && ./hooks/pre-commit + - $HOME/gopath/bin/goveralls -service=travis-ci notifications: webhooks: From d32397fbc86aa7c8938c9cdfbb53a4f6340a13f4 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Mon, 20 Feb 2017 17:14:56 +0000 Subject: [PATCH 4/6] Add coverage badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 636cc49e1..9981c20b2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Dendrite [![Build Status](https://travis-ci.org/matrix-org/dendrite.svg?branch=master)](https://travis-ci.org/matrix-org/dendrite) +# Dendrite [![Build Status](https://travis-ci.org/matrix-org/dendrite.svg?branch=master)](https://travis-ci.org/matrix-org/dendrite) [![Coverage Status](https://coveralls.io/repos/github/matrix-org/dendrite/badge.svg?branch=master)](https://coveralls.io/github/matrix-org/dendrite?branch=master) Dendrite will be a matrix homeserver written in go. From 434e525ff3db12b414702f967c1083cb950a83b8 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 20 Feb 2017 17:19:27 +0000 Subject: [PATCH 5/6] Remove coveralls. It doesn't work with gb --- .travis.yml | 2 -- README.md | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index fb80c957e..c9ad822cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,9 @@ install: - go get github.com/constabulary/gb/... - go get github.com/golang/lint/golint - go get github.com/fzipp/gocyclo - - go get github.com/mattn/goveralls script: - gb build github.com/matrix-org/dendrite/roomserver/roomserver && ./hooks/pre-commit - - $HOME/gopath/bin/goveralls -service=travis-ci notifications: webhooks: diff --git a/README.md b/README.md index 9981c20b2..636cc49e1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Dendrite [![Build Status](https://travis-ci.org/matrix-org/dendrite.svg?branch=master)](https://travis-ci.org/matrix-org/dendrite) [![Coverage Status](https://coveralls.io/repos/github/matrix-org/dendrite/badge.svg?branch=master)](https://coveralls.io/github/matrix-org/dendrite?branch=master) +# Dendrite [![Build Status](https://travis-ci.org/matrix-org/dendrite.svg?branch=master)](https://travis-ci.org/matrix-org/dendrite) Dendrite will be a matrix homeserver written in go. From 966d3b95b7b78b3e6fdf502d026e1fa70547de40 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Mon, 20 Feb 2017 17:20:49 +0000 Subject: [PATCH 6/6] Add error package and some Matrix errors (#13) Terse function names are used to make usage not stutter. For example: ```go err := error.Forbidden("you shall not pass") ``` At the moment they are all the same fundamental `MatrixError` type. This will be bad if we ever want to `switch` based on the kind of error. I'm hoping we won't ever need to introspect into errors like this: ideally these errors would be created purely for immediately being returned in an HTTP response. `MatrixError` implements the `error` interface. --- .../dendrite/clientapi/jsonerror/jsonerror.go | 62 +++++++++++++++++++ .../clientapi/jsonerror/jsonerror_test.go | 30 +++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go create mode 100644 src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror_test.go diff --git a/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go new file mode 100644 index 000000000..a0111197c --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go @@ -0,0 +1,62 @@ +package jsonerror + +import "fmt" + +// MatrixError represents the "standard error response" in Matrix. +// http://matrix.org/docs/spec/client_server/r0.2.0.html#api-standards +type MatrixError struct { + ErrCode string `json:"errcode"` + Err string `json:"error"` +} + +func (e *MatrixError) Error() string { + return fmt.Sprintf("%s: %s", e.ErrCode, e.Err) +} + +// Forbidden is an error when the client tries to access a resource +// they are not allowed to access. +func Forbidden(msg string) *MatrixError { + return &MatrixError{"M_FORBIDDEN", msg} +} + +// BadJSON is an error when the client supplies malformed JSON. +func BadJSON(msg string) *MatrixError { + return &MatrixError{"M_BAD_JSON", msg} +} + +// NotJSON is an error when the client supplies something that is not JSON +// to a JSON endpoint. +func NotJSON(msg string) *MatrixError { + return &MatrixError{"M_NOT_JSON", msg} +} + +// NotFound is an error when the client tries to access an unknown resource. +func NotFound(msg string) *MatrixError { + return &MatrixError{"M_NOT_FOUND", msg} +} + +// MissingToken is an error when the client tries to access a resource which +// requires authentication without supplying credentials. +func MissingToken(msg string) *MatrixError { + return &MatrixError{"M_MISSING_TOKEN", msg} +} + +// UnknownToken is an error when the client tries to access a resource which +// requires authentication and supplies a valid, but out-of-date token. +func UnknownToken(msg string) *MatrixError { + return &MatrixError{"M_UNKNOWN_TOKEN", msg} +} + +// LimitExceededError is a rate-limiting error. +type LimitExceededError struct { + MatrixError + RetryAfterMS int64 `json:"retry_after_ms,omitempty"` +} + +// LimitExceeded is an error when the client tries to send events too quickly. +func LimitExceeded(msg string, retryAfterMS int64) *LimitExceededError { + return &LimitExceededError{ + MatrixError: MatrixError{"M_LIMIT_EXCEEDED", msg}, + RetryAfterMS: retryAfterMS, + } +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror_test.go b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror_test.go new file mode 100644 index 000000000..33e2ede83 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror_test.go @@ -0,0 +1,30 @@ +package jsonerror + +import ( + "encoding/json" + "testing" +) + +func TestLimitExceeded(t *testing.T) { + e := LimitExceeded("too fast", 5000) + jsonBytes, err := json.Marshal(&e) + if err != nil { + t.Fatalf("TestLimitExceeded: Failed to marshal LimitExceeded error. %s", err.Error()) + } + want := `{"errcode":"M_LIMIT_EXCEEDED","error":"too fast","retry_after_ms":5000}` + if string(jsonBytes) != want { + t.Errorf("TestLimitExceeded: want %s, got %s", want, string(jsonBytes)) + } +} + +func TestForbidden(t *testing.T) { + e := Forbidden("you shall not pass") + jsonBytes, err := json.Marshal(&e) + if err != nil { + t.Fatalf("TestForbidden: Failed to marshal Forbidden error. %s", err.Error()) + } + want := `{"errcode":"M_FORBIDDEN","error":"you shall not pass"}` + if string(jsonBytes) != want { + t.Errorf("TestForbidden: want %s, got %s", want, string(jsonBytes)) + } +}