From caaf694b6c3f83beb37d2d0cb05d9ccf894b84a8 Mon Sep 17 00:00:00 2001 From: Vasile Popescu Date: Thu, 5 Apr 2018 23:03:17 +0200 Subject: [PATCH] Bundle the frontend resources inside the server binary Use go-bindata to bundle all the frontend server resources inside the server binary, so it's easier to re-distributed. --- .gitignore | 3 +++ Makefile | 25 +++++++++++++++++++++++-- frontend/src/main.js | 2 ++ tty-server/server.go | 29 +++++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index d5ab26b..ead350d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +public/ +output.log **/node_modules/ .DS_Store bundle.js @@ -6,3 +8,4 @@ playground/ tty_sender tty_server tmp-* +tty-server/assets_bundle.go diff --git a/Makefile b/Makefile index b34c93e..d0be0b9 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,40 @@ TTY_SERVER=tty_server TTY_SENDER=tty_sender -TTY_SERVER_SRC=$(wildcard ./tty-server/*.go) +# We need to make sure the assets_bundle is in the list only onces in both these two special cases: +# a) first time, when the assets_bundle.go is generated, and b) when it's already existing there, +# but it has to be re-generated. +# Unfortunately, the assets_bundle.go seems to have to be in the same folder as the rest of the +# server sources, so that's why all this mess +TTY_SERVER_SRC=$(filter-out ./tty-server/assets_bundle.go, $(wildcard ./tty-server/*.go)) ./tty-server/assets_bundle.go TTY_SENDER_SRC=$(wildcard ./tty-sender/*.go) EXTRA_BUILD_DEPS=$(wildcard ./common/*go) +TTY_SERVER_ASSETS=$(addprefix ./public/templates/,$(notdir $(wildcard ./frontend/templates/*))) public/bundle.js all: $(TTY_SERVER) $(TTY_SENDER) @echo "All done" -$(TTY_SERVER): $(TTY_SERVER_SRC) $(EXTRA_BUILD_DEPS) +$(TTY_SERVER): $(TTY_SERVER_SRC) $(EXTRA_BUILD_DEPS) $(TTY_SERVER_ASSETS) go build -o $@ $(TTY_SERVER_SRC) $(TTY_SENDER): $(TTY_SENDER_SRC) $(EXTRA_BUILD_DEPS) go build -o $@ $(TTY_SENDER_SRC) +# TODO: perhaps replace all these paths with variables? +frontend/bundle.js: $(wildcard ./frontend/src/*) + cd frontend && npm run build && cd - + +public/bundle.js: frontend/bundle.js + mkdir -p $(dir $@) + cp $^ $@ + +public/templates/%: frontend/templates/% + mkdir -p $(dir $@) + cp $^ $@ + +tty-server/assets_bundle.go: $(TTY_SERVER_ASSETS) + go-bindata --prefix public -o $@ $^ + frontend: FORCE cd frontend && npm run build && cd - diff --git a/frontend/src/main.js b/frontend/src/main.js index 2157f8c..c252afa 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -23,6 +23,8 @@ console.log(derivedKey); var wsAddress = 'ws://' + window.location.host + window.ttyInitialData.wsPath; var connection = new WebSocket(wsAddress); + + term.open(document.getElementById('terminal'), true); //term.attach(connection); diff --git a/tty-server/server.go b/tty-server/server.go index 931c4a2..d850b7a 100644 --- a/tty-server/server.go +++ b/tty-server/server.go @@ -50,7 +50,23 @@ func NewTTYProxyServer(config TTYProxyServerConfig) (server *TTYProxyServer) { Addr: config.WebAddress, } routesHandler := mux.NewRouter() - routesHandler.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("frontend")))) + + if config.FrontendPath != "" { + routesHandler.PathPrefix("/static/").Handler(http.StripPrefix("/static/", + http.FileServer(http.Dir(config.FrontendPath)))) + } else { + // Serve the bundled assets + routesHandler.PathPrefix("/static/").Handler(http.StripPrefix("/static/", + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data, err := Asset(r.URL.Path) + if err != nil { + w.WriteHeader(http.StatusNotFound) + return + } + w.Write(data) + log.Infof("Delivered %s from the bundle", r.URL.Path) + }))) + } routesHandler.HandleFunc("/", defaultHandler) routesHandler.HandleFunc("/s/{sessionID}", func(w http.ResponseWriter, r *http.Request) { @@ -122,7 +138,16 @@ func sessionsHandler(server *TTYProxyServer, w http.ResponseWriter, r *http.Requ return } - t, err := template.ParseFiles("./frontend/templates/index.html") + templateDta, err := Asset("templates/index.html") + + if err != nil { + fmt.Fprintf(w, err.Error()) + return + } + + t := template.New("index.html") + _, err = t.Parse(string(templateDta)) + if err != nil { fmt.Fprintf(w, err.Error()) return