From ce2517b9fb70403d1dbeb52194ccd55122f8ed14 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 11 Jun 2020 10:16:46 +0100 Subject: [PATCH] Support embedding Riot Web (#1113) * Embed * Support for embedding is better now * HTTP on localhost for Ygg demo * Move embed into Yggdrasil demo --- .gitignore | 3 + .../embed/embed_other.go | 7 +++ .../embed/embed_riotweb.go | 61 +++++++++++++++++++ cmd/dendrite-demo-yggdrasil/main.go | 5 +- go.mod | 1 + 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 cmd/dendrite-demo-yggdrasil/embed/embed_other.go create mode 100644 cmd/dendrite-demo-yggdrasil/embed/embed_riotweb.go diff --git a/.gitignore b/.gitignore index 1de8887c..34c5b680 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,6 @@ dendrite.yaml # Log files *.log* + +# Generated code +cmd/dendrite-demo-yggdrasil/embed/fs*.go diff --git a/cmd/dendrite-demo-yggdrasil/embed/embed_other.go b/cmd/dendrite-demo-yggdrasil/embed/embed_other.go new file mode 100644 index 00000000..a9108fad --- /dev/null +++ b/cmd/dendrite-demo-yggdrasil/embed/embed_other.go @@ -0,0 +1,7 @@ +// +build !riotweb + +package embed + +func Embed(_ int, _ string) { + +} diff --git a/cmd/dendrite-demo-yggdrasil/embed/embed_riotweb.go b/cmd/dendrite-demo-yggdrasil/embed/embed_riotweb.go new file mode 100644 index 00000000..360d0bc5 --- /dev/null +++ b/cmd/dendrite-demo-yggdrasil/embed/embed_riotweb.go @@ -0,0 +1,61 @@ +// +build riotweb + +package embed + +import ( + "fmt" + "io" + "net/http" + + "github.com/tidwall/sjson" +) + +// From within the Riot Web directory: +// go run github.com/mjibson/esc -o /path/to/dendrite/internal/embed/fs_riotweb.go -private -pkg embed . + +func Embed(listenPort int, serverName string) { + url := fmt.Sprintf("http://localhost:%d", listenPort) + embeddedFS := _escFS(false) + embeddedServ := http.FileServer(embeddedFS) + + http.DefaultServeMux.Handle("/", embeddedServ) + http.DefaultServeMux.HandleFunc("/config.json", func(w http.ResponseWriter, _ *http.Request) { + configFile, err := embeddedFS.Open("/config.sample.json") + if err != nil { + w.WriteHeader(500) + io.WriteString(w, "Couldn't open the file: "+err.Error()) + return + } + configFileInfo, err := configFile.Stat() + if err != nil { + w.WriteHeader(500) + io.WriteString(w, "Couldn't stat the file: "+err.Error()) + return + } + buf := make([]byte, configFileInfo.Size()) + n, err := configFile.Read(buf) + if err != nil { + w.WriteHeader(500) + io.WriteString(w, "Couldn't read the file: "+err.Error()) + return + } + if int64(n) != configFileInfo.Size() { + w.WriteHeader(500) + io.WriteString(w, "The returned file size didn't match what we expected") + return + } + js, _ := sjson.SetBytes(buf, "default_server_config.m\\.homeserver.base_url", url) + js, _ = sjson.SetBytes(js, "default_server_config.m\\.homeserver.server_name", serverName) + js, _ = sjson.SetBytes(js, "brand", fmt.Sprintf("Riot %s", serverName)) + js, _ = sjson.SetBytes(js, "disable_guests", true) + js, _ = sjson.SetBytes(js, "disable_3pid_login", true) + js, _ = sjson.DeleteBytes(js, "welcomeUserId") + _, _ = w.Write(js) + }) + + fmt.Println("*-------------------------------*") + fmt.Println("| This build includes Riot Web! |") + fmt.Println("*-------------------------------*") + fmt.Println("Point your browser to:", url) + fmt.Println() +} diff --git a/cmd/dendrite-demo-yggdrasil/main.go b/cmd/dendrite-demo-yggdrasil/main.go index b1530e88..6ef56d32 100644 --- a/cmd/dendrite-demo-yggdrasil/main.go +++ b/cmd/dendrite-demo-yggdrasil/main.go @@ -28,6 +28,7 @@ import ( "github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/convert" + "github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/embed" "github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing" "github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/yggconn" "github.com/matrix-org/dendrite/eduserver" @@ -166,6 +167,8 @@ func main() { logrus.WithError(err).Panicf("failed to connect to public rooms db") } + embed.Embed(*instancePort, "Yggdrasil Demo") + monolith := setup.Monolith{ Config: base.Cfg, AccountDB: accountDB, @@ -198,7 +201,7 @@ func main() { logrus.Fatal(httpServer.Serve(ygg)) }() go func() { - httpBindAddr := fmt.Sprintf(":%d", *instancePort) + httpBindAddr := fmt.Sprintf("localhost:%d", *instancePort) logrus.Info("Listening on ", httpBindAddr) logrus.Fatal(http.ListenAndServe(httpBindAddr, nil)) }() diff --git a/go.mod b/go.mod index a93f9a83..a05bb71e 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/prometheus/client_golang v1.4.1 github.com/sirupsen/logrus v1.6.0 github.com/tidwall/gjson v1.6.0 + github.com/tidwall/sjson v1.0.3 github.com/uber-go/atomic v1.3.0 // indirect github.com/uber/jaeger-client-go v2.15.0+incompatible github.com/uber/jaeger-lib v1.5.0