From 9c9aee41467790c8e013df889f0703fc70e95aaf Mon Sep 17 00:00:00 2001 From: oers Date: Sat, 9 Jan 2016 14:06:17 +0100 Subject: [PATCH 1/3] fix #25 Provide a relative/local WebContext Path argument --- config/config.go | 3 +++ web/web.go | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index adb2def..f597067 100644 --- a/config/config.go +++ b/config/config.go @@ -10,12 +10,14 @@ func DefaultConfig() *Config { return &Config{ APIHost: "", UIBindAddr: "0.0.0.0:8025", + WebPath: "", } } type Config struct { APIHost string UIBindAddr string + WebPath string } var cfg = DefaultConfig() @@ -27,4 +29,5 @@ func Configure() *Config { func RegisterFlags() { flag.StringVar(&cfg.APIHost, "api-host", envconf.FromEnvP("MH_API_HOST", "").(string), "API URL for MailHog UI to connect to, e.g. http://some.host:1234") flag.StringVar(&cfg.UIBindAddr, "ui-bind-addr", envconf.FromEnvP("MH_UI_BIND_ADDR", "0.0.0.0:8025").(string), "HTTP bind interface and port for UI, e.g. 0.0.0.0:8025 or just :8025") + flag.StringVar(&cfg.WebPath, "ui-web-path", envconf.FromEnvP("MH_UI_WEB_PATH", "").(string), "WebPath under which the ui is served (without leading or trailing slahes), e.g. /mailhog defaults to ''") } diff --git a/web/web.go b/web/web.go index 51f40ca..fe5f7f6 100644 --- a/web/web.go +++ b/web/web.go @@ -14,6 +14,7 @@ import ( ) var APIHost string +var WebPath string type Web struct { config *config.Config @@ -26,11 +27,20 @@ func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, asset: asset, } - pat.Path("/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}")) - pat.Path("/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}")) - pat.Path("/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}")) - pat.Path("/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}")) - pat.Path("/").Methods("GET").HandlerFunc(web.Index()) + WebPath = cfg.WebPath + + //add a leading slash + if WebPath != "" && !(WebPath[0] == '/') { + WebPath = "/" + WebPath + } + + log.Printf("Serving under http://%s%s/", cfg.UIBindAddr, WebPath) + + pat.Path(WebPath + "/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}")) + pat.Path(WebPath + "/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}")) + pat.Path(WebPath + "/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}")) + pat.Path(WebPath + "/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}")) + pat.Path(WebPath + "/").Methods("GET").HandlerFunc(web.Index()) return web } From af8bf62f1636cfa23e36a62685977ca59d872261 Mon Sep 17 00:00:00 2001 From: oers Date: Sun, 6 Mar 2016 12:03:59 +0100 Subject: [PATCH 2/3] Fix for mailhog/Mailog#25 provide a ui-web-path config --- config/config.go | 1 - web/web.go | 5 ----- 2 files changed, 6 deletions(-) diff --git a/config/config.go b/config/config.go index f597067..9e0b2fe 100644 --- a/config/config.go +++ b/config/config.go @@ -29,5 +29,4 @@ func Configure() *Config { func RegisterFlags() { flag.StringVar(&cfg.APIHost, "api-host", envconf.FromEnvP("MH_API_HOST", "").(string), "API URL for MailHog UI to connect to, e.g. http://some.host:1234") flag.StringVar(&cfg.UIBindAddr, "ui-bind-addr", envconf.FromEnvP("MH_UI_BIND_ADDR", "0.0.0.0:8025").(string), "HTTP bind interface and port for UI, e.g. 0.0.0.0:8025 or just :8025") - flag.StringVar(&cfg.WebPath, "ui-web-path", envconf.FromEnvP("MH_UI_WEB_PATH", "").(string), "WebPath under which the ui is served (without leading or trailing slahes), e.g. /mailhog defaults to ''") } diff --git a/web/web.go b/web/web.go index fe5f7f6..ea15083 100644 --- a/web/web.go +++ b/web/web.go @@ -29,11 +29,6 @@ func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, WebPath = cfg.WebPath - //add a leading slash - if WebPath != "" && !(WebPath[0] == '/') { - WebPath = "/" + WebPath - } - log.Printf("Serving under http://%s%s/", cfg.UIBindAddr, WebPath) pat.Path(WebPath + "/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}")) From f04b69ecad003d0f1f770376ab972d6b5cf8ceec Mon Sep 17 00:00:00 2001 From: oers Date: Sun, 6 Mar 2016 12:27:08 +0100 Subject: [PATCH 3/3] Use Router.StrictSlash to allow calling of host:port/webpath instead of host:port/webppath/ --- web/web.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/web.go b/web/web.go index ea15083..44e4527 100644 --- a/web/web.go +++ b/web/web.go @@ -35,7 +35,7 @@ func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, pat.Path(WebPath + "/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}")) pat.Path(WebPath + "/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}")) pat.Path(WebPath + "/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}")) - pat.Path(WebPath + "/").Methods("GET").HandlerFunc(web.Index()) + pat.StrictSlash(true).Path(WebPath + "/").Methods("GET").HandlerFunc(web.Index()) return web }