mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Set base path in index.html (#172)
- Create a new index-dev.html for webpack live server - Update Go+index.html to set <base href> - Fixes #171
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -21,9 +21,11 @@ _testmain.go
|
|||||||
|
|
||||||
*.exe
|
*.exe
|
||||||
|
|
||||||
# vim swp files
|
# vim files
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
tags
|
||||||
|
tags.*
|
||||||
|
|
||||||
# Desktop Services Store on macOS
|
# Desktop Services Store on macOS
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/inbucket/inbucket/pkg/config"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -82,3 +84,21 @@ func requestLoggingWrapper(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, req)
|
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")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"expvar"
|
"expvar"
|
||||||
|
"html/template"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/pprof"
|
"net/http/pprof"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -86,9 +88,26 @@ func Initialize(
|
|||||||
Router.Path(prefix("/favicon.png")).Handler(
|
Router.Path(prefix("/favicon.png")).Handler(
|
||||||
fileHandler(filepath.Join(conf.Web.UIDir, "favicon.png")))
|
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.
|
// SPA managed paths.
|
||||||
spaHandler := cookieHandler(appConfigCookie(conf.Web),
|
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("/")).Handler(spaHandler)
|
||||||
Router.Path(prefix("/monitor")).Handler(spaHandler)
|
Router.Path(prefix("/monitor")).Handler(spaHandler)
|
||||||
Router.Path(prefix("/status")).Handler(spaHandler)
|
Router.Path(prefix("/status")).Handler(spaHandler)
|
||||||
|
|||||||
21
ui/public/index-dev.html
Normal file
21
ui/public/index-dev.html
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<!-- This index file will be served by the webpack development server. -->
|
||||||
|
<base href="/">
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
|
<meta name="theme-color" content="#000000">
|
||||||
|
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
|
||||||
|
<meta http-equiv="Pragma" content="no-cache" />
|
||||||
|
<meta http-equiv="Expires" content="0" />
|
||||||
|
<title>Inbucket</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<noscript>
|
||||||
|
You need to enable JavaScript to run this app.
|
||||||
|
</noscript>
|
||||||
|
<div id="root"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
|
<base href="{{ .BasePath }}">
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
|
|||||||
@@ -54,11 +54,18 @@ module.exports = (env, argv) => {
|
|||||||
template: 'public/index.html',
|
template: 'public/index.html',
|
||||||
favicon: 'public/favicon.png',
|
favicon: 'public/favicon.png',
|
||||||
}),
|
}),
|
||||||
|
new HtmlWebpackPlugin({
|
||||||
|
filename: 'index-dev.html',
|
||||||
|
template: 'public/index-dev.html',
|
||||||
|
favicon: 'public/favicon.png',
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
devServer: {
|
devServer: {
|
||||||
|
historyApiFallback: {
|
||||||
|
index: '/index-dev.html',
|
||||||
|
},
|
||||||
|
index: 'index-dev.html',
|
||||||
inline: true,
|
inline: true,
|
||||||
historyApiFallback: true,
|
|
||||||
stats: { colors: true },
|
|
||||||
overlay: true,
|
overlay: true,
|
||||||
open: true,
|
open: true,
|
||||||
proxy: [{
|
proxy: [{
|
||||||
@@ -66,6 +73,7 @@ module.exports = (env, argv) => {
|
|||||||
target: 'http://localhost:9000',
|
target: 'http://localhost:9000',
|
||||||
ws: true,
|
ws: true,
|
||||||
}],
|
}],
|
||||||
|
stats: { colors: true },
|
||||||
watchOptions: {
|
watchOptions: {
|
||||||
ignored: /node_modules/,
|
ignored: /node_modules/,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user