From 8613b7ee9e524c5341f323b5db124cca33d7d402 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Wed, 16 Aug 2017 12:14:30 +0100 Subject: [PATCH] Create public rooms component --- .../dendrite/cmd/client-api-proxy/main.go | 33 ++++++++--- .../dendrite-public-rooms-api-server/main.go | 58 +++++++++++++++++++ 2 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go diff --git a/src/github.com/matrix-org/dendrite/cmd/client-api-proxy/main.go b/src/github.com/matrix-org/dendrite/cmd/client-api-proxy/main.go index f2b64d44a..283c308e2 100644 --- a/src/github.com/matrix-org/dendrite/cmd/client-api-proxy/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/client-api-proxy/main.go @@ -47,12 +47,13 @@ Arguments: ` var ( - syncServerURL = flag.String("sync-api-server-url", "", "The base URL of the listening 'dendrite-sync-api-server' process. E.g. 'http://localhost:4200'") - clientAPIURL = flag.String("client-api-server-url", "", "The base URL of the listening 'dendrite-client-api-server' process. E.g. 'http://localhost:4321'") - mediaAPIURL = flag.String("media-api-server-url", "", "The base URL of the listening 'dendrite-media-api-server' process. E.g. 'http://localhost:7779'") - bindAddress = flag.String("bind-address", ":8008", "The listening port for the proxy.") - certFile = flag.String("tls-cert", "server.crt", "The PEM formatted X509 certificate to use for TLS") - keyFile = flag.String("tls-key", "server.key", "The PEM private key to use for TLS") + syncServerURL = flag.String("sync-api-server-url", "", "The base URL of the listening 'dendrite-sync-api-server' process. E.g. 'http://localhost:4200'") + clientAPIURL = flag.String("client-api-server-url", "", "The base URL of the listening 'dendrite-client-api-server' process. E.g. 'http://localhost:4321'") + mediaAPIURL = flag.String("media-api-server-url", "", "The base URL of the listening 'dendrite-media-api-server' process. E.g. 'http://localhost:7779'") + publicRoomsAPIURL = flag.String("public-rooms-api-server-url", "", "The base URL of the listening 'dendrite-public-rooms-api-server' process. E.g. 'http://localhost:7775'") + bindAddress = flag.String("bind-address", ":8008", "The listening port for the proxy.") + certFile = flag.String("tls-cert", "server.crt", "The PEM formatted X509 certificate to use for TLS") + keyFile = flag.String("tls-key", "server.key", "The PEM private key to use for TLS") ) func makeProxy(targetURL string) (*httputil.ReverseProxy, error) { @@ -122,6 +123,12 @@ func main() { os.Exit(1) } + if *publicRoomsAPIURL == "" { + flag.Usage() + fmt.Fprintln(os.Stderr, "no --public-rooms-api-server-url specified.") + os.Exit(1) + } + syncProxy, err := makeProxy(*syncServerURL) if err != nil { panic(err) @@ -134,8 +141,14 @@ func main() { if err != nil { panic(err) } + publicRoomsProxy, err := makeProxy(*publicRoomsAPIURL) + if err != nil { + panic(err) + } http.Handle("/_matrix/client/r0/sync", syncProxy) + http.Handle("/_matrix/client/r0/directory/list/", publicRoomsProxy) + http.Handle("/_matrix/client/r0/publicRooms", publicRoomsProxy) http.Handle("/_matrix/media/v1/", mediaProxy) http.Handle("/", clientProxy) @@ -146,9 +159,11 @@ func main() { } fmt.Println("Proxying requests to:") - fmt.Println(" /_matrix/client/r0/sync => ", *syncServerURL+"/api/_matrix/client/r0/sync") - fmt.Println(" /_matrix/media/v1 => ", *mediaAPIURL+"/api/_matrix/media/v1") - fmt.Println(" /* => ", *clientAPIURL+"/api/*") + fmt.Println(" /_matrix/client/r0/sync => ", *syncServerURL+"/api/_matrix/client/r0/sync") + fmt.Println(" /_matrix/client/r0/directory/list => ", *publicRoomsAPIURL+"/_matrix/client/r0/directory/list") + fmt.Println(" /_matrix/client/r0/publicRooms => ", *publicRoomsAPIURL+"/_matrix/media/client/r0/publicRooms") + fmt.Println(" /_matrix/media/v1 => ", *mediaAPIURL+"/api/_matrix/media/v1") + fmt.Println(" /* => ", *clientAPIURL+"/api/*") fmt.Println("Listening on ", *bindAddress) if *certFile != "" && *keyFile != "" { panic(srv.ListenAndServeTLS(*certFile, *keyFile)) diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go new file mode 100644 index 000000000..f2c35d6a6 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-public-rooms-api-server/main.go @@ -0,0 +1,58 @@ +// Copyright 2017 Vector Creations Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "flag" + "net/http" + "os" + + "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/common" + "github.com/matrix-org/dendrite/common/config" + "github.com/matrix-org/dendrite/publicroomsapi/routing" + "github.com/matrix-org/dendrite/publicroomsapi/storage" + + log "github.com/Sirupsen/logrus" +) + +var configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") + +func main() { + common.SetupLogging(os.Getenv("LOG_DIR")) + + flag.Parse() + + if *configPath == "" { + log.Fatal("--config must be supplied") + } + cfg, err := config.Load(*configPath) + if err != nil { + log.Fatalf("Invalid config file: %s", err) + } + + db, err := storage.NewPublicRoomsServerDatabase(string(cfg.Database.PublicRoomsAPI)) + if err != nil { + log.Panicf("startup: failed to create public rooms server database with data source %s : %s", cfg.Database.PublicRoomsAPI, err) + } + + log.Info("Starting public rooms server on ", cfg.Listen.PublicRoomsAPI) + + api := mux.NewRouter() + routing.Setup(api, db) + common.SetupHTTPAPI(http.DefaultServeMux, api) + + log.Fatal(http.ListenAndServe(string(cfg.Listen.PublicRoomsAPI), nil)) +}