diff --git a/cmd/cli.go b/cmd/cli.go index b6605153..e5c2a713 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -274,6 +274,7 @@ func HelpEcho() { fmt.Println(" HTTP_PROXY - Use a HTTP Proxy for HTTP Requests") fmt.Println(" AUTH_USERNAME - HTTP Basic Authentication username") fmt.Println(" AUTH_PASSWORD - HTTP Basic Authentication password") + fmt.Println(" BASE_PATH - Set the base URL prefix (set to 'monitor' if URL is domain.com/monitor)") fmt.Println(" * You can insert environment variables into a '.env' file in root directory.") fmt.Println("Give Statping a Star at https://github.com/hunterlong/statping") } diff --git a/handlers/function.go b/handlers/function.go index 7ac44649..2928e11d 100644 --- a/handlers/function.go +++ b/handlers/function.go @@ -12,6 +12,10 @@ import ( "time" ) +var ( + basePath = "/" +) + var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap { return template.FuncMap{ "js": func(html interface{}) template.JS { @@ -88,7 +92,7 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap return utils.UnderScoreString(html) }, "URL": func() string { - return r.URL.String() + return basePath + r.URL.String() }, "CHART_DATA": func() string { return "" @@ -145,5 +149,8 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap "NewGroup": func() *types.Group { return new(types.Group) }, + "BasePath": func() string { + return basePath + }, } } diff --git a/handlers/handlers.go b/handlers/handlers.go index 1e54a6dd..23f05e8d 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -45,13 +45,16 @@ var ( mainTmpl = `{{define "main" }} {{ template "base" . }} {{ end }}` templates = []string{"base.gohtml", "head.gohtml", "nav.gohtml", "footer.gohtml", "scripts.gohtml", "form_service.gohtml", "form_notifier.gohtml", "form_integration.gohtml", "form_group.gohtml", "form_user.gohtml", "form_checkin.gohtml", "form_message.gohtml"} javascripts = []string{"charts.js", "chart_index.js"} - mainTemplate *template.Template ) // RunHTTPServer will start a HTTP server on a specific IP and port func RunHTTPServer(ip string, port int) error { host := fmt.Sprintf("%v:%v", ip, port) + if os.Getenv("BASE_PATH") != "" { + basePath = "/" + os.Getenv("BASE_PATH") + } + key := utils.FileExists(utils.Directory + "/server.key") cert := utils.FileExists(utils.Directory + "/server.crt") @@ -184,22 +187,22 @@ func IsUser(r *http.Request) bool { return session.Values["authenticated"].(bool) } -func loadTemplate(w http.ResponseWriter, r *http.Request) error { +func loadTemplate(w http.ResponseWriter, r *http.Request) (*template.Template, error) { var err error - mainTemplate = template.New("main") - mainTemplate.Funcs(handlerFuncs(w, r)) + mainTemplate := template.New("main") mainTemplate, err = mainTemplate.Parse(mainTmpl) if err != nil { log.Errorln(err) - return err + return nil, err } + mainTemplate.Funcs(handlerFuncs(w, r)) // render all templates for _, temp := range templates { tmp, _ := source.TmplBox.String(temp) mainTemplate, err = mainTemplate.Parse(tmp) if err != nil { log.Errorln(err) - return err + return nil, err } } // render all javascript files @@ -208,22 +211,25 @@ func loadTemplate(w http.ResponseWriter, r *http.Request) error { mainTemplate, err = mainTemplate.Parse(tmp) if err != nil { log.Errorln(err) - return err + return nil, err } } - return err + return mainTemplate, err } // ExecuteResponse will render a HTTP response for the front end user func ExecuteResponse(w http.ResponseWriter, r *http.Request, file string, data interface{}, redirect interface{}) { if url, ok := redirect.(string); ok { - http.Redirect(w, r, url, http.StatusSeeOther) + http.Redirect(w, r, basePath+url, http.StatusSeeOther) return } if usingSSL { w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains") } - loadTemplate(w, r) + mainTemplate, err := loadTemplate(w, r) + if err != nil { + log.Errorln(err) + } render, err := source.TmplBox.String(file) if err != nil { log.Errorln(err) diff --git a/handlers/middleware.go b/handlers/middleware.go index 02c6df44..73df055e 100644 --- a/handlers/middleware.go +++ b/handlers/middleware.go @@ -54,7 +54,7 @@ func authenticated(handler func(w http.ResponseWriter, r *http.Request), redirec return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !IsFullAuthenticated(r) { if redirect { - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, basePath, http.StatusSeeOther) } else { sendUnauthorizedJson(w, r) } @@ -69,7 +69,7 @@ func readOnly(handler func(w http.ResponseWriter, r *http.Request), redirect boo return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !IsReadAuthenticated(r) { if redirect { - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, basePath, http.StatusSeeOther) } else { sendUnauthorizedJson(w, r) } diff --git a/handlers/notifications.go b/handlers/notifications.go index 72ac4559..954bd9f2 100644 --- a/handlers/notifications.go +++ b/handlers/notifications.go @@ -86,7 +86,7 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) { fakeNotifer, notif, err := notifier.SelectNotifier(method) if err != nil { log.Errorln(fmt.Sprintf("issue saving notifier %v: %v", method, err)) - ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings") + ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "settings") return } diff --git a/handlers/routes.go b/handlers/routes.go index 343a3b23..acd084d7 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -39,25 +39,30 @@ func Router() *mux.Router { dir := utils.Directory CacheStorage = NewStorage() r := mux.NewRouter().StrictSlash(true) + if os.Getenv("AUTH_USERNAME") != "" && os.Getenv("AUTH_PASSWORD") != "" { authUser = os.Getenv("AUTH_USERNAME") authPass = os.Getenv("AUTH_PASSWORD") r.Use(basicAuthHandler) } + if basePath != "/" { + r = r.PathPrefix(basePath).Subrouter() + } + r.Use(sendLog) r.Handle("/", http.HandlerFunc(indexHandler)) if source.UsingAssets(dir) { indexHandler := http.FileServer(http.Dir(dir + "/assets/")) - r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir(dir+"/assets/css")))) - r.PathPrefix("/font/").Handler(http.StripPrefix("/font/", http.FileServer(http.Dir(dir+"/assets/font")))) - r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(http.Dir(dir+"/assets/js")))) + r.PathPrefix("/css/").Handler(http.StripPrefix(basePath+"/css/", http.FileServer(http.Dir(dir+"/assets/css")))) + r.PathPrefix("/font/").Handler(http.StripPrefix(basePath+"/font/", http.FileServer(http.Dir(dir+"/assets/font")))) + r.PathPrefix("/js/").Handler(http.StripPrefix(basePath+"/js/", http.FileServer(http.Dir(dir+"/assets/js")))) r.PathPrefix("/robots.txt").Handler(indexHandler) r.PathPrefix("/favicon.ico").Handler(indexHandler) r.PathPrefix("/banner.png").Handler(indexHandler) } else { - r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(source.CssBox.HTTPBox()))) - r.PathPrefix("/font/").Handler(http.StripPrefix("/font/", http.FileServer(source.FontBox.HTTPBox()))) - r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(source.JsBox.HTTPBox()))) + r.PathPrefix("/css/").Handler(http.StripPrefix(basePath+"/css/", http.FileServer(source.CssBox.HTTPBox()))) + r.PathPrefix("/font/").Handler(http.StripPrefix(basePath+"/font/", http.FileServer(source.FontBox.HTTPBox()))) + r.PathPrefix("/js/").Handler(http.StripPrefix(basePath+"/js/", http.FileServer(source.JsBox.HTTPBox()))) r.PathPrefix("/robots.txt").Handler(http.FileServer(source.TmplBox.HTTPBox())) r.PathPrefix("/favicon.ico").Handler(http.FileServer(source.TmplBox.HTTPBox())) r.PathPrefix("/banner.png").Handler(http.FileServer(source.TmplBox.HTTPBox())) diff --git a/handlers/settings.go b/handlers/settings.go index 65fa0213..918551ec 100644 --- a/handlers/settings.go +++ b/handlers/settings.go @@ -73,7 +73,7 @@ func saveSettingsHandler(w http.ResponseWriter, r *http.Request) { } //notifiers.OnSettingsSaved(core.CoreApp.ToCore()) - ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings") + ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "settings") } func saveSASSHandler(w http.ResponseWriter, r *http.Request) { @@ -86,7 +86,7 @@ func saveSASSHandler(w http.ResponseWriter, r *http.Request) { source.SaveAsset([]byte(mobile), utils.Directory, "scss/mobile.scss") source.CompileSASS(utils.Directory) resetRouter() - ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings") + ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "settings") } func saveAssetsHandler(w http.ResponseWriter, r *http.Request) { @@ -101,7 +101,7 @@ func saveAssetsHandler(w http.ResponseWriter, r *http.Request) { log.Errorln("Default 'base.css' was inserted because SASS did not work.") } resetRouter() - ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings") + ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "settings") } func deleteAssetsHandler(w http.ResponseWriter, r *http.Request) { @@ -109,7 +109,7 @@ func deleteAssetsHandler(w http.ResponseWriter, r *http.Request) { log.Errorln(fmt.Errorf("error deleting all assets %v", err)) } resetRouter() - ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings") + ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "settings") } func bulkImportHandler(w http.ResponseWriter, r *http.Request) { diff --git a/source/tmpl/form_checkin.gohtml b/source/tmpl/form_checkin.gohtml index 24079cf4..324ebf93 100644 --- a/source/tmpl/form_checkin.gohtml +++ b/source/tmpl/form_checkin.gohtml @@ -1,7 +1,7 @@ {{define "form_checkin"}}
-
+
diff --git a/source/tmpl/form_group.gohtml b/source/tmpl/form_group.gohtml index 683b5e28..e113881f 100644 --- a/source/tmpl/form_group.gohtml +++ b/source/tmpl/form_group.gohtml @@ -2,7 +2,7 @@
{{$message := .}} - +
diff --git a/source/tmpl/form_incident.gohtml b/source/tmpl/form_incident.gohtml index 694dcd82..7971e20f 100644 --- a/source/tmpl/form_incident.gohtml +++ b/source/tmpl/form_incident.gohtml @@ -2,7 +2,7 @@
{{$message := .}} - +
diff --git a/source/tmpl/form_integration.gohtml b/source/tmpl/form_integration.gohtml index 1fd7f1ea..dfb9d627 100644 --- a/source/tmpl/form_integration.gohtml +++ b/source/tmpl/form_integration.gohtml @@ -1,6 +1,6 @@ {{define "form_integration"}} {{$i := .Get}} - + {{if $i.ShortName}}

{{$i.ShortName}}

{{end}} {{if $i.Description}}

{{safe $i.Description}}

{{end}} diff --git a/source/tmpl/form_message.gohtml b/source/tmpl/form_message.gohtml index 0b0fb0bf..8b4b21e4 100644 --- a/source/tmpl/form_message.gohtml +++ b/source/tmpl/form_message.gohtml @@ -2,7 +2,7 @@
{{$message := .}} - +
diff --git a/source/tmpl/form_notifier.gohtml b/source/tmpl/form_notifier.gohtml index 33631e5d..3cd78d66 100644 --- a/source/tmpl/form_notifier.gohtml +++ b/source/tmpl/form_notifier.gohtml @@ -1,6 +1,6 @@ {{define "form_notifier"}} {{$n := .Select}} - + {{if $n.Title}}

{{$n.Title}}

{{end}} {{if $n.Description}}

{{safe $n.Description}}

{{end}} diff --git a/source/tmpl/form_service.gohtml b/source/tmpl/form_service.gohtml index e07712e4..197181c8 100644 --- a/source/tmpl/form_service.gohtml +++ b/source/tmpl/form_service.gohtml @@ -2,7 +2,7 @@
{{$s := .}} - +

Basic Information

diff --git a/source/tmpl/form_user.gohtml b/source/tmpl/form_user.gohtml index e0cdfee4..749191e8 100644 --- a/source/tmpl/form_user.gohtml +++ b/source/tmpl/form_user.gohtml @@ -1,7 +1,7 @@ {{define "form_user"}}
- +
diff --git a/source/tmpl/head.gohtml b/source/tmpl/head.gohtml index 9eae5ed3..04a2d12a 100644 --- a/source/tmpl/head.gohtml +++ b/source/tmpl/head.gohtml @@ -4,16 +4,17 @@ {{block "title" .}} {{end}} + {{if USE_CDN}} {{ else }} - - - - + + + + {{end}} {{block "extra_css" .}} {{end}} diff --git a/source/tmpl/login.gohtml b/source/tmpl/login.gohtml index a6872433..9af12577 100644 --- a/source/tmpl/login.gohtml +++ b/source/tmpl/login.gohtml @@ -10,7 +10,7 @@ Incorrect login information submitted, try again.
{{ end }} - +
diff --git a/source/tmpl/nav.gohtml b/source/tmpl/nav.gohtml index 35db6e6e..b0d3f0d6 100644 --- a/source/tmpl/nav.gohtml +++ b/source/tmpl/nav.gohtml @@ -1,40 +1,40 @@ {{define "nav"}} {{$isAdmin := Auth}} -{{end}} \ No newline at end of file +{{end}} diff --git a/source/tmpl/scripts.gohtml b/source/tmpl/scripts.gohtml index af8612a5..29064871 100644 --- a/source/tmpl/scripts.gohtml +++ b/source/tmpl/scripts.gohtml @@ -5,10 +5,10 @@ {{ else }} - - - - + + + + {{end}} {{block "extra_scripts" .}} {{end}} {{end}} diff --git a/source/tmpl/settings.gohtml b/source/tmpl/settings.gohtml index 0aaa2b6f..f3c64278 100644 --- a/source/tmpl/settings.gohtml +++ b/source/tmpl/settings.gohtml @@ -35,7 +35,7 @@ {{end}}
- +
@@ -196,7 +196,7 @@
{{ else }} - +
{{ end }} - +
diff --git a/utils/utils_windows.go b/utils/utils_windows.go index d2f1135f..3082b113 100644 --- a/utils/utils_windows.go +++ b/utils/utils_windows.go @@ -1,6 +1,9 @@ package utils -import "errors" +import ( + "errors" + "os" +) func DirWritable(path string) (bool, error) { info, err := os.Stat(path)