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.
pull/14/merge
Vasile Popescu 6 years ago
parent 9f0a1474f2
commit caaf694b6c

3
.gitignore vendored

@ -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

@ -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 -

@ -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);

@ -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

Loading…
Cancel
Save