Add push server component template

This commit is contained in:
Neil Alexander 2021-05-05 11:45:28 +01:00
parent 092edee210
commit 6843c3beee
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
22 changed files with 487 additions and 0 deletions

View file

@ -0,0 +1,48 @@
package inthttp
import (
"context"
"errors"
"net/http"
"github.com/matrix-org/dendrite/internal/httputil"
"github.com/matrix-org/dendrite/pushserver/api"
"github.com/opentracing/opentracing-go"
)
type httpPushserverInternalAPI struct {
roomserverURL string
httpClient *http.Client
}
const (
PushserverQueryExamplePath = "/pushserver/queryExample"
)
// NewRoomserverClient creates a PushserverInternalAPI implemented by talking to a HTTP POST API.
// If httpClient is nil an error is returned
func NewPushserverClient(
pushserverURL string,
httpClient *http.Client,
) (api.PushserverInternalAPI, error) {
if httpClient == nil {
return nil, errors.New("NewPushserverClient: httpClient is <nil>")
}
return &httpPushserverInternalAPI{
roomserverURL: pushserverURL,
httpClient: httpClient,
}, nil
}
// SetRoomAlias implements RoomserverAliasAPI
func (h *httpPushserverInternalAPI) QueryExample(
ctx context.Context,
request *api.QueryExampleRequest,
response *api.QueryExampleResponse,
) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryExample")
defer span.Finish()
apiURL := h.roomserverURL + PushserverQueryExamplePath
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
}

View file

@ -0,0 +1,29 @@
package inthttp
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/matrix-org/dendrite/internal/httputil"
"github.com/matrix-org/dendrite/pushserver/api"
"github.com/matrix-org/util"
)
// AddRoutes adds the RoomserverInternalAPI handlers to the http.ServeMux.
// nolint: gocyclo
func AddRoutes(r api.PushserverInternalAPI, internalAPIMux *mux.Router) {
internalAPIMux.Handle(PushserverQueryExamplePath,
httputil.MakeInternalAPI("queryExample", func(req *http.Request) util.JSONResponse {
var request api.QueryExampleRequest
var response api.QueryExampleResponse
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
return util.MessageResponse(http.StatusBadRequest, err.Error())
}
if err := r.QueryExample(req.Context(), &request, &response); err != nil {
return util.ErrorResponse(err)
}
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
}),
)
}