2020-06-12 14:55:57 +01:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2018-01-02 10:26:56 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-06-12 14:55:57 +01:00
|
|
|
package setup
|
2018-01-02 10:26:56 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-02 21:02:24 +01:00
|
|
|
"fmt"
|
2018-01-02 10:26:56 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-02-13 17:27:33 +00:00
|
|
|
"net/url"
|
2020-04-03 11:40:50 +01:00
|
|
|
"time"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-06-12 14:55:57 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2018-01-02 10:26:56 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-08-13 12:16:37 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-08-24 13:49:20 +01:00
|
|
|
"github.com/matrix-org/naffka"
|
|
|
|
naffkaStorage "github.com/matrix-org/naffka/storage"
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-17 12:05:56 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/accounts"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-04-22 15:26:56 +01:00
|
|
|
"github.com/Shopify/sarama"
|
2018-01-02 10:26:56 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2018-07-17 15:36:04 +01:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
2020-06-04 15:43:07 +01:00
|
|
|
asinthttp "github.com/matrix-org/dendrite/appservice/inthttp"
|
2020-03-30 15:02:20 +01:00
|
|
|
eduServerAPI "github.com/matrix-org/dendrite/eduserver/api"
|
2020-06-04 15:43:07 +01:00
|
|
|
eduinthttp "github.com/matrix-org/dendrite/eduserver/inthttp"
|
2019-10-01 17:09:47 +01:00
|
|
|
federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api"
|
2020-06-04 15:43:07 +01:00
|
|
|
fsinthttp "github.com/matrix-org/dendrite/federationsender/inthttp"
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2020-07-13 16:02:35 +01:00
|
|
|
keyserverAPI "github.com/matrix-org/dendrite/keyserver/api"
|
|
|
|
keyinthttp "github.com/matrix-org/dendrite/keyserver/inthttp"
|
2018-07-17 15:36:04 +01:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-06-04 15:43:07 +01:00
|
|
|
rsinthttp "github.com/matrix-org/dendrite/roomserver/inthttp"
|
2020-05-27 10:19:24 +01:00
|
|
|
serverKeyAPI "github.com/matrix-org/dendrite/serverkeyapi/api"
|
2020-06-04 16:26:35 +01:00
|
|
|
skinthttp "github.com/matrix-org/dendrite/serverkeyapi/inthttp"
|
2020-06-16 14:10:55 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
userapiinthttp "github.com/matrix-org/dendrite/userapi/inthttp"
|
2018-01-02 10:26:56 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-11 18:21:39 +01:00
|
|
|
|
|
|
|
_ "net/http/pprof"
|
2018-01-02 10:26:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BaseDendrite is a base for creating new instances of dendrite. It parses
|
|
|
|
// command line flags and config, and exposes methods for creating various
|
|
|
|
// resources. All errors are handled by logging then exiting, so all methods
|
|
|
|
// should only be used during start up.
|
|
|
|
// Must be closed when shutting down.
|
|
|
|
type BaseDendrite struct {
|
2020-08-13 12:16:37 +01:00
|
|
|
componentName string
|
|
|
|
tracerCloser io.Closer
|
|
|
|
PublicClientAPIMux *mux.Router
|
|
|
|
PublicFederationAPIMux *mux.Router
|
|
|
|
PublicKeyAPIMux *mux.Router
|
|
|
|
PublicMediaAPIMux *mux.Router
|
|
|
|
InternalAPIMux *mux.Router
|
|
|
|
UseHTTPAPIs bool
|
2020-10-01 11:55:17 +01:00
|
|
|
apiHttpClient *http.Client
|
2020-08-13 12:16:37 +01:00
|
|
|
httpClient *http.Client
|
|
|
|
Cfg *config.Dendrite
|
|
|
|
Caches *caching.Caches
|
|
|
|
KafkaConsumer sarama.Consumer
|
|
|
|
KafkaProducer sarama.SyncProducer
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 17:42:34 +01:00
|
|
|
const HTTPServerTimeout = time.Minute * 5
|
|
|
|
const HTTPClientTimeout = time.Second * 30
|
|
|
|
|
2020-08-13 12:16:37 +01:00
|
|
|
const NoExternalListener = ""
|
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
// NewBaseDendrite creates a new instance to be used by a component.
|
|
|
|
// The componentName is used for logging purposes, and should be a friendly name
|
|
|
|
// of the compontent running, e.g. "SyncAPI"
|
2020-06-04 14:27:10 +01:00
|
|
|
func NewBaseDendrite(cfg *config.Dendrite, componentName string, useHTTPAPIs bool) *BaseDendrite {
|
2020-08-10 14:18:04 +01:00
|
|
|
configErrors := &config.ConfigErrors{}
|
|
|
|
cfg.Verify(configErrors, componentName == "Monolith") // TODO: better way?
|
|
|
|
if len(*configErrors) > 0 {
|
|
|
|
for _, err := range *configErrors {
|
|
|
|
logrus.Errorf("Configuration error: %s", err)
|
|
|
|
}
|
|
|
|
logrus.Fatalf("Failed to start due to configuration errors")
|
|
|
|
}
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
internal.SetupStdLogging()
|
|
|
|
internal.SetupHookLogging(cfg.Logging, componentName)
|
|
|
|
internal.SetupPprof()
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-09-02 16:18:08 +01:00
|
|
|
logrus.Infof("Dendrite version %s", internal.VersionString())
|
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
closer, err := cfg.SetupTracing("Dendrite" + componentName)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to start opentracing")
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:27:33 +00:00
|
|
|
var kafkaConsumer sarama.Consumer
|
|
|
|
var kafkaProducer sarama.SyncProducer
|
2020-08-10 14:18:04 +01:00
|
|
|
if cfg.Global.Kafka.UseNaffka {
|
2020-02-13 17:27:33 +00:00
|
|
|
kafkaConsumer, kafkaProducer = setupNaffka(cfg)
|
|
|
|
} else {
|
|
|
|
kafkaConsumer, kafkaProducer = setupKafka(cfg)
|
|
|
|
}
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-06-16 13:11:20 +01:00
|
|
|
cache, err := caching.NewInMemoryLRUCache(true)
|
2020-04-22 13:00:05 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Warnf("Failed to create cache")
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:00:35 +01:00
|
|
|
apiClient := http.Client{Timeout: time.Minute * 10}
|
2020-06-02 21:02:24 +01:00
|
|
|
client := http.Client{Timeout: HTTPClientTimeout}
|
2020-08-10 14:18:04 +01:00
|
|
|
if cfg.FederationSender.Proxy.Enabled {
|
2020-06-02 21:02:24 +01:00
|
|
|
client.Transport = &http.Transport{Proxy: http.ProxyURL(&url.URL{
|
2020-08-10 14:18:04 +01:00
|
|
|
Scheme: cfg.FederationSender.Proxy.Protocol,
|
|
|
|
Host: fmt.Sprintf("%s:%d", cfg.FederationSender.Proxy.Host, cfg.FederationSender.Proxy.Port),
|
2020-06-02 21:02:24 +01:00
|
|
|
})}
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:14:08 +01:00
|
|
|
// Ideally we would only use SkipClean on routes which we know can allow '/' but due to
|
|
|
|
// https://github.com/gorilla/mux/issues/460 we have to attach this at the top router.
|
|
|
|
// When used in conjunction with UseEncodedPath() we get the behaviour we want when parsing
|
|
|
|
// path parameters:
|
|
|
|
// /foo/bar%2Fbaz == [foo, bar%2Fbaz] (from UseEncodedPath)
|
|
|
|
// /foo/bar%2F%2Fbaz == [foo, bar%2F%2Fbaz] (from SkipClean)
|
|
|
|
// In particular, rooms v3 event IDs are not urlsafe and can include '/' and because they
|
|
|
|
// are randomly generated it results in flakey tests.
|
|
|
|
// We need to be careful with media APIs if they read from a filesystem to make sure they
|
|
|
|
// are not inadvertently reading paths without cleaning, else this could introduce a
|
|
|
|
// directory traversal attack e.g /../../../etc/passwd
|
2018-01-02 10:26:56 +00:00
|
|
|
return &BaseDendrite{
|
2020-08-13 12:16:37 +01:00
|
|
|
componentName: componentName,
|
|
|
|
UseHTTPAPIs: useHTTPAPIs,
|
|
|
|
tracerCloser: closer,
|
|
|
|
Cfg: cfg,
|
|
|
|
Caches: cache,
|
|
|
|
PublicClientAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicClientPathPrefix).Subrouter().UseEncodedPath(),
|
|
|
|
PublicFederationAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicFederationPathPrefix).Subrouter().UseEncodedPath(),
|
|
|
|
PublicKeyAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicKeyPathPrefix).Subrouter().UseEncodedPath(),
|
|
|
|
PublicMediaAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicMediaPathPrefix).Subrouter().UseEncodedPath(),
|
|
|
|
InternalAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.InternalPathPrefix).Subrouter().UseEncodedPath(),
|
2020-10-01 11:55:17 +01:00
|
|
|
apiHttpClient: &apiClient,
|
2020-08-13 12:16:37 +01:00
|
|
|
httpClient: &client,
|
|
|
|
KafkaConsumer: kafkaConsumer,
|
|
|
|
KafkaProducer: kafkaProducer,
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements io.Closer
|
|
|
|
func (b *BaseDendrite) Close() error {
|
|
|
|
return b.tracerCloser.Close()
|
|
|
|
}
|
|
|
|
|
2020-06-04 15:43:07 +01:00
|
|
|
// AppserviceHTTPClient returns the AppServiceQueryAPI for hitting the appservice component over HTTP.
|
|
|
|
func (b *BaseDendrite) AppserviceHTTPClient() appserviceAPI.AppServiceQueryAPI {
|
2020-10-01 11:55:17 +01:00
|
|
|
a, err := asinthttp.NewAppserviceClient(b.Cfg.AppServiceURL(), b.apiHttpClient)
|
2020-04-03 11:40:50 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("CreateHTTPAppServiceAPIs failed")
|
|
|
|
}
|
|
|
|
return a
|
2018-07-17 15:36:04 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 15:43:07 +01:00
|
|
|
// RoomserverHTTPClient returns RoomserverInternalAPI for hitting the roomserver over HTTP.
|
|
|
|
func (b *BaseDendrite) RoomserverHTTPClient() roomserverAPI.RoomserverInternalAPI {
|
2020-10-01 11:55:17 +01:00
|
|
|
rsAPI, err := rsinthttp.NewRoomserverClient(b.Cfg.RoomServerURL(), b.apiHttpClient, b.Caches)
|
2020-04-03 11:40:50 +01:00
|
|
|
if err != nil {
|
2020-10-01 11:55:17 +01:00
|
|
|
logrus.WithError(err).Panic("RoomserverHTTPClient failed", b.apiHttpClient)
|
2020-04-03 11:40:50 +01:00
|
|
|
}
|
2020-05-01 10:48:17 +01:00
|
|
|
return rsAPI
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 14:10:55 +01:00
|
|
|
// UserAPIClient returns UserInternalAPI for hitting the userapi over HTTP.
|
|
|
|
func (b *BaseDendrite) UserAPIClient() userapi.UserInternalAPI {
|
2020-10-01 11:55:17 +01:00
|
|
|
userAPI, err := userapiinthttp.NewUserAPIClient(b.Cfg.UserAPIURL(), b.apiHttpClient)
|
2020-06-16 14:10:55 +01:00
|
|
|
if err != nil {
|
2020-10-01 11:55:17 +01:00
|
|
|
logrus.WithError(err).Panic("UserAPIClient failed", b.apiHttpClient)
|
2020-06-16 14:10:55 +01:00
|
|
|
}
|
|
|
|
return userAPI
|
|
|
|
}
|
|
|
|
|
2020-06-04 15:43:07 +01:00
|
|
|
// EDUServerClient returns EDUServerInputAPI for hitting the EDU server over HTTP
|
|
|
|
func (b *BaseDendrite) EDUServerClient() eduServerAPI.EDUServerInputAPI {
|
2020-10-01 11:55:17 +01:00
|
|
|
e, err := eduinthttp.NewEDUServerClient(b.Cfg.EDUServerURL(), b.apiHttpClient)
|
2020-04-03 11:40:50 +01:00
|
|
|
if err != nil {
|
2020-10-01 11:55:17 +01:00
|
|
|
logrus.WithError(err).Panic("EDUServerClient failed", b.apiHttpClient)
|
2020-04-03 11:40:50 +01:00
|
|
|
}
|
|
|
|
return e
|
2018-07-24 15:49:49 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 14:27:10 +01:00
|
|
|
// FederationSenderHTTPClient returns FederationSenderInternalAPI for hitting
|
2019-10-01 17:09:47 +01:00
|
|
|
// the federation sender over HTTP
|
2020-06-04 14:27:10 +01:00
|
|
|
func (b *BaseDendrite) FederationSenderHTTPClient() federationSenderAPI.FederationSenderInternalAPI {
|
2020-10-01 11:55:17 +01:00
|
|
|
f, err := fsinthttp.NewFederationSenderClient(b.Cfg.FederationSenderURL(), b.apiHttpClient)
|
2020-04-03 11:40:50 +01:00
|
|
|
if err != nil {
|
2020-10-01 11:55:17 +01:00
|
|
|
logrus.WithError(err).Panic("FederationSenderHTTPClient failed", b.apiHttpClient)
|
2020-04-03 11:40:50 +01:00
|
|
|
}
|
|
|
|
return f
|
2019-10-01 17:09:47 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 16:26:35 +01:00
|
|
|
// ServerKeyAPIClient returns ServerKeyInternalAPI for hitting the server key API over HTTP
|
|
|
|
func (b *BaseDendrite) ServerKeyAPIClient() serverKeyAPI.ServerKeyInternalAPI {
|
|
|
|
f, err := skinthttp.NewServerKeyClient(
|
2020-05-27 10:19:24 +01:00
|
|
|
b.Cfg.ServerKeyAPIURL(),
|
2020-10-01 11:55:17 +01:00
|
|
|
b.apiHttpClient,
|
2020-06-05 16:42:01 +01:00
|
|
|
b.Caches,
|
2020-05-27 10:19:24 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("NewServerKeyInternalAPIHTTP failed", b.httpClient)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:35 +01:00
|
|
|
// KeyServerHTTPClient returns KeyInternalAPI for hitting the key server over HTTP
|
|
|
|
func (b *BaseDendrite) KeyServerHTTPClient() keyserverAPI.KeyInternalAPI {
|
2020-10-01 11:55:17 +01:00
|
|
|
f, err := keyinthttp.NewKeyServerClient(b.Cfg.KeyServerURL(), b.apiHttpClient)
|
2020-07-13 16:02:35 +01:00
|
|
|
if err != nil {
|
2020-10-01 11:55:17 +01:00
|
|
|
logrus.WithError(err).Panic("KeyServerHTTPClient failed", b.apiHttpClient)
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
// CreateAccountsDB creates a new instance of the accounts database. Should only
|
|
|
|
// be called once per component.
|
2020-02-13 17:27:33 +00:00
|
|
|
func (b *BaseDendrite) CreateAccountsDB() accounts.Database {
|
2020-08-10 14:18:04 +01:00
|
|
|
db, err := accounts.NewDatabase(&b.Cfg.UserAPI.AccountDatabase, b.Cfg.Global.ServerName)
|
2018-01-02 10:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to connect to accounts db")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2020-10-01 11:55:17 +01:00
|
|
|
// CreateClient creates a new client (normally used for media fetch requests).
|
|
|
|
// Should only be called once per component.
|
|
|
|
func (b *BaseDendrite) CreateClient() *gomatrixserverlib.Client {
|
|
|
|
client := gomatrixserverlib.NewClient(
|
|
|
|
b.Cfg.FederationSender.DisableTLSValidation,
|
|
|
|
)
|
|
|
|
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
// CreateFederationClient creates a new federation client. Should only be called
|
|
|
|
// once per component.
|
|
|
|
func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient {
|
2020-10-01 11:55:17 +01:00
|
|
|
client := gomatrixserverlib.NewFederationClient(
|
2020-08-10 14:18:04 +01:00
|
|
|
b.Cfg.Global.ServerName, b.Cfg.Global.KeyID, b.Cfg.Global.PrivateKey,
|
|
|
|
b.Cfg.FederationSender.DisableTLSValidation,
|
2018-01-02 10:26:56 +00:00
|
|
|
)
|
2020-10-01 11:55:17 +01:00
|
|
|
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
|
|
|
|
return client
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on
|
|
|
|
// ApiMux under /api/ and adds a prometheus handler under /metrics.
|
2020-08-13 12:16:37 +01:00
|
|
|
// nolint:gocyclo
|
|
|
|
func (b *BaseDendrite) SetupAndServeHTTP(
|
|
|
|
internalHTTPAddr, externalHTTPAddr config.HTTPAddress,
|
|
|
|
certFile, keyFile *string,
|
|
|
|
) {
|
|
|
|
internalAddr, _ := internalHTTPAddr.Address()
|
|
|
|
externalAddr, _ := externalHTTPAddr.Address()
|
|
|
|
|
2020-08-17 12:28:20 +01:00
|
|
|
internalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2020-08-13 12:16:37 +01:00
|
|
|
externalRouter := internalRouter
|
|
|
|
|
|
|
|
internalServ := &http.Server{
|
|
|
|
Addr: string(internalAddr),
|
2020-04-20 17:42:34 +01:00
|
|
|
WriteTimeout: HTTPServerTimeout,
|
2020-08-13 12:16:37 +01:00
|
|
|
Handler: internalRouter,
|
|
|
|
}
|
|
|
|
externalServ := internalServ
|
|
|
|
|
2020-08-13 18:27:19 +01:00
|
|
|
if externalAddr != NoExternalListener && externalAddr != internalAddr {
|
2020-08-17 12:28:20 +01:00
|
|
|
externalRouter = mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2020-08-13 12:16:37 +01:00
|
|
|
externalServ = &http.Server{
|
|
|
|
Addr: string(externalAddr),
|
|
|
|
WriteTimeout: HTTPServerTimeout,
|
|
|
|
Handler: externalRouter,
|
|
|
|
}
|
2020-04-20 17:42:34 +01:00
|
|
|
}
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-08-13 12:16:37 +01:00
|
|
|
internalRouter.PathPrefix(httputil.InternalPathPrefix).Handler(b.InternalAPIMux)
|
|
|
|
if b.Cfg.Global.Metrics.Enabled {
|
|
|
|
internalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), b.Cfg.Global.Metrics.BasicAuth))
|
|
|
|
}
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-08-13 12:16:37 +01:00
|
|
|
externalRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(b.PublicClientAPIMux)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicKeyPathPrefix).Handler(b.PublicKeyAPIMux)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicFederationPathPrefix).Handler(b.PublicFederationAPIMux)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.PublicMediaAPIMux)
|
|
|
|
|
|
|
|
go func() {
|
2020-08-13 18:27:19 +01:00
|
|
|
logrus.Infof("Starting %s listener on %s", b.componentName, internalServ.Addr)
|
2020-08-13 12:16:37 +01:00
|
|
|
if certFile != nil && keyFile != nil {
|
2020-08-13 18:27:19 +01:00
|
|
|
if err := internalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
|
2020-08-13 12:16:37 +01:00
|
|
|
logrus.WithError(err).Fatal("failed to serve HTTPS")
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-13 18:27:19 +01:00
|
|
|
if err := internalServ.ListenAndServe(); err != nil {
|
2020-08-13 12:16:37 +01:00
|
|
|
logrus.WithError(err).Fatal("failed to serve HTTP")
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 18:27:19 +01:00
|
|
|
logrus.Infof("Stopped %s listener on %s", b.componentName, internalServ.Addr)
|
2020-08-13 12:16:37 +01:00
|
|
|
}()
|
|
|
|
|
2020-08-13 18:27:19 +01:00
|
|
|
if externalAddr != NoExternalListener && internalAddr != externalAddr {
|
2020-08-13 12:16:37 +01:00
|
|
|
go func() {
|
2020-08-13 18:27:19 +01:00
|
|
|
logrus.Infof("Starting %s listener on %s", b.componentName, externalServ.Addr)
|
2020-08-13 12:16:37 +01:00
|
|
|
if certFile != nil && keyFile != nil {
|
2020-08-13 18:27:19 +01:00
|
|
|
if err := externalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
|
2020-08-13 12:16:37 +01:00
|
|
|
logrus.WithError(err).Fatal("failed to serve HTTPS")
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-13 18:27:19 +01:00
|
|
|
if err := externalServ.ListenAndServe(); err != nil {
|
2020-08-13 12:16:37 +01:00
|
|
|
logrus.WithError(err).Fatal("failed to serve HTTP")
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 18:27:19 +01:00
|
|
|
logrus.Infof("Stopped %s listener on %s", b.componentName, externalServ.Addr)
|
2020-08-13 12:16:37 +01:00
|
|
|
}()
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 12:16:37 +01:00
|
|
|
select {}
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 17:27:33 +00:00
|
|
|
// setupKafka creates kafka consumer/producer pair from the config.
|
2018-01-02 10:26:56 +00:00
|
|
|
func setupKafka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) {
|
2020-08-10 14:18:04 +01:00
|
|
|
consumer, err := sarama.NewConsumer(cfg.Global.Kafka.Addresses, nil)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("failed to start kafka consumer")
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:18:04 +01:00
|
|
|
producer, err := sarama.NewSyncProducer(cfg.Global.Kafka.Addresses, nil)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("failed to setup kafka producers")
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumer, producer
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupNaffka creates kafka consumer/producer pair from the config.
|
|
|
|
func setupNaffka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) {
|
2020-08-24 13:49:20 +01:00
|
|
|
naffkaDB, err := naffkaStorage.NewDatabase(string(cfg.Global.Kafka.Database.ConnectionString))
|
2020-08-10 14:18:04 +01:00
|
|
|
if err != nil {
|
2020-08-24 13:49:20 +01:00
|
|
|
logrus.WithError(err).Panic("Failed to setup naffka database")
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
2020-02-13 17:27:33 +00:00
|
|
|
naff, err := naffka.New(naffkaDB)
|
2018-01-02 10:26:56 +00:00
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
logrus.WithError(err).Panic("Failed to setup naffka")
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
2020-02-13 17:27:33 +00:00
|
|
|
return naff, naff
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|