clientapi: Call things addresses if possibly URL or host:port

This commit is contained in:
Robert Swain 2017-04-21 16:47:23 +02:00
parent 8ae95c5906
commit ded4c98715
4 changed files with 40 additions and 41 deletions

View file

@ -17,7 +17,6 @@ package clientapi
import (
"fmt"
"net/http"
"net/url"
"strings"
"golang.org/x/crypto/ed25519"
@ -31,21 +30,17 @@ import (
)
// App is a function that configures and starts a client API server
func App(host string, kafkaURIsStr string, roomserverURL string, topicPrefix string) {
if host == "" {
log.Panic("No host specified.")
func App(address string, kafkaAddressesStr string, roomserverURL string, topicPrefix string) {
if address == "" {
log.Panic("No address specified.")
}
hostURL, err := url.Parse(host)
if hostURL.Port() == "" {
host += ":7778"
}
kafkaURIs := strings.Split(kafkaURIsStr, ",")
if len(kafkaURIs) == 0 {
kafkaAddresses := strings.Split(kafkaAddressesStr, ",")
if len(kafkaAddresses) == 0 {
// the kafka default is :9092
kafkaURIs = []string{"localhost:9092"}
kafkaAddresses = []string{"localhost:9092"}
}
if roomserverURL == "" {
log.Panic("No roomserver host specified.")
log.Panic("No roomserver URL specified.")
}
clientAPIOutputTopic := fmt.Sprintf("%sroomserver_input_topic", topicPrefix)
@ -61,24 +56,24 @@ func App(host string, kafkaURIsStr string, roomserverURL string, topicPrefix str
ServerName: "localhost",
KeyID: "ed25519:something",
PrivateKey: privKey,
KafkaProducerURIs: kafkaURIs,
KafkaProducerAddresses: kafkaAddresses,
ClientAPIOutputTopic: clientAPIOutputTopic,
RoomserverURL: roomserverURL,
}
log.Infoln("clientapi host:", host)
log.Infoln("clientapi address:", address)
log.Infoln("clientapi output topic:", clientAPIOutputTopic)
log.Infoln("kafka URIs:", kafkaURIs)
log.Infoln("kafka addresses:", kafkaAddresses)
log.Infoln("roomserver URL:", roomserverURL)
log.Info("Starting clientapi")
roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerURIs, cfg.ClientAPIOutputTopic)
roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerAddresses, cfg.ClientAPIOutputTopic)
if err != nil {
log.Panicf("Failed to setup kafka producers(%s): %s", cfg.KafkaProducerURIs, err)
log.Panicf("Failed to setup kafka producers(%s): %s", cfg.KafkaProducerAddresses, err)
}
queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomserverURL, nil)
routing.Setup(http.DefaultServeMux, http.DefaultClient, cfg, roomserverProducer, queryAPI)
log.Fatal(http.ListenAndServe(host, nil))
log.Fatal(http.ListenAndServe(address, nil))
}

View file

@ -25,8 +25,8 @@ type ClientAPI struct {
// An arbitrary string used to uniquely identify the PrivateKey. Must start with the
// prefix "ed25519:".
KeyID string
// A list of URIs to send events to. These kafka logs should be consumed by a Room Server.
KafkaProducerURIs []string
// A list of addresses to send events to. These kafka logs should be consumed by a Room Server.
KafkaProducerAddresses []string
// The topic for events which are written to the logs.
ClientAPIOutputTopic string
// The URL of the roomserver which can service Query API requests

View file

@ -65,16 +65,16 @@ func main() {
ServerName: "localhost",
KeyID: "ed25519:something",
PrivateKey: privKey,
KafkaProducerURIs: kafkaURIs,
KafkaProducerAddresses: kafkaURIs,
ClientAPIOutputTopic: clientAPIOutputTopic,
RoomserverURL: roomserverURL,
}
log.Info("Starting clientapi")
roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerURIs, cfg.ClientAPIOutputTopic)
roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerAddresses, cfg.ClientAPIOutputTopic)
if err != nil {
log.Panicf("Failed to setup kafka producers(%s): %s", cfg.KafkaProducerURIs, err)
log.Panicf("Failed to setup kafka producers(%s): %s", cfg.KafkaProducerAddresses, err)
}
queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomserverURL, nil)

View file

@ -41,16 +41,16 @@ Arguments:
<server-type> One of: client-api, room-server, sync-api.
Options:
-c <config-file>, --config-file=<config-file>
Path to a YAML-format configuration file.
-H <host>, --host=<host>
Host to bind. The port is optional and ignored for 'serve all'. The
-a <address>, --address=<address>
Address to bind. The port is required but ignored for 'serve all'. The
default ports are: 7776 (sync-api), 7777 (room-server), and 7778 (client-api)
[default: localhost]
-c <config-file>, --config-file=<config-file>
Path to a YAML-format configuration file.
-h, --help
Print this usage text.
-k <kafka-hosts>, --kafka-hosts=<kafka-hosts>
A comma-separated list of Kafka hosts. [default: localhost:9092]
-k <kafka-addresses>, --kafka-addresses=<kafka-addresses>
A comma-separated list of Kafka addresses. [default: localhost:9092]
-l <log-dir>, --log-dir=<log-dir>
Path to log directory. If not set, logs will only be written to stderr.
-r <room-server-host>, --room-server-host=<room-server-host>
@ -86,9 +86,13 @@ Environment Variables:
switch serverType := args["<server-type>"]; serverType {
case "client-api":
log.Infof("Starting %v server...", serverType)
address := maybeArgToStr(args["--address"])
if address == "localhost" {
address += ":7778"
}
clientapi.App(
maybeArgToStr(args["--host"]),
maybeArgToStr(args["--kafka-hosts"]),
address,
maybeArgToStr(args["--kafka-addresses"]),
maybeArgToStr(args["--room-server-host"]),
maybeArgToStr(args["--topic-prefix"]),
)