2017-08-04 07:12:36 -05: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 main
|
|
|
|
|
|
|
|
import (
|
2017-09-18 10:52:22 -05:00
|
|
|
"context"
|
2017-11-16 11:35:28 -06:00
|
|
|
"database/sql"
|
2017-08-04 07:12:36 -05:00
|
|
|
"flag"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
|
|
|
"github.com/matrix-org/dendrite/common"
|
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
|
|
|
"github.com/matrix-org/dendrite/common/keydb"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-08-16 07:36:41 -05:00
|
|
|
"github.com/matrix-org/naffka"
|
2017-08-04 07:12:36 -05:00
|
|
|
|
|
|
|
mediaapi_routing "github.com/matrix-org/dendrite/mediaapi/routing"
|
|
|
|
mediaapi_storage "github.com/matrix-org/dendrite/mediaapi/storage"
|
|
|
|
|
|
|
|
roomserver_alias "github.com/matrix-org/dendrite/roomserver/alias"
|
|
|
|
roomserver_input "github.com/matrix-org/dendrite/roomserver/input"
|
|
|
|
roomserver_query "github.com/matrix-org/dendrite/roomserver/query"
|
|
|
|
roomserver_storage "github.com/matrix-org/dendrite/roomserver/storage"
|
|
|
|
|
|
|
|
clientapi_consumers "github.com/matrix-org/dendrite/clientapi/consumers"
|
|
|
|
clientapi_routing "github.com/matrix-org/dendrite/clientapi/routing"
|
|
|
|
|
|
|
|
syncapi_consumers "github.com/matrix-org/dendrite/syncapi/consumers"
|
|
|
|
syncapi_routing "github.com/matrix-org/dendrite/syncapi/routing"
|
|
|
|
syncapi_storage "github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
syncapi_sync "github.com/matrix-org/dendrite/syncapi/sync"
|
|
|
|
syncapi_types "github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
|
|
|
|
federationapi_routing "github.com/matrix-org/dendrite/federationapi/routing"
|
|
|
|
|
|
|
|
federationsender_consumers "github.com/matrix-org/dendrite/federationsender/consumers"
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/queue"
|
|
|
|
federationsender_storage "github.com/matrix-org/dendrite/federationsender/storage"
|
|
|
|
|
2017-08-22 05:12:51 -05:00
|
|
|
publicroomsapi_consumers "github.com/matrix-org/dendrite/publicroomsapi/consumers"
|
|
|
|
publicroomsapi_routing "github.com/matrix-org/dendrite/publicroomsapi/routing"
|
|
|
|
publicroomsapi_storage "github.com/matrix-org/dendrite/publicroomsapi/storage"
|
|
|
|
|
2017-10-13 08:42:57 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-04 07:12:36 -05:00
|
|
|
sarama "gopkg.in/Shopify/sarama.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
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.")
|
|
|
|
httpBindAddr = flag.String("http-bind-address", ":8008", "The HTTP listening port for the server")
|
|
|
|
httpsBindAddr = flag.String("https-bind-address", ":8448", "The HTTPS listening port for the server")
|
2017-08-07 07:39:53 -05:00
|
|
|
certFile = flag.String("tls-cert", "", "The PEM formatted X509 certificate to use for TLS")
|
|
|
|
keyFile = flag.String("tls-key", "", "The PEM private key to use for TLS")
|
2017-08-04 07:12:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
common.SetupLogging(logDir)
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *configPath == "" {
|
|
|
|
log.Fatal("--config must be supplied")
|
|
|
|
}
|
2017-08-16 07:36:41 -05:00
|
|
|
cfg, err := config.LoadMonolithic(*configPath)
|
2017-08-04 07:12:36 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid config file: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-28 11:00:23 -05:00
|
|
|
closer, err := cfg.SetupTracing("DendriteMonolith")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatalf("Failed to start tracer")
|
|
|
|
}
|
|
|
|
defer closer.Close() // nolint: errcheck
|
|
|
|
|
2017-08-04 07:12:36 -05:00
|
|
|
m := newMonolith(cfg)
|
|
|
|
m.setupDatabases()
|
|
|
|
m.setupFederation()
|
2017-08-16 07:36:41 -05:00
|
|
|
m.setupKafka()
|
2017-08-04 07:12:36 -05:00
|
|
|
m.setupRoomServer()
|
|
|
|
m.setupProducers()
|
|
|
|
m.setupNotifiers()
|
|
|
|
m.setupConsumers()
|
|
|
|
m.setupAPIs()
|
|
|
|
|
|
|
|
// Expose the matrix APIs directly rather than putting them under a /api path.
|
2017-08-07 07:39:53 -05:00
|
|
|
go func() {
|
|
|
|
log.Info("Listening on ", *httpBindAddr)
|
2017-12-06 03:36:50 -06:00
|
|
|
log.Fatal(http.ListenAndServe(*httpBindAddr, common.WrapHandlerInCORS(m.api)))
|
2017-08-07 07:39:53 -05:00
|
|
|
}()
|
|
|
|
// Handle HTTPS if certificate and key are provided
|
|
|
|
go func() {
|
|
|
|
if *certFile != "" && *keyFile != "" {
|
|
|
|
log.Info("Listening on ", *httpsBindAddr)
|
|
|
|
log.Fatal(http.ListenAndServeTLS(*httpsBindAddr, *certFile, *keyFile, m.api))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// We want to block forever to let the HTTP and HTTPS handler serve the APIs
|
|
|
|
select {}
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// A monolith contains all the dendrite components.
|
|
|
|
// Some of the setup functions depend on previous setup functions, so they must
|
|
|
|
// be called in the same order as they are defined in the file.
|
|
|
|
type monolith struct {
|
|
|
|
cfg *config.Dendrite
|
|
|
|
api *mux.Router
|
|
|
|
|
|
|
|
roomServerDB *roomserver_storage.Database
|
|
|
|
accountDB *accounts.Database
|
|
|
|
deviceDB *devices.Database
|
|
|
|
keyDB *keydb.Database
|
|
|
|
mediaAPIDB *mediaapi_storage.Database
|
|
|
|
syncAPIDB *syncapi_storage.SyncServerDatabase
|
|
|
|
federationSenderDB *federationsender_storage.Database
|
2017-08-22 05:12:51 -05:00
|
|
|
publicRoomsAPIDB *publicroomsapi_storage.PublicRoomsServerDatabase
|
2017-08-04 07:12:36 -05:00
|
|
|
|
|
|
|
federation *gomatrixserverlib.FederationClient
|
|
|
|
keyRing gomatrixserverlib.KeyRing
|
|
|
|
|
|
|
|
inputAPI *roomserver_input.RoomserverInputAPI
|
|
|
|
queryAPI *roomserver_query.RoomserverQueryAPI
|
|
|
|
aliasAPI *roomserver_alias.RoomserverAliasAPI
|
|
|
|
|
2017-08-16 10:55:34 -05:00
|
|
|
naffka *naffka.Naffka
|
2017-08-16 07:36:41 -05:00
|
|
|
kafkaProducer sarama.SyncProducer
|
|
|
|
|
2017-08-04 07:12:36 -05:00
|
|
|
roomServerProducer *producers.RoomserverProducer
|
|
|
|
userUpdateProducer *producers.UserUpdateProducer
|
|
|
|
syncProducer *producers.SyncAPIProducer
|
|
|
|
|
|
|
|
syncAPINotifier *syncapi_sync.Notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMonolith(cfg *config.Dendrite) *monolith {
|
|
|
|
return &monolith{cfg: cfg, api: mux.NewRouter()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monolith) setupDatabases() {
|
|
|
|
var err error
|
|
|
|
m.roomServerDB, err = roomserver_storage.Open(string(m.cfg.Database.RoomServer))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
m.accountDB, err = accounts.NewDatabase(string(m.cfg.Database.Account), m.cfg.Matrix.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Failed to setup account database(%q): %s", m.cfg.Database.Account, err.Error())
|
|
|
|
}
|
|
|
|
m.deviceDB, err = devices.NewDatabase(string(m.cfg.Database.Device), m.cfg.Matrix.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Failed to setup device database(%q): %s", m.cfg.Database.Device, err.Error())
|
|
|
|
}
|
|
|
|
m.keyDB, err = keydb.NewDatabase(string(m.cfg.Database.ServerKey))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Failed to setup key database(%q): %s", m.cfg.Database.ServerKey, err.Error())
|
|
|
|
}
|
|
|
|
m.mediaAPIDB, err = mediaapi_storage.Open(string(m.cfg.Database.MediaAPI))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Failed to setup sync api database(%q): %s", m.cfg.Database.MediaAPI, err.Error())
|
|
|
|
}
|
|
|
|
m.syncAPIDB, err = syncapi_storage.NewSyncServerDatabase(string(m.cfg.Database.SyncAPI))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Failed to setup sync api database(%q): %s", m.cfg.Database.SyncAPI, err.Error())
|
|
|
|
}
|
|
|
|
m.federationSenderDB, err = federationsender_storage.NewDatabase(string(m.cfg.Database.FederationSender))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("startup: failed to create federation sender database with data source %s : %s", m.cfg.Database.FederationSender, err)
|
|
|
|
}
|
2017-08-22 05:12:51 -05:00
|
|
|
m.publicRoomsAPIDB, err = publicroomsapi_storage.NewPublicRoomsServerDatabase(string(m.cfg.Database.PublicRoomsAPI))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("startup: failed to setup public rooms api database with data source %s : %s", m.cfg.Database.PublicRoomsAPI, err)
|
|
|
|
}
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monolith) setupFederation() {
|
|
|
|
m.federation = gomatrixserverlib.NewFederationClient(
|
|
|
|
m.cfg.Matrix.ServerName, m.cfg.Matrix.KeyID, m.cfg.Matrix.PrivateKey,
|
|
|
|
)
|
|
|
|
|
2017-11-13 12:39:09 -06:00
|
|
|
m.keyRing = keydb.CreateKeyRing(m.federation.Client, m.keyDB)
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|
|
|
|
|
2017-08-16 07:36:41 -05:00
|
|
|
func (m *monolith) setupKafka() {
|
|
|
|
if m.cfg.Kafka.UseNaffka {
|
2017-11-16 11:35:28 -06:00
|
|
|
db, err := sql.Open("postgres", string(m.cfg.Database.Naffka))
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.ErrorKey: err,
|
|
|
|
}).Panic("Failed to open naffka database")
|
|
|
|
}
|
|
|
|
|
|
|
|
naffkaDB, err := naffka.NewPostgresqlDatabase(db)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.ErrorKey: err,
|
|
|
|
}).Panic("Failed to setup naffka database")
|
|
|
|
}
|
|
|
|
|
|
|
|
naff, err := naffka.New(naffkaDB)
|
2017-08-16 07:36:41 -05:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.ErrorKey: err,
|
|
|
|
}).Panic("Failed to setup naffka")
|
|
|
|
}
|
2017-08-16 10:55:34 -05:00
|
|
|
m.naffka = naff
|
2017-08-16 07:36:41 -05:00
|
|
|
m.kafkaProducer = naff
|
|
|
|
} else {
|
2017-09-05 11:40:46 -05:00
|
|
|
var err error
|
2017-08-16 07:36:41 -05:00
|
|
|
m.kafkaProducer, err = sarama.NewSyncProducer(m.cfg.Kafka.Addresses, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.ErrorKey: err,
|
|
|
|
"addresses": m.cfg.Kafka.Addresses,
|
|
|
|
}).Panic("Failed to setup kafka producers")
|
|
|
|
}
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|
2017-08-16 07:36:41 -05:00
|
|
|
}
|
2017-08-04 07:12:36 -05:00
|
|
|
|
2017-08-16 10:55:34 -05:00
|
|
|
func (m *monolith) kafkaConsumer() sarama.Consumer {
|
|
|
|
if m.cfg.Kafka.UseNaffka {
|
|
|
|
return m.naffka
|
|
|
|
}
|
|
|
|
consumer, err := sarama.NewConsumer(m.cfg.Kafka.Addresses, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.ErrorKey: err,
|
|
|
|
"addresses": m.cfg.Kafka.Addresses,
|
|
|
|
}).Panic("Failed to setup kafka consumers")
|
|
|
|
}
|
|
|
|
return consumer
|
|
|
|
}
|
|
|
|
|
2017-08-16 07:36:41 -05:00
|
|
|
func (m *monolith) setupRoomServer() {
|
2017-08-04 07:12:36 -05:00
|
|
|
m.inputAPI = &roomserver_input.RoomserverInputAPI{
|
|
|
|
DB: m.roomServerDB,
|
2017-08-16 07:36:41 -05:00
|
|
|
Producer: m.kafkaProducer,
|
2017-08-04 07:12:36 -05:00
|
|
|
OutputRoomEventTopic: string(m.cfg.Kafka.Topics.OutputRoomEvent),
|
|
|
|
}
|
|
|
|
|
|
|
|
m.queryAPI = &roomserver_query.RoomserverQueryAPI{
|
|
|
|
DB: m.roomServerDB,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.aliasAPI = &roomserver_alias.RoomserverAliasAPI{
|
|
|
|
DB: m.roomServerDB,
|
|
|
|
Cfg: m.cfg,
|
|
|
|
InputAPI: m.inputAPI,
|
|
|
|
QueryAPI: m.queryAPI,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monolith) setupProducers() {
|
|
|
|
m.roomServerProducer = producers.NewRoomserverProducer(m.inputAPI)
|
2017-08-16 07:36:41 -05:00
|
|
|
m.userUpdateProducer = &producers.UserUpdateProducer{
|
|
|
|
Producer: m.kafkaProducer,
|
|
|
|
Topic: string(m.cfg.Kafka.Topics.UserUpdates),
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|
2017-08-16 07:36:41 -05:00
|
|
|
m.syncProducer = &producers.SyncAPIProducer{
|
|
|
|
Producer: m.kafkaProducer,
|
|
|
|
Topic: string(m.cfg.Kafka.Topics.OutputClientData),
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monolith) setupNotifiers() {
|
2017-09-18 10:52:22 -05:00
|
|
|
pos, err := m.syncAPIDB.SyncStreamPosition(context.Background())
|
2017-08-04 07:12:36 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panicf("startup: failed to get latest sync stream position : %s", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 08:54:08 -05:00
|
|
|
m.syncAPINotifier = syncapi_sync.NewNotifier(syncapi_types.StreamPosition(pos))
|
2017-09-18 10:52:22 -05:00
|
|
|
if err = m.syncAPINotifier.Load(context.Background(), m.syncAPIDB); err != nil {
|
2017-08-04 07:12:36 -05:00
|
|
|
log.Panicf("startup: failed to set up notifier: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monolith) setupConsumers() {
|
2017-08-16 07:36:41 -05:00
|
|
|
var err error
|
|
|
|
|
2017-10-11 12:13:43 -05:00
|
|
|
clientAPIConsumer := clientapi_consumers.NewOutputRoomEventConsumer(
|
2017-08-16 10:55:34 -05:00
|
|
|
m.cfg, m.kafkaConsumer(), m.accountDB, m.queryAPI,
|
2017-08-16 07:36:41 -05:00
|
|
|
)
|
2017-08-04 07:12:36 -05:00
|
|
|
if err = clientAPIConsumer.Start(); err != nil {
|
2017-08-31 06:27:53 -05:00
|
|
|
log.Panicf("startup: failed to start room server consumer: %s", err)
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:13:43 -05:00
|
|
|
syncAPIRoomConsumer := syncapi_consumers.NewOutputRoomEventConsumer(
|
2017-08-16 10:55:34 -05:00
|
|
|
m.cfg, m.kafkaConsumer(), m.syncAPINotifier, m.syncAPIDB, m.queryAPI,
|
2017-08-04 07:12:36 -05:00
|
|
|
)
|
|
|
|
if err = syncAPIRoomConsumer.Start(); err != nil {
|
|
|
|
log.Panicf("startup: failed to start room server consumer: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:13:43 -05:00
|
|
|
syncAPIClientConsumer := syncapi_consumers.NewOutputClientDataConsumer(
|
2017-08-16 10:55:34 -05:00
|
|
|
m.cfg, m.kafkaConsumer(), m.syncAPINotifier, m.syncAPIDB,
|
2017-08-04 07:12:36 -05:00
|
|
|
)
|
|
|
|
if err = syncAPIClientConsumer.Start(); err != nil {
|
|
|
|
log.Panicf("startup: failed to start client API server consumer: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:13:43 -05:00
|
|
|
publicRoomsAPIConsumer := publicroomsapi_consumers.NewOutputRoomEventConsumer(
|
2017-08-22 05:12:51 -05:00
|
|
|
m.cfg, m.kafkaConsumer(), m.publicRoomsAPIDB, m.queryAPI,
|
|
|
|
)
|
|
|
|
if err = publicRoomsAPIConsumer.Start(); err != nil {
|
|
|
|
log.Panicf("startup: failed to start room server consumer: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 07:12:36 -05:00
|
|
|
federationSenderQueues := queue.NewOutgoingQueues(m.cfg.Matrix.ServerName, m.federation)
|
|
|
|
|
2017-10-11 12:13:43 -05:00
|
|
|
federationSenderRoomConsumer := federationsender_consumers.NewOutputRoomEventConsumer(
|
2017-08-16 10:55:34 -05:00
|
|
|
m.cfg, m.kafkaConsumer(), federationSenderQueues, m.federationSenderDB, m.queryAPI,
|
2017-08-04 07:12:36 -05:00
|
|
|
)
|
|
|
|
if err = federationSenderRoomConsumer.Start(); err != nil {
|
|
|
|
log.WithError(err).Panicf("startup: failed to start room server consumer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monolith) setupAPIs() {
|
|
|
|
clientapi_routing.Setup(
|
2017-09-07 06:50:39 -05:00
|
|
|
m.api, *m.cfg, m.roomServerProducer,
|
2017-08-04 07:12:36 -05:00
|
|
|
m.queryAPI, m.aliasAPI, m.accountDB, m.deviceDB, m.federation, m.keyRing,
|
|
|
|
m.userUpdateProducer, m.syncProducer,
|
|
|
|
)
|
|
|
|
|
|
|
|
mediaapi_routing.Setup(
|
2017-09-28 08:50:40 -05:00
|
|
|
m.api, m.cfg, m.mediaAPIDB, m.deviceDB, &m.federation.Client,
|
2017-08-04 07:12:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
syncapi_routing.Setup(m.api, syncapi_sync.NewRequestPool(
|
|
|
|
m.syncAPIDB, m.syncAPINotifier, m.accountDB,
|
2017-11-15 09:42:39 -06:00
|
|
|
), m.syncAPIDB, m.deviceDB)
|
2017-08-04 07:12:36 -05:00
|
|
|
|
|
|
|
federationapi_routing.Setup(
|
2017-11-20 08:33:49 -06:00
|
|
|
m.api, *m.cfg, m.queryAPI, m.aliasAPI, m.roomServerProducer, m.keyRing, m.federation,
|
2017-09-13 07:31:46 -05:00
|
|
|
m.accountDB,
|
2017-08-04 07:12:36 -05:00
|
|
|
)
|
2017-08-22 05:12:51 -05:00
|
|
|
|
|
|
|
publicroomsapi_routing.Setup(m.api, m.deviceDB, m.publicRoomsAPIDB)
|
2017-08-04 07:12:36 -05:00
|
|
|
}
|