qa: Make some crappy hacks to let sveltekit work with baseurl

This commit is contained in:
nemunaire 2022-11-07 04:57:00 +01:00
commit 5560a526b1
6 changed files with 39 additions and 20 deletions

View file

@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"log"
"mime"
"net/http"
"net/url"
"path"
@ -19,15 +20,33 @@ var (
indexTmpl []byte
)
func getIndexHtml(w io.Writer, file io.Reader) {
var err error
if indexTmpl, err = ioutil.ReadAll(file); err != nil {
func getIndexHtml(w io.Writer, file io.Reader) []byte {
if indexTmpl, err := ioutil.ReadAll(file); err != nil {
log.Println("Cannot read whole index.html: ", err)
return nil
} else {
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1)
}
good_base := []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4])
w.Write(indexTmpl)
indexTmpl = bytes.Replace(
bytes.Replace(
bytes.Replace(
indexTmpl,
[]byte("{{.urlbase}}"),
good_base,
-1,
),
[]byte("\"/_app/"),
append([]byte("\""), append(good_base, '_', 'a', 'p', 'p', '/')...),
-1,
),
[]byte("paths: {\"base\":\""),
append([]byte("paths: {\"base\":\""), good_base[:len(good_base)-1]...),
-1,
)
w.Write(indexTmpl)
return indexTmpl
}
}
func serveOrReverse(forced_url string, baseURL string) func(c *gin.Context) {
@ -75,11 +94,21 @@ func serveOrReverse(forced_url string, baseURL string) func(c *gin.Context) {
} else {
defer file.Close()
getIndexHtml(c.Writer, file)
indexTmpl = getIndexHtml(c.Writer, file)
}
} else {
c.Writer.Write(indexTmpl)
}
} else if baseURL != "/" {
if file, err := Assets.Open(c.Request.URL.Path); err != nil {
http.FileServer(Assets).ServeHTTP(c.Writer, c.Request)
} else {
defer file.Close()
c.Writer.Header().Add("Content-Type", mime.TypeByExtension(path.Ext(c.Request.URL.Path)))
getIndexHtml(c.Writer, file)
}
} else {
http.FileServer(Assets).ServeHTTP(c.Writer, c.Request)
}