2017-05-19 15:06:41 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2017-08-03 14:10:39 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-05-19 15:06:41 +00:00
|
|
|
"github.com/gorilla/mux"
|
2017-06-07 13:32:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-06-19 14:21:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-05-19 15:06:41 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/readers"
|
2017-06-07 13:32:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/writers"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-19 15:06:41 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-07 13:32:53 +00:00
|
|
|
pathPrefixV2Keys = "/_matrix/key/v2"
|
|
|
|
pathPrefixV1Federation = "/_matrix/federation/v1"
|
2017-05-19 15:06:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Setup registers HTTP handlers with the given ServeMux.
|
2017-06-07 13:32:53 +00:00
|
|
|
func Setup(
|
2017-08-03 14:10:39 +00:00
|
|
|
apiMux *mux.Router,
|
2017-06-19 14:21:04 +00:00
|
|
|
cfg config.Dendrite,
|
2017-06-07 13:32:53 +00:00
|
|
|
query api.RoomserverQueryAPI,
|
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
keys gomatrixserverlib.KeyRing,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
) {
|
2017-05-19 15:06:41 +00:00
|
|
|
v2keysmux := apiMux.PathPrefix(pathPrefixV2Keys).Subrouter()
|
2017-06-07 13:32:53 +00:00
|
|
|
v1fedmux := apiMux.PathPrefix(pathPrefixV1Federation).Subrouter()
|
2017-05-19 15:06:41 +00:00
|
|
|
|
2017-05-25 15:08:28 +00:00
|
|
|
localKeys := makeAPI("localkeys", func(req *http.Request) util.JSONResponse {
|
|
|
|
return readers.LocalKeys(req, cfg)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Ignore the {keyID} argument as we only have a single server key so we always
|
|
|
|
// return that key.
|
|
|
|
// Even if we had more than one server key, we would probably still ignore the
|
|
|
|
// {keyID} argument and always return a response containing all of the keys.
|
|
|
|
v2keysmux.Handle("/server/{keyID}", localKeys)
|
|
|
|
v2keysmux.Handle("/server/", localKeys)
|
2017-05-19 15:06:41 +00:00
|
|
|
|
2017-08-23 14:13:47 +00:00
|
|
|
v1fedmux.Handle("/send/{txnID}/", makeAPI("federation_send",
|
2017-06-07 13:32:53 +00:00
|
|
|
func(req *http.Request) util.JSONResponse {
|
|
|
|
vars := mux.Vars(req)
|
|
|
|
return writers.Send(
|
|
|
|
req, gomatrixserverlib.TransactionID(vars["txnID"]),
|
|
|
|
time.Now(),
|
|
|
|
cfg, query, producer, keys, federation,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
))
|
2017-08-23 14:13:47 +00:00
|
|
|
|
|
|
|
v1fedmux.Handle("/invite/{roomID}/{eventID}", makeAPI("federation_invite",
|
|
|
|
func(req *http.Request) util.JSONResponse {
|
|
|
|
vars := mux.Vars(req)
|
|
|
|
return writers.Invite(
|
|
|
|
req, vars["roomID"], vars["eventID"],
|
|
|
|
time.Now(),
|
|
|
|
cfg, producer, keys,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
))
|
2017-05-19 15:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeAPI(metricsName string, f func(*http.Request) util.JSONResponse) http.Handler {
|
|
|
|
h := util.NewJSONRequestHandler(f)
|
|
|
|
return prometheus.InstrumentHandler(metricsName, util.MakeJSONAPI(h))
|
|
|
|
}
|