2017-04-20 23:40:52 +01: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-03 16:05:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-19 15:21:04 +01:00
|
|
|
"flag"
|
2017-02-03 16:05:46 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-02-20 16:14:23 +00:00
|
|
|
|
2017-08-03 15:10:39 +01:00
|
|
|
"github.com/gorilla/mux"
|
2017-05-22 16:49:32 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
2017-05-23 17:43:05 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
2017-07-17 18:10:56 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/consumers"
|
2017-03-15 13:36:26 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-02-20 16:14:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/routing"
|
2017-04-21 00:43:36 +01:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-06-19 15:21:04 +01:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-06-09 18:07:34 +01:00
|
|
|
"github.com/matrix-org/dendrite/common/keydb"
|
2017-03-15 11:22:40 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-05-25 16:08:28 +01:00
|
|
|
|
2017-05-19 16:06:41 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-02-03 16:05:46 +00:00
|
|
|
|
2017-10-13 14:42:57 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-16 13:36:41 +01:00
|
|
|
sarama "gopkg.in/Shopify/sarama.v1"
|
2017-02-03 16:05:46 +00:00
|
|
|
)
|
|
|
|
|
2017-04-20 17:15:34 +01:00
|
|
|
var (
|
2017-06-19 15:21:04 +01:00
|
|
|
logDir = os.Getenv("LOG_DIR")
|
|
|
|
configPath = flag.String("config", "dendrite.yaml", "The path to the config file, For more information see the config file in this repository")
|
2017-04-20 17:15:34 +01:00
|
|
|
)
|
|
|
|
|
2017-02-03 16:05:46 +00:00
|
|
|
func main() {
|
2017-04-21 00:43:36 +01:00
|
|
|
common.SetupLogging(logDir)
|
2017-03-10 16:19:23 +00:00
|
|
|
|
2017-06-19 15:21:04 +01:00
|
|
|
flag.Parse()
|
2017-03-10 16:19:23 +00:00
|
|
|
|
2017-06-19 15:21:04 +01:00
|
|
|
cfg, err := config.Load(*configPath)
|
2017-05-19 16:06:41 +01:00
|
|
|
if err != nil {
|
2017-06-19 15:21:04 +01:00
|
|
|
log.Fatalf("Invalid config file: %s", err)
|
2017-05-19 16:06:41 +01:00
|
|
|
}
|
|
|
|
|
2017-09-28 17:00:23 +01:00
|
|
|
closer, err := cfg.SetupTracing("DendriteClientAPI")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatalf("Failed to start tracer")
|
|
|
|
}
|
|
|
|
defer closer.Close() // nolint: errcheck
|
|
|
|
|
2017-07-18 13:40:03 +01:00
|
|
|
queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomServerURL(), nil)
|
2017-07-28 11:31:43 +01:00
|
|
|
aliasAPI := api.NewRoomserverAliasAPIHTTP(cfg.RoomServerURL(), nil)
|
2017-08-04 13:12:36 +01:00
|
|
|
inputAPI := api.NewRoomserverInputAPIHTTP(cfg.RoomServerURL(), nil)
|
2017-07-18 13:40:03 +01:00
|
|
|
|
2017-08-04 13:12:36 +01:00
|
|
|
roomserverProducer := producers.NewRoomserverProducer(inputAPI)
|
2017-08-16 13:36:41 +01:00
|
|
|
|
|
|
|
kafkaProducer, err := sarama.NewSyncProducer(cfg.Kafka.Addresses, nil)
|
2017-03-10 16:19:23 +00:00
|
|
|
if err != nil {
|
2017-08-16 13:36:41 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.ErrorKey: err,
|
|
|
|
"addresses": cfg.Kafka.Addresses,
|
|
|
|
}).Panic("Failed to setup kafka producers")
|
2017-03-10 16:19:23 +00:00
|
|
|
}
|
2017-08-16 13:36:41 +01:00
|
|
|
|
|
|
|
userUpdateProducer := &producers.UserUpdateProducer{
|
|
|
|
Producer: kafkaProducer,
|
|
|
|
Topic: string(cfg.Kafka.Topics.UserUpdates),
|
|
|
|
}
|
|
|
|
|
|
|
|
syncProducer := &producers.SyncAPIProducer{
|
|
|
|
Producer: kafkaProducer,
|
|
|
|
Topic: string(cfg.Kafka.Topics.OutputClientData),
|
2017-08-02 16:21:35 +01:00
|
|
|
}
|
2017-03-15 13:36:26 +00:00
|
|
|
|
2017-06-19 15:21:04 +01:00
|
|
|
federation := gomatrixserverlib.NewFederationClient(
|
|
|
|
cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey,
|
|
|
|
)
|
2017-05-25 16:08:28 +01:00
|
|
|
|
2017-06-19 15:21:04 +01:00
|
|
|
accountDB, err := accounts.NewDatabase(string(cfg.Database.Account), cfg.Matrix.ServerName)
|
2017-05-22 15:55:39 +01:00
|
|
|
if err != nil {
|
2017-06-19 15:21:04 +01:00
|
|
|
log.Panicf("Failed to setup account database(%q): %s", cfg.Database.Account, err.Error())
|
2017-05-22 15:55:39 +01:00
|
|
|
}
|
2017-06-19 15:21:04 +01:00
|
|
|
deviceDB, err := devices.NewDatabase(string(cfg.Database.Device), cfg.Matrix.ServerName)
|
2017-05-23 17:43:05 +01:00
|
|
|
if err != nil {
|
2017-06-19 15:21:04 +01:00
|
|
|
log.Panicf("Failed to setup device database(%q): %s", cfg.Database.Device, err.Error())
|
2017-06-09 18:07:34 +01:00
|
|
|
}
|
2017-06-19 15:21:04 +01:00
|
|
|
keyDB, err := keydb.NewDatabase(string(cfg.Database.ServerKey))
|
2017-06-09 18:07:34 +01:00
|
|
|
if err != nil {
|
2017-06-19 15:21:04 +01:00
|
|
|
log.Panicf("Failed to setup key database(%q): %s", cfg.Database.ServerKey, err.Error())
|
2017-05-23 17:43:05 +01:00
|
|
|
}
|
2017-03-10 16:19:23 +00:00
|
|
|
|
2017-05-25 16:08:28 +01:00
|
|
|
keyRing := gomatrixserverlib.KeyRing{
|
|
|
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{
|
|
|
|
// TODO: Use perspective key fetchers for production.
|
2017-08-16 13:36:41 +01:00
|
|
|
&gomatrixserverlib.DirectKeyFetcher{Client: federation.Client},
|
2017-05-25 16:08:28 +01:00
|
|
|
},
|
2017-06-09 18:07:34 +01:00
|
|
|
KeyDatabase: keyDB,
|
2017-05-25 16:08:28 +01:00
|
|
|
}
|
|
|
|
|
2017-08-16 13:36:41 +01:00
|
|
|
kafkaConsumer, err := sarama.NewConsumer(cfg.Kafka.Addresses, nil)
|
2017-07-17 18:10:56 +01:00
|
|
|
if err != nil {
|
2017-08-16 13:36:41 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.ErrorKey: err,
|
|
|
|
"addresses": cfg.Kafka.Addresses,
|
|
|
|
}).Panic("Failed to setup kafka consumers")
|
2017-07-17 18:10:56 +01:00
|
|
|
}
|
2017-08-16 13:36:41 +01:00
|
|
|
|
2017-10-11 18:13:43 +01:00
|
|
|
consumer := consumers.NewOutputRoomEventConsumer(cfg, kafkaConsumer, accountDB, queryAPI)
|
2017-07-17 18:10:56 +01:00
|
|
|
if err = consumer.Start(); err != nil {
|
|
|
|
log.Panicf("startup: failed to start room server consumer")
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:21:04 +01:00
|
|
|
log.Info("Starting client API server on ", cfg.Listen.ClientAPI)
|
2017-08-03 15:10:39 +01:00
|
|
|
|
|
|
|
api := mux.NewRouter()
|
2017-05-25 16:08:28 +01:00
|
|
|
routing.Setup(
|
2017-09-07 12:50:39 +01:00
|
|
|
api, *cfg, roomserverProducer,
|
2017-07-28 11:31:43 +01:00
|
|
|
queryAPI, aliasAPI, accountDB, deviceDB, federation, keyRing,
|
2017-08-02 16:21:35 +01:00
|
|
|
userUpdateProducer, syncProducer,
|
2017-05-25 16:08:28 +01:00
|
|
|
)
|
2017-08-03 15:10:39 +01:00
|
|
|
common.SetupHTTPAPI(http.DefaultServeMux, api)
|
|
|
|
|
2017-06-19 15:21:04 +01:00
|
|
|
log.Fatal(http.ListenAndServe(string(cfg.Listen.ClientAPI), nil))
|
2017-02-03 16:05:46 +00:00
|
|
|
}
|