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 ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"strings" "strings"
"golang.org/x/crypto/ed25519" "golang.org/x/crypto/ed25519"
@ -31,21 +30,17 @@ import (
) )
// App is a function that configures and starts a client API server // App is a function that configures and starts a client API server
func App(host string, kafkaURIsStr string, roomserverURL string, topicPrefix string) { func App(address string, kafkaAddressesStr string, roomserverURL string, topicPrefix string) {
if host == "" { if address == "" {
log.Panic("No host specified.") log.Panic("No address specified.")
} }
hostURL, err := url.Parse(host) kafkaAddresses := strings.Split(kafkaAddressesStr, ",")
if hostURL.Port() == "" { if len(kafkaAddresses) == 0 {
host += ":7778"
}
kafkaURIs := strings.Split(kafkaURIsStr, ",")
if len(kafkaURIs) == 0 {
// the kafka default is :9092 // the kafka default is :9092
kafkaURIs = []string{"localhost:9092"} kafkaAddresses = []string{"localhost:9092"}
} }
if roomserverURL == "" { if roomserverURL == "" {
log.Panic("No roomserver host specified.") log.Panic("No roomserver URL specified.")
} }
clientAPIOutputTopic := fmt.Sprintf("%sroomserver_input_topic", topicPrefix) clientAPIOutputTopic := fmt.Sprintf("%sroomserver_input_topic", topicPrefix)
@ -61,24 +56,24 @@ func App(host string, kafkaURIsStr string, roomserverURL string, topicPrefix str
ServerName: "localhost", ServerName: "localhost",
KeyID: "ed25519:something", KeyID: "ed25519:something",
PrivateKey: privKey, PrivateKey: privKey,
KafkaProducerURIs: kafkaURIs, KafkaProducerAddresses: kafkaAddresses,
ClientAPIOutputTopic: clientAPIOutputTopic, ClientAPIOutputTopic: clientAPIOutputTopic,
RoomserverURL: roomserverURL, RoomserverURL: roomserverURL,
} }
log.Infoln("clientapi host:", host) log.Infoln("clientapi address:", address)
log.Infoln("clientapi output topic:", clientAPIOutputTopic) log.Infoln("clientapi output topic:", clientAPIOutputTopic)
log.Infoln("kafka URIs:", kafkaURIs) log.Infoln("kafka addresses:", kafkaAddresses)
log.Infoln("roomserver URL:", roomserverURL) log.Infoln("roomserver URL:", roomserverURL)
log.Info("Starting clientapi") log.Info("Starting clientapi")
roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerURIs, cfg.ClientAPIOutputTopic) roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerAddresses, cfg.ClientAPIOutputTopic)
if err != nil { 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) queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomserverURL, nil)
routing.Setup(http.DefaultServeMux, http.DefaultClient, cfg, roomserverProducer, queryAPI) 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 // An arbitrary string used to uniquely identify the PrivateKey. Must start with the
// prefix "ed25519:". // prefix "ed25519:".
KeyID string KeyID string
// A list of URIs to send events to. These kafka logs should be consumed by a Room Server. // A list of addresses to send events to. These kafka logs should be consumed by a Room Server.
KafkaProducerURIs []string KafkaProducerAddresses []string
// The topic for events which are written to the logs. // The topic for events which are written to the logs.
ClientAPIOutputTopic string ClientAPIOutputTopic string
// The URL of the roomserver which can service Query API requests // The URL of the roomserver which can service Query API requests

View file

@ -65,16 +65,16 @@ func main() {
ServerName: "localhost", ServerName: "localhost",
KeyID: "ed25519:something", KeyID: "ed25519:something",
PrivateKey: privKey, PrivateKey: privKey,
KafkaProducerURIs: kafkaURIs, KafkaProducerAddresses: kafkaURIs,
ClientAPIOutputTopic: clientAPIOutputTopic, ClientAPIOutputTopic: clientAPIOutputTopic,
RoomserverURL: roomserverURL, RoomserverURL: roomserverURL,
} }
log.Info("Starting clientapi") log.Info("Starting clientapi")
roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerURIs, cfg.ClientAPIOutputTopic) roomserverProducer, err := producers.NewRoomserverProducer(cfg.KafkaProducerAddresses, cfg.ClientAPIOutputTopic)
if err != nil { 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) queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomserverURL, nil)

View file

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