main.go 2.0 KB
Newer Older
1 2 3
package main

import (
4 5
	"flag"
	"io/ioutil"
6 7 8
	"net/http"
	"os"

9
	"github.com/matrix-org/dendrite/common"
10 11 12 13 14
	"github.com/matrix-org/dendrite/syncserver/config"
	"github.com/matrix-org/dendrite/syncserver/consumers"
	"github.com/matrix-org/dendrite/syncserver/routing"
	"github.com/matrix-org/dendrite/syncserver/storage"
	"github.com/matrix-org/dendrite/syncserver/sync"
15 16

	log "github.com/Sirupsen/logrus"
17
	yaml "gopkg.in/yaml.v2"
18 19
)

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
var configPath = flag.String("config", "sync-server-config.yaml", "The path to the config file. For more information, see the config file in this repository.")
var bindAddr = flag.String("listen", ":4200", "The port to listen on.")

func loadConfig(configPath string) (*config.Sync, error) {
	contents, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, err
	}
	var cfg config.Sync
	if err = yaml.Unmarshal(contents, &cfg); err != nil {
		return nil, err
	}
	// check required fields
	return &cfg, nil
}

36
func main() {
37 38 39 40 41 42 43 44 45 46 47 48
	flag.Parse()

	if *configPath == "" {
		log.Fatal("--config must be supplied")
	}
	cfg, err := loadConfig(*configPath)
	if err != nil {
		log.Fatalf("Invalid config file: %s", err)
	}

	if *bindAddr == "" {
		log.Fatal("--listen must be supplied")
49 50 51
	}
	logDir := os.Getenv("LOG_DIR")
	if logDir != "" {
52
		common.SetupLogging(logDir)
53 54
	}

55
	log.Info("sync server config: ", cfg)
56

57 58 59 60 61
	db, err := storage.NewSyncServerDatabase(cfg.DataSource)
	if err != nil {
		log.Panicf("startup: failed to create sync server database with data source %s : %s", cfg.DataSource, err)
	}

62 63 64 65
	rp, err := sync.NewRequestPool(db)
	if err != nil {
		log.Panicf("startup: Failed to create request pool : %s", err)
	}
K
Kegsay 已提交
66

67
	server, err := consumers.NewServer(cfg, rp, db)
68 69 70 71 72 73 74
	if err != nil {
		log.Panicf("startup: failed to create sync server: %s", err)
	}
	if err = server.Start(); err != nil {
		log.Panicf("startup: failed to start sync server")
	}

75
	log.Info("Starting sync server on ", *bindAddr)
K
Kegsay 已提交
76
	routing.SetupSyncServerListeners(http.DefaultServeMux, http.DefaultClient, *cfg, rp)
77
	log.Fatal(http.ListenAndServe(*bindAddr, nil))
78
}