mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-03-23 01:54:28 -05:00
Merge branch 'master' into kegan/http-auth
This commit is contained in:
commit
c9c602129d
|
@ -6,7 +6,8 @@ install:
|
||||||
- go get github.com/golang/lint/golint
|
- go get github.com/golang/lint/golint
|
||||||
- go get github.com/fzipp/gocyclo
|
- go get github.com/fzipp/gocyclo
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
webhooks:
|
webhooks:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# Dendrite
|
# Dendrite [](https://travis-ci.org/matrix-org/dendrite)
|
||||||
|
|
||||||
Dendrite will be a matrix homeserver written in go.
|
Dendrite will be a matrix homeserver written in go.
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,40 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/matrix-org/dendrite/clientapi/routing"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/routing"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
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() {
|
func main() {
|
||||||
bindAddr := os.Getenv("BIND_ADDRESS")
|
bindAddr := os.Getenv("BIND_ADDRESS")
|
||||||
if bindAddr == "" {
|
if bindAddr == "" {
|
||||||
log.Panic("No BIND_ADDRESS environment variable found.")
|
log.Panic("No BIND_ADDRESS environment variable found.")
|
||||||
}
|
}
|
||||||
|
logDir := os.Getenv("LOG_DIR")
|
||||||
|
if logDir != "" {
|
||||||
|
setupLogging(logDir)
|
||||||
|
}
|
||||||
log.Info("Starting clientapi")
|
log.Info("Starting clientapi")
|
||||||
routing.Setup(http.DefaultServeMux, http.DefaultClient)
|
routing.Setup(http.DefaultServeMux, http.DefaultClient)
|
||||||
log.Fatal(http.ListenAndServe(bindAddr, nil))
|
log.Fatal(http.ListenAndServe(bindAddr, nil))
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue