2017-03-22 17:39:08 +00:00
package main
import (
2017-03-29 14:09:27 +01:00
"flag"
"io/ioutil"
2017-03-22 17:39:08 +00:00
"net/http"
"os"
"path/filepath"
2017-04-20 17:22:44 +01:00
"github.com/matrix-org/dendrite/syncapi/config"
"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-03-22 17:39:08 +00:00
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dugong"
2017-03-29 14:09:27 +01:00
yaml "gopkg.in/yaml.v2"
2017-03-22 17:39:08 +00:00
)
2017-03-29 14:09:27 +01:00
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." )
2017-03-22 17:39:08 +00:00
func setupLogging ( logDir string ) {
_ = os . Mkdir ( logDir , os . ModePerm )
log . AddHook ( dugong . NewFSHook (
filepath . Join ( logDir , "info.log" ) ,
filepath . Join ( logDir , "warn.log" ) ,
filepath . Join ( logDir , "error.log" ) ,
& log . TextFormatter {
TimestampFormat : "2006-01-02 15:04:05.000000" ,
DisableColors : true ,
DisableTimestamp : false ,
DisableSorting : false ,
} , & dugong . DailyRotationSchedule { GZip : true } ,
) )
}
2017-03-29 14:09:27 +01:00
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
}
2017-03-22 17:39:08 +00:00
func main ( ) {
2017-03-29 14:09:27 +01:00
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" )
2017-03-22 17:39:08 +00:00
}
logDir := os . Getenv ( "LOG_DIR" )
if logDir != "" {
setupLogging ( logDir )
}
2017-03-29 14:09:27 +01:00
log . Info ( "sync server config: " , cfg )
2017-03-22 17:39:08 +00:00
2017-03-29 14:05:43 +01:00
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 )
}
2017-04-10 15:12:18 +01:00
rp , err := sync . NewRequestPool ( db )
if err != nil {
log . Panicf ( "startup: Failed to create request pool : %s" , err )
}
2017-04-07 14:32:42 +01:00
2017-04-12 16:06:26 +01:00
server , err := consumers . NewServer ( cfg , rp , db )
2017-03-29 14:05:43 +01:00
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" )
}
2017-03-29 14:09:27 +01:00
log . Info ( "Starting sync server on " , * bindAddr )
2017-04-07 14:32:42 +01:00
routing . SetupSyncServerListeners ( http . DefaultServeMux , http . DefaultClient , * cfg , rp )
2017-03-29 14:09:27 +01:00
log . Fatal ( http . ListenAndServe ( * bindAddr , nil ) )
2017-03-22 17:39:08 +00:00
}