2020-09-08 10:50:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-10 01:12:41 +00:00
|
|
|
"io"
|
2020-09-08 10:50:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2022-11-07 03:57:00 +00:00
|
|
|
"mime"
|
2020-09-08 10:50:41 +00:00
|
|
|
"net/http"
|
2021-12-10 01:12:41 +00:00
|
|
|
"net/url"
|
2020-09-08 10:50:41 +00:00
|
|
|
"path"
|
2022-11-06 15:36:31 +00:00
|
|
|
"strings"
|
2020-09-08 10:50:41 +00:00
|
|
|
|
2022-11-06 15:36:31 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-09-08 10:50:41 +00:00
|
|
|
)
|
|
|
|
|
2021-12-10 01:12:41 +00:00
|
|
|
var (
|
|
|
|
BaseURL = "/"
|
|
|
|
DevProxy string
|
|
|
|
indexTmpl []byte
|
|
|
|
)
|
2022-11-06 15:36:31 +00:00
|
|
|
|
2022-11-07 03:57:00 +00:00
|
|
|
func getIndexHtml(w io.Writer, file io.Reader) []byte {
|
|
|
|
if indexTmpl, err := ioutil.ReadAll(file); err != nil {
|
2021-12-10 01:12:41 +00:00
|
|
|
log.Println("Cannot read whole index.html: ", err)
|
2022-11-07 03:57:00 +00:00
|
|
|
return nil
|
2021-12-10 01:12:41 +00:00
|
|
|
} else {
|
2022-11-07 03:57:00 +00:00
|
|
|
good_base := []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4])
|
|
|
|
|
|
|
|
indexTmpl = bytes.Replace(
|
|
|
|
bytes.Replace(
|
|
|
|
bytes.Replace(
|
|
|
|
indexTmpl,
|
2023-02-19 11:28:04 +00:00
|
|
|
[]byte(`<base href="/">`),
|
|
|
|
[]byte(`<base href="`+string(good_base)+`">`),
|
2022-11-07 03:57:00 +00:00
|
|
|
-1,
|
|
|
|
),
|
|
|
|
[]byte("\"/_app/"),
|
|
|
|
append([]byte("\""), append(good_base, '_', 'a', 'p', 'p', '/')...),
|
|
|
|
-1,
|
|
|
|
),
|
2023-06-06 10:28:52 +00:00
|
|
|
[]byte("base: \""),
|
|
|
|
append([]byte("base: \""), good_base[:len(good_base)-1]...),
|
2022-11-07 03:57:00 +00:00
|
|
|
-1,
|
|
|
|
)
|
2020-09-08 10:50:41 +00:00
|
|
|
|
2022-11-07 03:57:00 +00:00
|
|
|
w.Write(indexTmpl)
|
|
|
|
return indexTmpl
|
|
|
|
}
|
2021-12-10 01:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serveOrReverse(forced_url string, baseURL string) func(c *gin.Context) {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
if DevProxy != "" {
|
|
|
|
if u, err := url.Parse(DevProxy); err != nil {
|
|
|
|
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
if forced_url != "" {
|
|
|
|
u.Path = path.Join(u.Path, forced_url)
|
|
|
|
} else {
|
|
|
|
u.Path = path.Join(u.Path, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
|
|
|
}
|
2020-09-08 10:50:41 +00:00
|
|
|
|
2021-12-10 01:12:41 +00:00
|
|
|
if r, err := http.NewRequest(c.Request.Method, u.String(), c.Request.Body); err != nil {
|
|
|
|
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else if resp, err := http.DefaultClient.Do(r); err != nil {
|
|
|
|
http.Error(c.Writer, err.Error(), http.StatusBadGateway)
|
|
|
|
} else {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
for key := range resp.Header {
|
|
|
|
c.Writer.Header().Add(key, resp.Header.Get(key))
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(resp.StatusCode)
|
|
|
|
|
|
|
|
if r.URL.Path == path.Join(u.Path, "/") {
|
|
|
|
getIndexHtml(c.Writer, resp.Body)
|
|
|
|
} else {
|
|
|
|
io.Copy(c.Writer, resp.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 10:50:41 +00:00
|
|
|
} else {
|
2021-12-10 01:12:41 +00:00
|
|
|
if forced_url != "" {
|
|
|
|
c.Request.URL.Path = forced_url
|
|
|
|
} else {
|
|
|
|
c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, baseURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Request.URL.Path == "/" {
|
|
|
|
if len(indexTmpl) == 0 {
|
|
|
|
if file, err := Assets.Open("index.html"); err != nil {
|
|
|
|
log.Println("Unable to open index.html: ", err)
|
|
|
|
} else {
|
|
|
|
defer file.Close()
|
2020-09-08 10:50:41 +00:00
|
|
|
|
2022-11-07 03:57:00 +00:00
|
|
|
indexTmpl = getIndexHtml(c.Writer, file)
|
2021-12-10 01:12:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Writer.Write(indexTmpl)
|
|
|
|
}
|
2022-11-07 03:57:00 +00:00
|
|
|
} 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)
|
|
|
|
}
|
2020-09-08 10:50:41 +00:00
|
|
|
} else {
|
2021-12-10 01:12:41 +00:00
|
|
|
http.FileServer(Assets).ServeHTTP(c.Writer, c.Request)
|
2020-09-08 10:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 15:36:31 +00:00
|
|
|
func declareStaticRoutes(router *gin.RouterGroup, baseURL string) {
|
2021-12-10 01:12:41 +00:00
|
|
|
router.GET("/", serveOrReverse("", baseURL))
|
|
|
|
router.GET("/exercices", serveOrReverse("/", baseURL))
|
|
|
|
router.GET("/exercices/*_", serveOrReverse("/", baseURL))
|
2023-06-06 22:11:28 +00:00
|
|
|
router.GET("/export", serveOrReverse("/", baseURL))
|
2023-07-25 09:17:40 +00:00
|
|
|
router.GET("/teams", serveOrReverse("/", baseURL))
|
|
|
|
router.GET("/teams/*_", serveOrReverse("/", baseURL))
|
2021-12-10 01:12:41 +00:00
|
|
|
router.GET("/themes", serveOrReverse("/", baseURL))
|
|
|
|
router.GET("/themes/*_", serveOrReverse("/", baseURL))
|
|
|
|
router.GET("/_app/*_", serveOrReverse("", baseURL))
|
2022-11-06 16:10:28 +00:00
|
|
|
|
|
|
|
router.GET("/.svelte-kit/*_", serveOrReverse("", baseURL))
|
|
|
|
router.GET("/node_modules/*_", serveOrReverse("", baseURL))
|
|
|
|
router.GET("/@vite/*_", serveOrReverse("", baseURL))
|
2022-11-06 14:14:35 +00:00
|
|
|
router.GET("/@fs/*_", serveOrReverse("", baseURL))
|
2023-07-25 07:36:43 +00:00
|
|
|
router.GET("/@id/*_", serveOrReverse("", baseURL))
|
2022-11-06 16:10:28 +00:00
|
|
|
router.GET("/src/*_", serveOrReverse("", baseURL))
|
2020-09-08 10:50:41 +00:00
|
|
|
}
|