From c82f5eccecbfa2a2e9e86aed058826e5bb659b86 Mon Sep 17 00:00:00 2001 From: Daniel Lundin Date: Sat, 20 Jul 2019 14:19:35 +0200 Subject: [PATCH] Fix embedded assets, particularly when serving UI/App urls --- Makefile | 8 +++++++- server.go | 11 +++++++++++ ui/public/index.html | 8 ++++---- ui/src/server.js | 4 ++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index dad1c39..24a1d5e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,12 @@ -binary: +.PHONY: binary container ui + +binary: ui + go-bindata-assetfs -prefix ui/public ui/public go build . +ui: + cd ui && npm install && npm run build + container: docker build -t wireguard-ui . diff --git a/server.go b/server.go index 0b8e68d..27b0633 100644 --- a/server.go +++ b/server.go @@ -288,6 +288,9 @@ func (s *Server) Start() error { log.Debug("Serving static assets proxying from development server: ", *devUIServer) devProxy := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { url, _ := url.Parse(*devUIServer) + if strings.HasPrefix(r.URL.Path, "/client/") || r.URL.Path == "/about" { + r.URL.Path = "/" + } proxy := httputil.NewSingleHostReverseProxy(url) r.URL.Host = url.Host r.URL.Scheme = url.Scheme @@ -298,6 +301,8 @@ func (s *Server) Start() error { router.NotFound = devProxy } else { log.Debug("Serving static assets embedded in binary") + router.GET("/about", s.Index) + router.GET("/client/:client", s.Index) router.NotFound = s.assets } @@ -371,6 +376,12 @@ func (s *Server) GetClients(w http.ResponseWriter, r *http.Request, ps httproute } } +func (s *Server) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + log.Debug("Serving single-page app from URL: ", r.URL) + r.URL.Path = "/" + s.assets.ServeHTTP(w, r) +} + func (s *Server) GetClient(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { user := r.Context().Value("user").(string) usercfg := s.Config.Users[user] diff --git a/ui/public/index.html b/ui/public/index.html index 642f404..9d03541 100644 --- a/ui/public/index.html +++ b/ui/public/index.html @@ -8,13 +8,13 @@ - - - + + + - + diff --git a/ui/src/server.js b/ui/src/server.js index 15daa1b..b61c029 100644 --- a/ui/src/server.js +++ b/ui/src/server.js @@ -1,5 +1,5 @@ const { createServer } = require("http"); -const app = require("./dist/App.js"); +const app = require("./App.js"); createServer((req, res) => { const { html } = app.render({ url: req.url }); @@ -7,7 +7,7 @@ createServer((req, res) => { res.write(` ${html} - + `); res.end();