diff options
author | Kegsay <kegan@matrix.org> | 2020-06-04 15:43:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-04 15:43:07 +0100 |
commit | 9834ac97db042b0c78fbd72652aa534129ca3afe (patch) | |
tree | 4fd862ffba61e47f94b2fcc5685a0502e21b3c70 /eduserver | |
parent | d785ad82b996989381657292fbd2c28b9fbb7df6 (diff) |
Convert everything but serverkeyapi to inthttp (#1096)
* Convert roomserver to new inthttp format
* Convert eduserver to new inthttp format
* Convert appservice to new inthttp format
Diffstat (limited to 'eduserver')
-rw-r--r-- | eduserver/api/input.go | 49 | ||||
-rw-r--r-- | eduserver/eduserver.go | 3 | ||||
-rw-r--r-- | eduserver/input/input.go | 34 | ||||
-rw-r--r-- | eduserver/inthttp/client.go | 56 | ||||
-rw-r--r-- | eduserver/inthttp/server.go | 41 |
5 files changed, 99 insertions, 84 deletions
diff --git a/eduserver/api/input.go b/eduserver/api/input.go index fa7f30cb..0d0d21f3 100644 --- a/eduserver/api/input.go +++ b/eduserver/api/input.go @@ -19,12 +19,8 @@ package api import ( "context" - "errors" - "net/http" - internalHTTP "github.com/matrix-org/dendrite/internal/http" "github.com/matrix-org/gomatrixserverlib" - opentracing "github.com/opentracing/opentracing-go" ) // InputTypingEvent is an event for notifying the typing server about typing updates. @@ -77,48 +73,3 @@ type EDUServerInputAPI interface { response *InputSendToDeviceEventResponse, ) error } - -// EDUServerInputTypingEventPath is the HTTP path for the InputTypingEvent API. -const EDUServerInputTypingEventPath = "/eduserver/input" - -// EDUServerInputSendToDeviceEventPath is the HTTP path for the InputSendToDeviceEvent API. -const EDUServerInputSendToDeviceEventPath = "/eduserver/sendToDevice" - -// NewEDUServerInputAPIHTTP creates a EDUServerInputAPI implemented by talking to a HTTP POST API. -func NewEDUServerInputAPIHTTP(eduServerURL string, httpClient *http.Client) (EDUServerInputAPI, error) { - if httpClient == nil { - return nil, errors.New("NewTypingServerInputAPIHTTP: httpClient is <nil>") - } - return &httpEDUServerInputAPI{eduServerURL, httpClient}, nil -} - -type httpEDUServerInputAPI struct { - eduServerURL string - httpClient *http.Client -} - -// InputTypingEvent implements EDUServerInputAPI -func (h *httpEDUServerInputAPI) InputTypingEvent( - ctx context.Context, - request *InputTypingEventRequest, - response *InputTypingEventResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "InputTypingEvent") - defer span.Finish() - - apiURL := h.eduServerURL + EDUServerInputTypingEventPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// InputSendToDeviceEvent implements EDUServerInputAPI -func (h *httpEDUServerInputAPI) InputSendToDeviceEvent( - ctx context.Context, - request *InputSendToDeviceEventRequest, - response *InputSendToDeviceEventResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "InputSendToDeviceEvent") - defer span.Finish() - - apiURL := h.eduServerURL + EDUServerInputSendToDeviceEventPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} diff --git a/eduserver/eduserver.go b/eduserver/eduserver.go index 6f664eb6..59decc8f 100644 --- a/eduserver/eduserver.go +++ b/eduserver/eduserver.go @@ -21,6 +21,7 @@ import ( "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/eduserver/cache" "github.com/matrix-org/dendrite/eduserver/input" + "github.com/matrix-org/dendrite/eduserver/inthttp" "github.com/matrix-org/dendrite/internal/basecomponent" ) @@ -42,7 +43,7 @@ func SetupEDUServerComponent( ServerName: base.Cfg.Matrix.ServerName, } - inputAPI.SetupHTTP(base.InternalAPIMux) + inthttp.AddRoutes(inputAPI, base.InternalAPIMux) return inputAPI } diff --git a/eduserver/input/input.go b/eduserver/input/input.go index 4e305195..0bbf5b84 100644 --- a/eduserver/input/input.go +++ b/eduserver/input/input.go @@ -19,17 +19,13 @@ package input import ( "context" "encoding/json" - "net/http" "time" "github.com/Shopify/sarama" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/eduserver/cache" - "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/gomatrixserverlib" - "github.com/matrix-org/util" "github.com/sirupsen/logrus" ) @@ -170,33 +166,3 @@ func (t *EDUServerInputAPI) sendToDeviceEvent(ise *api.InputSendToDeviceEvent) e return nil } - -// SetupHTTP adds the EDUServerInputAPI handlers to the http.ServeMux. -func (t *EDUServerInputAPI) SetupHTTP(internalAPIMux *mux.Router) { - internalAPIMux.Handle(api.EDUServerInputTypingEventPath, - internal.MakeInternalAPI("inputTypingEvents", func(req *http.Request) util.JSONResponse { - var request api.InputTypingEventRequest - var response api.InputTypingEventResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := t.InputTypingEvent(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.EDUServerInputSendToDeviceEventPath, - internal.MakeInternalAPI("inputSendToDeviceEvents", func(req *http.Request) util.JSONResponse { - var request api.InputSendToDeviceEventRequest - var response api.InputSendToDeviceEventResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := t.InputSendToDeviceEvent(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) -} diff --git a/eduserver/inthttp/client.go b/eduserver/inthttp/client.go new file mode 100644 index 00000000..149e4fb0 --- /dev/null +++ b/eduserver/inthttp/client.go @@ -0,0 +1,56 @@ +package inthttp + +import ( + "context" + "errors" + "net/http" + + "github.com/matrix-org/dendrite/eduserver/api" + internalHTTP "github.com/matrix-org/dendrite/internal/http" + "github.com/opentracing/opentracing-go" +) + +// HTTP paths for the internal HTTP APIs +const ( + EDUServerInputTypingEventPath = "/eduserver/input" + EDUServerInputSendToDeviceEventPath = "/eduserver/sendToDevice" +) + +// NewEDUServerClient creates a EDUServerInputAPI implemented by talking to a HTTP POST API. +func NewEDUServerClient(eduServerURL string, httpClient *http.Client) (api.EDUServerInputAPI, error) { + if httpClient == nil { + return nil, errors.New("NewEDUServerClient: httpClient is <nil>") + } + return &httpEDUServerInputAPI{eduServerURL, httpClient}, nil +} + +type httpEDUServerInputAPI struct { + eduServerURL string + httpClient *http.Client +} + +// InputTypingEvent implements EDUServerInputAPI +func (h *httpEDUServerInputAPI) InputTypingEvent( + ctx context.Context, + request *api.InputTypingEventRequest, + response *api.InputTypingEventResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "InputTypingEvent") + defer span.Finish() + + apiURL := h.eduServerURL + EDUServerInputTypingEventPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// InputSendToDeviceEvent implements EDUServerInputAPI +func (h *httpEDUServerInputAPI) InputSendToDeviceEvent( + ctx context.Context, + request *api.InputSendToDeviceEventRequest, + response *api.InputSendToDeviceEventResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "InputSendToDeviceEvent") + defer span.Finish() + + apiURL := h.eduServerURL + EDUServerInputSendToDeviceEventPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} diff --git a/eduserver/inthttp/server.go b/eduserver/inthttp/server.go new file mode 100644 index 00000000..6c7d21a4 --- /dev/null +++ b/eduserver/inthttp/server.go @@ -0,0 +1,41 @@ +package inthttp + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/eduserver/api" + "github.com/matrix-org/dendrite/internal" + "github.com/matrix-org/util" +) + +// AddRoutes adds the EDUServerInputAPI handlers to the http.ServeMux. +func AddRoutes(t api.EDUServerInputAPI, internalAPIMux *mux.Router) { + internalAPIMux.Handle(EDUServerInputTypingEventPath, + internal.MakeInternalAPI("inputTypingEvents", func(req *http.Request) util.JSONResponse { + var request api.InputTypingEventRequest + var response api.InputTypingEventResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := t.InputTypingEvent(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(EDUServerInputSendToDeviceEventPath, + internal.MakeInternalAPI("inputSendToDeviceEvents", func(req *http.Request) util.JSONResponse { + var request api.InputSendToDeviceEventRequest + var response api.InputSendToDeviceEventResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := t.InputSendToDeviceEvent(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) +} |