diff --git a/Makefile b/Makefile index 32d2a129..b9c7e318 100644 --- a/Makefile +++ b/Makefile @@ -58,6 +58,7 @@ web-build: && rm -rf ../server/site \ && mv build ../server/site \ && rm \ + ../server/site/config.js \ ../server/site/precache* \ ../server/site/service-worker.js \ ../server/site/asset-manifest.json \ diff --git a/server/server.go b/server/server.go index 74caa261..3aff4e15 100644 --- a/server/server.go +++ b/server/server.go @@ -65,6 +65,7 @@ var ( authPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/auth$`) publishPathRegex = regexp.MustCompile(`^/[-_A-Za-z0-9]{1,64}(,[-_A-Za-z0-9]{1,64})*/(publish|send|trigger)$`) + webConfigPath = "/config.js" staticRegex = regexp.MustCompile(`^/static/.+`) docsRegex = regexp.MustCompile(`^/docs(|/.*)$`) fileRegex = regexp.MustCompile(`^/file/([-_A-Za-z0-9]{1,64})(?:\.[A-Za-z0-9]{1,16})?$`) @@ -266,6 +267,8 @@ func (s *Server) handleInternal(w http.ResponseWriter, r *http.Request, v *visit return s.handleExample(w, r) } else if r.Method == http.MethodHead && r.URL.Path == "/" { return s.handleEmpty(w, r, v) + } else if r.Method == http.MethodGet && r.URL.Path == webConfigPath { + return s.handleWebConfig(w, r) } else if r.Method == http.MethodGet && staticRegex.MatchString(r.URL.Path) { return s.handleStatic(w, r) } else if r.Method == http.MethodGet && docsRegex.MatchString(r.URL.Path) { @@ -331,6 +334,20 @@ func (s *Server) handleExample(w http.ResponseWriter, _ *http.Request) error { return err } +func (s *Server) handleWebConfig(w http.ResponseWriter, r *http.Request) error { + appRoot := "/" + if !s.config.WebRootIsApp { + appRoot = "/app" + } + disallowedTopicsStr := `"` + strings.Join(disallowedTopics, `", "`) + `"` + _, err := io.WriteString(w, fmt.Sprintf(`// Generated server configuration +var config = { + appRoot: "%s", + disallowedTopics: [%s] +};`, appRoot, disallowedTopicsStr)) + return err +} + func (s *Server) handleStatic(w http.ResponseWriter, r *http.Request) error { r.URL.Path = webSiteDir + r.URL.Path http.FileServer(http.FS(webFsCached)).ServeHTTP(w, r) diff --git a/web/public/config.js b/web/public/config.js new file mode 100644 index 00000000..cd5fbf05 --- /dev/null +++ b/web/public/config.js @@ -0,0 +1,9 @@ +// Configuration injected by the ntfy server. +// +// This file is just an example. It is removed during the build process. +// The actual config is dynamically generated server-side. + +var config = { + appRoot: "/", + disallowedTopics: ["docs", "static", "file", "app", "settings"] +}; diff --git a/web/public/index.html b/web/public/index.html index 232b0cf9..93aa4af5 100644 --- a/web/public/index.html +++ b/web/public/index.html @@ -15,13 +15,13 @@ - + - + @@ -30,10 +30,14 @@ - +
- + +