From 5a0c4778cb03460d227625c98beb26181fae1b5a Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sat, 29 Aug 2020 19:06:21 -0700 Subject: [PATCH] Set base path in index.html (#172) - Create a new index-dev.html for webpack live server - Update Go+index.html to set - Fixes #171 --- .gitignore | 4 +++- pkg/server/web/handlers.go | 20 ++++++++++++++++++++ pkg/server/web/server.go | 21 ++++++++++++++++++++- ui/public/index-dev.html | 21 +++++++++++++++++++++ ui/public/index.html | 1 + ui/webpack.config.js | 12 ++++++++++-- 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 ui/public/index-dev.html diff --git a/.gitignore b/.gitignore index f4417f8..44e149b 100644 --- a/.gitignore +++ b/.gitignore @@ -21,9 +21,11 @@ _testmain.go *.exe -# vim swp files +# vim files *.swp *.swo +tags +tags.* # Desktop Services Store on macOS .DS_Store diff --git a/pkg/server/web/handlers.go b/pkg/server/web/handlers.go index 15c65ec..385c2b1 100644 --- a/pkg/server/web/handlers.go +++ b/pkg/server/web/handlers.go @@ -1,9 +1,11 @@ package web import ( + "html/template" "net/http" "os" + "github.com/inbucket/inbucket/pkg/config" "github.com/rs/zerolog/log" ) @@ -82,3 +84,21 @@ func requestLoggingWrapper(next http.Handler) http.Handler { next.ServeHTTP(w, req) }) } + +// spaTemplateHandler creates a handler to serve the index.html template for our SPA. +func spaTemplateHandler(tmpl *template.Template, basePath string, + webConfig config.Web) http.Handler { + tmplData := struct { + BasePath string + }{ + BasePath: basePath, + } + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + err := tmpl.Execute(w, tmplData) + if err != nil { + log.Error().Str("module", "web").Str("remote", req.RemoteAddr).Str("proto", req.Proto). + Str("method", req.Method).Str("path", req.RequestURI).Err(err). + Msg("Error rendering SPA index template") + } + }) +} diff --git a/pkg/server/web/server.go b/pkg/server/web/server.go index 08a19e9..b4ce078 100644 --- a/pkg/server/web/server.go +++ b/pkg/server/web/server.go @@ -5,10 +5,12 @@ import ( "context" "encoding/json" "expvar" + "html/template" "net" "net/http" "net/http/pprof" "net/url" + "os" "path/filepath" "time" @@ -86,9 +88,26 @@ func Initialize( Router.Path(prefix("/favicon.png")).Handler( fileHandler(filepath.Join(conf.Web.UIDir, "favicon.png"))) + // Parse index.html template, allowing for configuration to be passed to the SPA. + indexPath := filepath.Join(conf.Web.UIDir, "index.html") + indexTmpl, err := template.ParseFiles(indexPath) + if err != nil { + msg := "Failed to parse HTML template" + cwd, _ := os.Getwd() + log.Error(). + Str("module", "web"). + Str("phase", "startup"). + Str("path", indexPath). + Str("cwd", cwd). + Err(err). + Msg(msg) + // Create a dummy template to allow tests to pass. + indexTmpl, _ = template.New("index.html").Parse(msg) + } + // SPA managed paths. spaHandler := cookieHandler(appConfigCookie(conf.Web), - fileHandler(filepath.Join(conf.Web.UIDir, "index.html"))) + spaTemplateHandler(indexTmpl, prefix("/"), conf.Web)) Router.Path(prefix("/")).Handler(spaHandler) Router.Path(prefix("/monitor")).Handler(spaHandler) Router.Path(prefix("/status")).Handler(spaHandler) diff --git a/ui/public/index-dev.html b/ui/public/index-dev.html new file mode 100644 index 0000000..c75f6c0 --- /dev/null +++ b/ui/public/index-dev.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + Inbucket + + + +
+ + diff --git a/ui/public/index.html b/ui/public/index.html index 9c8f8ad..dd4b511 100644 --- a/ui/public/index.html +++ b/ui/public/index.html @@ -1,6 +1,7 @@ + diff --git a/ui/webpack.config.js b/ui/webpack.config.js index 2310dc8..4e577e7 100644 --- a/ui/webpack.config.js +++ b/ui/webpack.config.js @@ -54,11 +54,18 @@ module.exports = (env, argv) => { template: 'public/index.html', favicon: 'public/favicon.png', }), + new HtmlWebpackPlugin({ + filename: 'index-dev.html', + template: 'public/index-dev.html', + favicon: 'public/favicon.png', + }), ], devServer: { + historyApiFallback: { + index: '/index-dev.html', + }, + index: 'index-dev.html', inline: true, - historyApiFallback: true, - stats: { colors: true }, overlay: true, open: true, proxy: [{ @@ -66,6 +73,7 @@ module.exports = (env, argv) => { target: 'http://localhost:9000', ws: true, }], + stats: { colors: true }, watchOptions: { ignored: /node_modules/, },