qa-svelte: initial commit

This commit is contained in:
nemunaire 2021-12-10 02:12:41 +01:00
commit 0fe037d7f5
43 changed files with 2089 additions and 1297 deletions

View file

@ -2,74 +2,96 @@ package main
import (
"bytes"
"embed"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"net/url"
"path"
"strings"
"github.com/gin-gonic/gin"
)
//go:embed static
var assets embed.FS
var (
BaseURL = "/"
DevProxy string
indexTmpl []byte
)
var BaseURL = "/"
func getIndexHtml(w io.Writer, file io.Reader) {
var err error
if indexTmpl, err = ioutil.ReadAll(file); err != nil {
log.Println("Cannot read whole index.html: ", err)
} else {
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1)
}
var indexTmpl []byte
w.Write(indexTmpl)
}
func getIndexHtml(c *gin.Context) {
if len(indexTmpl) == 0 {
if file, err := os.Open(path.Join(StaticDir, "index.html")); err != nil {
log.Println("Unable to open index.html: ", err)
} else {
defer file.Close()
if indexTmpl, err = ioutil.ReadAll(file); err != nil {
log.Println("Cannot read whole index.html: ", err)
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 {
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1)
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))
}
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)
}
}
}
} else {
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()
getIndexHtml(c.Writer, file)
}
} else {
c.Writer.Write(indexTmpl)
}
} else {
http.FileServer(Assets).ServeHTTP(c.Writer, c.Request)
}
}
}
c.Writer.Write(indexTmpl)
}
var staticFS http.FileSystem
func serveFile(c *gin.Context, url string) {
c.Request.URL.Path = url
http.FileServer(staticFS).ServeHTTP(c.Writer, c.Request)
}
func declareStaticRoutes(router *gin.RouterGroup, baseURL string) {
router.GET("/", func(c *gin.Context) {
getIndexHtml(c)
})
router.GET("/exercices/*_", func(c *gin.Context) {
getIndexHtml(c)
})
router.GET("/themes/*_", func(c *gin.Context) {
getIndexHtml(c)
})
router.GET("/css/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
router.GET("/fonts/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
router.GET("/img/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
router.GET("/js/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
router.GET("/views/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
router.GET("/", serveOrReverse("", baseURL))
router.GET("/exercices", serveOrReverse("/", baseURL))
router.GET("/exercices/*_", serveOrReverse("/", baseURL))
router.GET("/themes", serveOrReverse("/", baseURL))
router.GET("/themes/*_", serveOrReverse("/", baseURL))
router.GET("/_app/*_", serveOrReverse("", baseURL))
}