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-03-22 17:39:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-29 13:09:27 +00:00
|
|
|
"flag"
|
2017-03-22 17:39:08 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2017-08-03 14:10:39 +00:00
|
|
|
"github.com/gorilla/mux"
|
2017-07-26 13:53:11 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
2017-05-23 16:43:05 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
2017-04-20 12:40:56 +00:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-06-19 14:21:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-04-20 16:22:44 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/consumers"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/routing"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/sync"
|
2017-05-10 09:42:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2017-03-22 17:39:08 +00:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
var configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.")
|
2017-03-29 13:09:27 +00:00
|
|
|
|
2017-03-22 17:39:08 +00:00
|
|
|
func main() {
|
2017-04-20 14:26:34 +00:00
|
|
|
common.SetupLogging(os.Getenv("LOG_DIR"))
|
|
|
|
|
2017-03-29 13:09:27 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *configPath == "" {
|
|
|
|
log.Fatal("--config must be supplied")
|
|
|
|
}
|
2017-06-19 14:21:04 +00:00
|
|
|
cfg, err := config.Load(*configPath)
|
2017-03-29 13:09:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid config file: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Info("config: ", cfg)
|
2017-03-22 17:39:08 +00:00
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
db, err := storage.NewSyncServerDatabase(string(cfg.Database.SyncAPI))
|
2017-03-29 13:05:43 +00:00
|
|
|
if err != nil {
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Panicf("startup: failed to create sync server database with data source %s : %s", cfg.Database.SyncAPI, err)
|
2017-03-29 13:05:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
deviceDB, err := devices.NewDatabase(string(cfg.Database.Device), cfg.Matrix.ServerName)
|
2017-05-23 16:43:05 +00:00
|
|
|
if err != nil {
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Panicf("startup: failed to create device database with data source %s : %s", cfg.Database.Device, err)
|
2017-05-23 16:43:05 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 13:53:11 +00:00
|
|
|
adb, err := accounts.NewDatabase(string(cfg.Database.Account), cfg.Matrix.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("startup: failed to create account database with data source %s : %s", cfg.Database.Account, err)
|
|
|
|
}
|
|
|
|
|
2017-05-10 09:42:00 +00:00
|
|
|
pos, err := db.SyncStreamPosition()
|
2017-04-10 14:12:18 +00:00
|
|
|
if err != nil {
|
2017-05-10 09:42:00 +00:00
|
|
|
log.Panicf("startup: failed to get latest sync stream position : %s", err)
|
2017-04-10 14:12:18 +00:00
|
|
|
}
|
2017-04-07 13:32:42 +00:00
|
|
|
|
2017-05-10 09:42:00 +00:00
|
|
|
n := sync.NewNotifier(types.StreamPosition(pos))
|
2017-06-19 14:21:04 +00:00
|
|
|
if err = n.Load(db); err != nil {
|
2017-05-17 14:38:24 +00:00
|
|
|
log.Panicf("startup: failed to set up notifier: %s", err)
|
|
|
|
}
|
2017-08-02 15:21:35 +00:00
|
|
|
roomConsumer, err := consumers.NewOutputRoomEvent(cfg, n, db)
|
2017-03-29 13:05:43 +00:00
|
|
|
if err != nil {
|
2017-05-17 16:29:26 +00:00
|
|
|
log.Panicf("startup: failed to create room server consumer: %s", err)
|
2017-03-29 13:05:43 +00:00
|
|
|
}
|
2017-08-02 15:21:35 +00:00
|
|
|
if err = roomConsumer.Start(); err != nil {
|
|
|
|
log.Panicf("startup: failed to start room server consumer: %s", err)
|
|
|
|
}
|
|
|
|
clientConsumer, err := consumers.NewOutputClientData(cfg, n, db)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("startup: failed to create client API server consumer: %s", err)
|
|
|
|
}
|
|
|
|
if err = clientConsumer.Start(); err != nil {
|
|
|
|
log.Panicf("startup: failed to start client API server consumer: %s", err)
|
2017-03-29 13:05:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Info("Starting sync server on ", cfg.Listen.SyncAPI)
|
2017-08-03 14:10:39 +00:00
|
|
|
|
|
|
|
api := mux.NewRouter()
|
|
|
|
routing.Setup(api, sync.NewRequestPool(db, n, adb), deviceDB)
|
|
|
|
common.SetupHTTPAPI(http.DefaultServeMux, api)
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Fatal(http.ListenAndServe(string(cfg.Listen.SyncAPI), nil))
|
2017-03-22 17:39:08 +00:00
|
|
|
}
|