Fix UI loading redirections. Should fix #906

This commit is contained in:
Deluan 2021-03-29 20:13:04 -04:00
parent 404253a881
commit 3b0defefec
3 changed files with 17 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"io/fs"
"io/ioutil"
"net/http"
"path"
"strings"
"github.com/microcosm-cc/bluemonday"
@ -19,6 +20,11 @@ import (
func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
policy := bluemonday.UGCPolicy()
return func(w http.ResponseWriter, r *http.Request) {
base := path.Join(conf.Server.BaseURL, consts.URLPathUI)
if r.URL.Path == base {
http.Redirect(w, r, base+"/", 302)
}
c, err := ds.User(r.Context()).CountAll()
firstTime := c == 0 && err == nil

View File

@ -26,6 +26,16 @@ var _ = Describe("serveIndex", func() {
conf.Server.UILoginBackgroundURL = ""
})
It("redirects bare /app path to /app/", func() {
r := httptest.NewRequest("GET", "/app", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
Expect(w.Code).To(Equal(302))
Expect(w.Header().Get("Location")).To(Equal("/app/"))
})
It("adds app_config to index.html", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()

View File

@ -61,7 +61,7 @@ func (a *Server) initRoutes() {
r.Use(requestLogger)
r.Use(robotsTXT(ui.Assets()))
indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI, "index.html")
indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI)
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, indexHtml, 302)
})