main.go 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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 (
18
	"flag"
19 20 21
	"net/http"
	"os"

22
	"github.com/gorilla/mux"
23
	"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
24
	"github.com/matrix-org/dendrite/clientapi/producers"
25
	"github.com/matrix-org/dendrite/common"
26
	"github.com/matrix-org/dendrite/common/config"
27
	"github.com/matrix-org/dendrite/common/keydb"
28
	"github.com/matrix-org/dendrite/federationapi/routing"
29
	"github.com/matrix-org/dendrite/roomserver/api"
30 31
	"github.com/matrix-org/gomatrixserverlib"

32
	log "github.com/sirupsen/logrus"
33 34 35 36
)

var (
	logDir     = os.Getenv("LOG_DIR")
37
	configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.")
38 39 40 41
)

func main() {
	common.SetupLogging(logDir)
42

43 44
	flag.Parse()

45 46
	if *configPath == "" {
		log.Fatal("--config must be supplied")
47
	}
48
	cfg, err := config.Load(*configPath)
49
	if err != nil {
50
		log.Fatalf("Invalid config file: %s", err)
51 52
	}

53 54 55 56 57 58
	closer, err := cfg.SetupTracing("DendriteFederationAPI")
	if err != nil {
		log.WithError(err).Fatalf("Failed to start tracer")
	}
	defer closer.Close() // nolint: errcheck

59 60 61
	federation := gomatrixserverlib.NewFederationClient(
		cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey,
	)
62

63
	keyDB, err := keydb.NewDatabase(string(cfg.Database.ServerKey))
64
	if err != nil {
65
		log.Panicf("Failed to setup key database(%q): %s", cfg.Database.ServerKey, err.Error())
66 67
	}

68 69 70 71 72
	accountDB, err := accounts.NewDatabase(string(cfg.Database.Account), cfg.Matrix.ServerName)
	if err != nil {
		log.Panicf("Failed to setup account database(%q): %s", cfg.Database.Account, err.Error())
	}

73 74 75
	keyRing := gomatrixserverlib.KeyRing{
		KeyFetchers: []gomatrixserverlib.KeyFetcher{
			// TODO: Use perspective key fetchers for production.
E
Erik Johnston 已提交
76
			&gomatrixserverlib.DirectKeyFetcher{Client: federation.Client},
77
		},
78
		KeyDatabase: keyDB,
79 80
	}

81
	queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomServerURL(), nil)
82
	inputAPI := api.NewRoomserverInputAPIHTTP(cfg.RoomServerURL(), nil)
83

84
	roomserverProducer := producers.NewRoomserverProducer(inputAPI)
85

86
	if err != nil {
87
		log.Panicf("Failed to setup kafka producers(%s): %s", cfg.Kafka.Addresses, err)
88 89
	}

90 91
	log.Info("Starting federation API server on ", cfg.Listen.FederationAPI)

92
	api := mux.NewRouter()
93
	routing.Setup(api, *cfg, queryAPI, roomserverProducer, keyRing, federation, accountDB)
94 95
	common.SetupHTTPAPI(http.DefaultServeMux, api)

96
	log.Fatal(http.ListenAndServe(string(cfg.Listen.FederationAPI), nil))
97
}