2017-02-03 07:52:32 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/input"
|
2017-03-06 08:29:39 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/query"
|
2017-02-03 07:52:32 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
|
|
|
sarama "gopkg.in/Shopify/sarama.v1"
|
2017-03-06 08:29:39 -06:00
|
|
|
"net/http"
|
2017-03-17 08:01:07 -05:00
|
|
|
_ "net/http/pprof"
|
2017-02-03 07:52:32 -06:00
|
|
|
"os"
|
2017-03-17 08:01:07 -05:00
|
|
|
"strconv"
|
2017-02-03 07:52:32 -06:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-27 05:25:35 -06:00
|
|
|
database = os.Getenv("DATABASE")
|
|
|
|
kafkaURIs = strings.Split(os.Getenv("KAFKA_URIS"), ",")
|
|
|
|
inputRoomEventTopic = os.Getenv("TOPIC_INPUT_ROOM_EVENT")
|
|
|
|
outputRoomEventTopic = os.Getenv("TOPIC_OUTPUT_ROOM_EVENT")
|
2017-03-06 08:29:39 -06:00
|
|
|
bindAddr = os.Getenv("BIND_ADDRESS")
|
2017-03-17 08:01:07 -05:00
|
|
|
// Shuts the roomserver down after processing a given number of messages.
|
|
|
|
// This is useful for running benchmarks for seeing how quickly the server
|
|
|
|
// can process a given number of messages.
|
|
|
|
stopProcessingAfter = os.Getenv("STOP_AFTER")
|
2017-02-03 07:52:32 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
db, err := storage.Open(database)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kafkaConsumer, err := sarama.NewConsumer(kafkaURIs, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-02-27 05:25:35 -06:00
|
|
|
kafkaProducer, err := sarama.NewSyncProducer(kafkaURIs, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-02-03 07:52:32 -06:00
|
|
|
consumer := input.Consumer{
|
2017-02-27 05:25:35 -06:00
|
|
|
Consumer: kafkaConsumer,
|
|
|
|
DB: db,
|
|
|
|
Producer: kafkaProducer,
|
|
|
|
InputRoomEventTopic: inputRoomEventTopic,
|
|
|
|
OutputRoomEventTopic: outputRoomEventTopic,
|
2017-02-03 07:52:32 -06:00
|
|
|
}
|
|
|
|
|
2017-03-17 08:01:07 -05:00
|
|
|
if stopProcessingAfter != "" {
|
|
|
|
count, err := strconv.ParseInt(stopProcessingAfter, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
consumer.StopProcessingAfter = &count
|
|
|
|
consumer.ShutdownCallback = func(message string) {
|
|
|
|
fmt.Println("Stopping roomserver", message)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 07:52:32 -06:00
|
|
|
if err = consumer.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-03-06 08:29:39 -06:00
|
|
|
queryAPI := query.RoomserverQueryAPI{
|
|
|
|
DB: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
queryAPI.SetupHTTP(http.DefaultServeMux)
|
|
|
|
|
2017-02-03 07:52:32 -06:00
|
|
|
fmt.Println("Started roomserver")
|
|
|
|
|
|
|
|
// TODO: Implement clean shutdown.
|
2017-03-17 08:01:07 -05:00
|
|
|
if err := http.ListenAndServe(bindAddr, nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-02-03 07:52:32 -06:00
|
|
|
}
|