2017-05-26 07:57:09 +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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-08 13:40:51 +00:00
|
|
|
"flag"
|
2017-05-26 07:57:09 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2017-08-03 14:10:39 +00:00
|
|
|
"github.com/gorilla/mux"
|
2017-09-28 13:50:40 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
2017-05-26 07:57:09 +00:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-06-19 14:21:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-05-26 07:57:09 +00:00
|
|
|
"github.com/matrix-org/dendrite/mediaapi/routing"
|
2017-05-26 14:49:54 +00:00
|
|
|
"github.com/matrix-org/dendrite/mediaapi/storage"
|
2017-09-21 15:20:10 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-26 07:57:09 +00:00
|
|
|
|
2017-10-13 13:42:57 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-05-26 07:57:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
logDir = os.Getenv("LOG_DIR")
|
2017-07-28 10:32:17 +00:00
|
|
|
configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.")
|
2017-05-26 07:57:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
common.SetupLogging(logDir)
|
|
|
|
|
2017-06-08 13:40:51 +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-05-26 07:57:09 +00:00
|
|
|
if err != nil {
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Fatalf("Invalid config file: %s", err)
|
2017-05-26 07:57:09 +00:00
|
|
|
}
|
2017-06-06 23:12:49 +00:00
|
|
|
|
2017-09-28 16:00:23 +00:00
|
|
|
closer, err := cfg.SetupTracing("DendriteMediaAPI")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatalf("Failed to start tracer")
|
|
|
|
}
|
|
|
|
defer closer.Close() // nolint: errcheck
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
db, err := storage.Open(string(cfg.Database.MediaAPI))
|
2017-05-26 07:57:09 +00:00
|
|
|
if err != nil {
|
2017-06-19 14:21:04 +00:00
|
|
|
log.WithError(err).Panic("Failed to open database")
|
2017-06-06 23:12:49 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 13:50:40 +00:00
|
|
|
deviceDB, err := devices.NewDatabase(string(cfg.Database.Device), cfg.Matrix.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Panicf("Failed to setup device database(%q)", cfg.Database.Device)
|
|
|
|
}
|
|
|
|
|
2017-09-21 15:20:10 +00:00
|
|
|
client := gomatrixserverlib.NewClient()
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Info("Starting media API server on ", cfg.Listen.MediaAPI)
|
2017-06-06 23:12:49 +00:00
|
|
|
|
2017-08-03 14:10:39 +00:00
|
|
|
api := mux.NewRouter()
|
2017-09-28 13:50:40 +00:00
|
|
|
routing.Setup(api, cfg, db, deviceDB, client)
|
2017-12-06 09:36:50 +00:00
|
|
|
common.SetupHTTPAPI(http.DefaultServeMux, common.WrapHandlerInCORS(api))
|
2017-08-03 14:10:39 +00:00
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
log.Fatal(http.ListenAndServe(string(cfg.Listen.MediaAPI), nil))
|
2017-05-26 07:57:09 +00:00
|
|
|
}
|