2017-03-22 17:39:08 +00:00
package main
import (
2017-03-29 13:09:27 +00:00
"flag"
"io/ioutil"
2017-03-22 17:39:08 +00:00
"net/http"
"os"
"path/filepath"
"github.com/matrix-org/dendrite/clientapi/config"
"github.com/matrix-org/dendrite/clientapi/routing"
2017-03-29 13:05:43 +00:00
"github.com/matrix-org/dendrite/clientapi/storage"
"github.com/matrix-org/dendrite/clientapi/sync"
2017-03-22 17:39:08 +00:00
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dugong"
2017-03-29 13:09:27 +00:00
yaml "gopkg.in/yaml.v2"
2017-03-22 17:39:08 +00:00
)
2017-03-29 13:09:27 +00: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 13:09:27 +00: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 13:09:27 +00: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 13:09:27 +00:00
log . Info ( "sync server config: " , cfg )
2017-03-22 17:39:08 +00:00
2017-03-29 13:05:43 +00: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-03-29 13:09:27 +00:00
server , err := sync . NewServer ( cfg , db )
2017-03-29 13:05:43 +00: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 13:09:27 +00:00
log . Info ( "Starting sync server on " , * bindAddr )
routing . SetupSyncServerListeners ( http . DefaultServeMux , http . DefaultClient , * cfg )
log . Fatal ( http . ListenAndServe ( * bindAddr , nil ) )
2017-03-22 17:39:08 +00:00
}