2017-04-20 22:40:52 +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.
|
|
|
|
|
2017-02-20 15:41:29 +00:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2017-04-20 16:11:53 +00:00
|
|
|
"encoding/json"
|
2017-02-20 15:41:29 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-03-10 11:32:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/config"
|
2017-03-15 13:36:26 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-04-20 16:11:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/readers"
|
2017-02-20 15:41:29 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/writers"
|
2017-03-15 11:22:40 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-02-20 15:41:29 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const pathPrefixR0 = "/_matrix/client/r0"
|
|
|
|
|
|
|
|
// Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client
|
|
|
|
// to clients which need to make outbound HTTP requests.
|
2017-03-15 13:36:26 +00:00
|
|
|
func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI, producer *producers.RoomserverProducer, queryAPI api.RoomserverQueryAPI) {
|
2017-02-20 15:41:29 +00:00
|
|
|
apiMux := mux.NewRouter()
|
|
|
|
r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
|
2017-05-18 12:47:23 +00:00
|
|
|
r0mux.Handle("/createRoom",
|
|
|
|
makeAPI("createRoom", func(req *http.Request) util.JSONResponse {
|
|
|
|
return writers.CreateRoom(req, cfg, producer)
|
|
|
|
}),
|
|
|
|
)
|
2017-03-15 11:22:40 +00:00
|
|
|
r0mux.Handle("/rooms/{roomID}/send/{eventType}/{txnID}",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("send_message", func(req *http.Request) util.JSONResponse {
|
2017-02-20 15:41:29 +00:00
|
|
|
vars := mux.Vars(req)
|
2017-03-17 11:21:52 +00:00
|
|
|
return writers.SendEvent(req, vars["roomID"], vars["eventType"], vars["txnID"], nil, cfg, queryAPI, producer)
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-03-17 11:21:52 +00:00
|
|
|
)
|
|
|
|
r0mux.Handle("/rooms/{roomID}/state/{eventType}",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("send_message", func(req *http.Request) util.JSONResponse {
|
2017-03-17 11:21:52 +00:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
emptyString := ""
|
|
|
|
return writers.SendEvent(req, vars["roomID"], vars["eventType"], vars["txnID"], &emptyString, cfg, queryAPI, producer)
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-03-17 11:21:52 +00:00
|
|
|
)
|
|
|
|
r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("send_message", func(req *http.Request) util.JSONResponse {
|
2017-03-17 11:21:52 +00:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
stateKey := vars["stateKey"]
|
|
|
|
return writers.SendEvent(req, vars["roomID"], vars["eventType"], vars["txnID"], &stateKey, cfg, queryAPI, producer)
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-02-20 15:41:29 +00:00
|
|
|
)
|
|
|
|
|
2017-04-20 16:11:53 +00:00
|
|
|
// Stub endpoints required by Riot
|
|
|
|
|
|
|
|
r0mux.Handle("/login",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("login", func(req *http.Request) util.JSONResponse {
|
2017-04-20 16:11:53 +00:00
|
|
|
return readers.Login(req, cfg)
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-04-20 16:11:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
r0mux.Handle("/pushrules/",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("push_rules", func(req *http.Request) util.JSONResponse {
|
2017-04-20 16:11:53 +00:00
|
|
|
// TODO: Implement push rules API
|
|
|
|
res := json.RawMessage(`{
|
|
|
|
"global": {
|
|
|
|
"content": [],
|
|
|
|
"override": [],
|
|
|
|
"room": [],
|
|
|
|
"sender": [],
|
|
|
|
"underride": []
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: &res,
|
|
|
|
}
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-04-20 16:11:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
r0mux.Handle("/user/{userID}/filter",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("make_filter", func(req *http.Request) util.JSONResponse {
|
2017-04-20 16:11:53 +00:00
|
|
|
// TODO: Persist filter and return filter ID
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-04-20 16:11:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
r0mux.Handle("/user/{userID}/filter/{filterID}",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("filter", func(req *http.Request) util.JSONResponse {
|
2017-04-20 16:11:53 +00:00
|
|
|
// TODO: Retrieve filter based on ID
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-04-20 16:11:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Riot user settings
|
|
|
|
|
|
|
|
r0mux.Handle("/profile/{userID}",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("profile", func(req *http.Request) util.JSONResponse {
|
2017-04-20 16:11:53 +00:00
|
|
|
// TODO: Get profile data for user ID
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-04-20 16:11:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
r0mux.Handle("/account/3pid",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("account_3pid", func(req *http.Request) util.JSONResponse {
|
2017-04-20 16:11:53 +00:00
|
|
|
// TODO: Get 3pid data for user ID
|
|
|
|
res := json.RawMessage(`{"threepids":[]}`)
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: &res,
|
|
|
|
}
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-04-20 16:11:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Riot logs get flooded unless this is handled
|
|
|
|
r0mux.Handle("/presence/{userID}/status",
|
2017-05-18 12:47:23 +00:00
|
|
|
makeAPI("presence", func(req *http.Request) util.JSONResponse {
|
2017-04-20 16:11:53 +00:00
|
|
|
// TODO: Set presence (probably the responsibility of a presence server not clientapi)
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
2017-05-18 12:47:23 +00:00
|
|
|
}),
|
2017-04-20 16:11:53 +00:00
|
|
|
)
|
|
|
|
|
2017-02-20 15:41:29 +00:00
|
|
|
servMux.Handle("/metrics", prometheus.Handler())
|
|
|
|
servMux.Handle("/api/", http.StripPrefix("/api", apiMux))
|
|
|
|
}
|
|
|
|
|
2017-05-18 12:47:23 +00:00
|
|
|
// make a util.JSONRequestHandler function into an http.Handler.
|
|
|
|
func makeAPI(metricsName string, f func(*http.Request) util.JSONResponse) http.Handler {
|
|
|
|
h := util.NewJSONRequestHandler(f)
|
2017-02-20 15:41:29 +00:00
|
|
|
return prometheus.InstrumentHandler(metricsName, util.MakeJSONAPI(h))
|
|
|
|
}
|